-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInsertInterval.java
More file actions
65 lines (52 loc) · 1.84 KB
/
InsertInterval.java
File metadata and controls
65 lines (52 loc) · 1.84 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
package interval;
import java.util.ArrayList;
import java.util.List;
/**
* Description: https://leetcode.com/problems/insert-interval
* Difficulty: Medium
* Time complexity: O(n)
* Space complexity: O(n)
*/
public class InsertInterval {
public int[][] insertWithThreeLoops(int[][] intervals, int[] newInterval) {
List<int[]> result = new ArrayList<>();
int i = 0;
// insert all intervals before the new one to the left
while (i < intervals.length && newInterval[0] > intervals[i][1]) {
result.add(intervals[i]);
i++;
}
// merge overlaps
while (i < intervals.length && newInterval[1] >= intervals[i][0]) {
newInterval[0] = Math.min(newInterval[0], intervals[i][0]);
newInterval[1] = Math.max(newInterval[1], intervals[i][1]);
i++;
}
result.add(newInterval);
// insert all the rest intervals to the right
while (i < intervals.length) {
result.add(intervals[i]);
i++;
}
return result.toArray(new int[0][]);
}
public int[][] insertWithSingleLoop(int[][] intervals, int[] newInterval) {
List<int[]> result = new ArrayList<>();
for (int[] slot : intervals) {
if (newInterval[1] < slot[0]) {
// newInterval goes on the left
result.add(newInterval);
newInterval = slot;
} else if (newInterval[0] > slot[1]) {
// newInterval goes on the right
result.add(slot);
} else {
// overlap
newInterval[0] = Math.min(newInterval[0], slot[0]);
newInterval[1] = Math.max(newInterval[1], slot[1]);
}
}
result.add(newInterval);
return result.toArray(new int[0][]);
}
}