-
Notifications
You must be signed in to change notification settings - Fork 70
/
res.js
39 lines (34 loc) · 797 Bytes
/
res.js
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
/**
* @param {number[][]} intervals
* @param {number[]} newInterval
* @return {number[][]}
*/
var insert = function(intervals, newInterval) {
const len = intervals.length;
if (!len) return [newInterval];
const [start, end] = newInterval;
const new_itervals = [];
let i = 0;
while (i < len && start > intervals[i][0]) {
new_itervals.push(intervals[i]);
i++;
}
new_itervals.push(newInterval);
while (i < len) {
new_itervals.push(intervals[i]);
i++;
}
console.log(new_itervals);
res = [];
i = 0;
while (i < len+1) {
let [left, right] = new_itervals[i];
while (i < len && new_itervals[i+1][0] <= right) {
i += 1;
right = Math.max(new_itervals[i][1], right);
}
res.push([left, right]);
i += 1;
}
return res;
};