-
Notifications
You must be signed in to change notification settings - Fork 1
/
InsertInterval.java
48 lines (46 loc) · 1.42 KB
/
InsertInterval.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
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedList;
/**
* 57. Insert Interval.
* @author LBW
*/
public class InsertInterval {
public int[][] insert(int[][] intervals, int[] newInterval) {
// sort
Arrays.sort(intervals, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o1[0] - o2[0];
}
});
// Greedy Algorithm
LinkedList<int[]> results = new LinkedList<>();
int n = intervals.length;
int i = 0;
for (; i < n && intervals[i][0] <= newInterval[0]; i++) {
results.add(intervals[i]);
}
if (i == 0 || newInterval[0] > intervals[i-1][1]) { // no overlap
results.add(newInterval);
}
else {
results.getLast()[1] = Math.max(results.getLast()[1], newInterval[1]);
}
for (; i < n; i++) {
if (intervals[i][1] <= results.getLast()[1]) {
continue;
}
if (intervals[i][0] > results.getLast()[1]) { // normal
for (; i < n; i++) {
results.add(intervals[i]);
}
break;
}
else {
results.getLast()[1] = Math.max(results.getLast()[1], intervals[i][1]);
}
}
return results.toArray(new int[results.size()][2]);
}
}