Merge Intervals 2021-06-15 20:11

Problem Description

public int[][] merge(int[][] intervals) {
    Arrays.sort(intervals, Comparator.comparingInt(a -> a[0]));
    LinkedList<int[]> merged = new LinkedList<>();
    for (int[] array : intervals) {
        if (merged.isEmpty() || merged.getLast()[1] < array[0]) {
            merged.add(array);
        } else {
            merged.getLast()[1] = Math.max(array[1], merged.getLast()[1]);
        }
    }
    return merged.toArray(new int[merged.size()][]);
}
Runtime Memory
6 ms 41.5 MB

henryxi leetcode list

EOF