-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0057-insert-interval.java
More file actions
30 lines (27 loc) · 1.04 KB
/
0057-insert-interval.java
File metadata and controls
30 lines (27 loc) · 1.04 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
class Solution {
public int[][] insert(int[][] intervals, int[] newInterval) {
List<int []> res = new ArrayList<>();
int start = newInterval[0];
int end = newInterval[1];
int i = 0;
//3 stages, pre overlap, overlap, and after overlap
//pre and after is easy cuz we just add them to the result
//in the overlap case, we take the min between start and the next start
//and take the max between the end and next end
while (i < intervals.length && intervals[i][1] < start) { //pre overlap
res.add(intervals[i]);
i++;
}
while (i<intervals.length && (end >= intervals[i][0])) { //overlap
start = Math.min(intervals[i][0], start);
end = Math.max(intervals[i][1], end);
i++;
}
res.add(new int[] {start, end});
while (i < intervals.length) { //after overlap
res.add(intervals[i]);
i++;
}
return res.toArray(new int[0][]);
}
}