forked from varunu28/LeetCode-Java-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsert Interval.java
More file actions
49 lines (48 loc) · 1.31 KB
/
Insert Interval.java
File metadata and controls
49 lines (48 loc) · 1.31 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
class Solution {
public int[][] insert(int[][] intervals, int[] newInterval) {
List<int[]> list = new ArrayList<>();
boolean merged = false;
int idx = 0;
int n = intervals.length;
while (idx < n) {
if (merged) {
list.add(intervals[idx++]);
continue;
}
if (isOverlap(intervals[idx], newInterval)) {
int start = Math.min(intervals[idx][0], newInterval[0]);
int currEnd = Math.max(intervals[idx][1], newInterval[1]);
idx++;
while (idx < n && intervals[idx][0] <= currEnd) {
currEnd = Math.max(currEnd, intervals[idx][1]);
idx++;
}
list.add(new int[]{start, currEnd});
merged = true;
}
else if (intervals[idx][0] > newInterval[1]) {
list.add(newInterval);
list.add(intervals[idx++]);
merged = true;
}
else {
list.add(intervals[idx++]);
}
}
if (!merged) {
list.add(newInterval);
}
int[][] ans = new int[list.size()][2];
for (int i = 0; i < list.size(); i++) {
ans[i] = list.get(i);
}
return ans;
}
private boolean isOverlap(int[] i1, int[] i2) {
return (
(i2[0] <= i1[1] && i2[0] >= i1[0]) ||
(i2[1] >= i1[0] && i2[0] <= i1[0]) ||
(i2[0] >= i1[0] && i2[1] <= i1[1])
);
}
}