-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path0057-insert-interval.js
43 lines (40 loc) · 1.3 KB
/
0057-insert-interval.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
40
41
42
43
/**
* 57. Insert Interval
* https://leetcode.com/problems/insert-interval/
* Difficulty: Medium
*
* You are given an array of non-overlapping intervals intervals where
* intervals[i] = [starti, endi] represent the start and the end of the
* ith interval and intervals is sorted in ascending order by starti.
* You are also given an interval newInterval = [start, end] that
* represents the start and end of another interval.
*
* Insert newInterval into intervals such that intervals is still sorted
* in ascending order by starti and intervals still does not have any
* overlapping intervals (merge overlapping intervals if necessary).
*
* Return intervals after the insertion.
*/
/**
* @param {number[][]} intervals
* @param {number[]} newInterval
* @return {number[][]}
*/
var insert = function(intervals, newInterval) {
const result = [];
for (let i = 0; i < intervals.length; i++) {
if (newInterval[1] < intervals[i][0]) {
result.push(newInterval);
return [...result, ...intervals.slice(i)];
} else if (newInterval[0] > intervals[i][1]) {
result.push(intervals[i]);
} else {
newInterval = [
Math.min(newInterval[0], intervals[i][0]),
Math.max(newInterval[1], intervals[i][1]),
];
}
}
result.push(newInterval);
return result;
};