-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathMergeIntervals.java
More file actions
68 lines (57 loc) · 1.62 KB
/
MergeIntervals.java
File metadata and controls
68 lines (57 loc) · 1.62 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package algorithm.lc;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
/**
* Given a collection of intervals, merge all overlapping intervals.
*
* For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18].
*
*/
// O(n) space, O(nlgn) time
public class MergeIntervals {
public class Interval {
int start;
int end;
Interval() {
start = 0;
end = 0;
}
Interval(int s, int e) {
start = s;
end = e;
}
}
public class Solution {
public ArrayList<Interval> merge(ArrayList<Interval> intervals) {
// Start typing your Java solution below
// DO NOT write main() function
ArrayList<Interval> res = new ArrayList<Interval>();
if (intervals.size() == 0) {
return res;
}
Collections.sort(intervals, new IntervalSorter());
int start = intervals.get(0).start;
int end = intervals.get(0).end;
for (int i = 1; i < intervals.size(); ++i) {
int curStart = intervals.get(i).start;
int curEnd = intervals.get(i).end;
if (end >= curStart) { // can merge, update end
end = Math.max(end, curEnd);
} else { // cannot merge, add previous one
res.add(new Interval(start, end));
start = curStart;
end = curEnd;
}
}
res.add(new Interval(start, end));
return res;
}
private class IntervalSorter implements Comparator<Interval> {
public int compare(Interval i1, Interval i2) {
int cmp = i1.start - i2.start;
return cmp == 0 ? i1.end - i2.end : cmp;
}
}
}
}