forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhsskey.js
More file actions
25 lines (23 loc) · 681 Bytes
/
hsskey.js
File metadata and controls
25 lines (23 loc) · 681 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
/**
* @param {number[][]} intervals
* @param {number[]} newInterval
* @return {number[][]}
*/
var insert = function(intervals, newInterval) {
const res = [];
for (let i = 0; i < intervals.length; i++) {
if (newInterval[1] < intervals[i][0]) {
res.push(newInterval);
return res.concat(intervals.slice(i));
} else if (newInterval[0] > intervals[i][1]) {
res.push(intervals[i]);
} else {
newInterval = [
Math.min(newInterval[0], intervals[i][0]),
Math.max(newInterval[1], intervals[i][1])
];
}
}
res.push(newInterval);
return res;
};