-
Notifications
You must be signed in to change notification settings - Fork 16
/
MergeIntervals.java
35 lines (32 loc) · 1.11 KB
/
MergeIntervals.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// https://leetcode.com/problems/merge-intervals/submissions
// T: O(n log(n))
// S: O(n)
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class MergeIntervals {
public int[][] merge(int[][] intervals) {
Arrays.sort(intervals, Comparator.comparingInt(interval -> interval[0]));
final List<int[]> mergedIntervals = new ArrayList<>();
int start = intervals[0][0], stop = intervals[0][1];
for (int i = 1 ; i < intervals.length ; i++) {
if (intervals[i][0] <= stop) stop = Math.max(stop, intervals[i][1]);
else {
mergedIntervals.add(new int[] {start, stop});
start = intervals[i][0];
stop = intervals[i][1];
}
}
mergedIntervals.add(new int[] {start, stop});
return toArray(mergedIntervals);
}
private int[][] toArray(List<int[]> list) {
int[][] result = new int[list.size()][2];
int i = 0;
for (int[] interval : list) {
result[i++] = interval;
}
return result;
}
}