forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbky373.java
More file actions
29 lines (24 loc) · 762 Bytes
/
bky373.java
File metadata and controls
29 lines (24 loc) · 762 Bytes
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
// time: O(N)
// space: O(1)
class Solution {
public int[][] insert(int[][] intervals, int[] newInterval) {
int n = intervals.length;
int i = 0;
List<int[]> result = new ArrayList<>();
while (i < n && intervals[i][1] < newInterval[0]) {
result.add(intervals[i]);
i++;
}
while (i < n && 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);
while (i < n) {
result.add(intervals[i]);
i++;
}
return result.toArray(new int[result.size()][]);
}
}