-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMergeIntervals.java
More file actions
38 lines (30 loc) · 962 Bytes
/
MergeIntervals.java
File metadata and controls
38 lines (30 loc) · 962 Bytes
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
36
37
38
package interval;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Description: https://leetcode.com/problems/merge-intervals
* Difficulty: Medium
* Time complexity: O(nlog n)
* Space complexity: O(n)
*/
public class MergeIntervals {
public int[][] merge(int[][] intervals) {
if (intervals.length < 2) return intervals;
Arrays.sort(intervals, (i1, i2) -> Integer.compare(i1[0], i2[0]));
int start = intervals[0][0];
int end = intervals[0][1];
List<int[]> result = new ArrayList<>();
for (int[] current : intervals) {
if (current[0] > end) {
result.add(new int[]{start, end});
start = current[0];
end = current[1];
} else {
end = Math.max(current[1], end);
}
}
result.add(new int[]{start, end});
return result.toArray(new int[0][]);
}
}