-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_intervals.js
35 lines (32 loc) · 939 Bytes
/
merge_intervals.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
// https://leetcode.com/problems/merge-intervals/description/
// 56. Merge Intervals
/**
* @param {number[][]} intervals
* @return {number[][]}
*/
var merge = function(intervals) {
intervals.sort((a, b) => a[0] - b[0]);
let output = [];
if (intervals.length > 0) output.push(intervals[0])
for (let i = 1; i < intervals.length; i++) {
const e = intervals[i];
const last_ind = output.length -1;
let [l, r] = output[last_ind];
if (e[0] <= r && e[1] >= l) {
output[last_ind] = [Math.min(e[0], l), Math.max(e[1], r)];
} else {
output.push(e);
}
}
return output;
};
console.log(merge([[1,3],[2,6],[8,10],[15,18]]))
// Output: [[1,6],[8,10],[15,18]]
console.log(merge([[1,3],[15,18],[2,6],[8,10]]))
// Output: [[1,6],[8,10],[15,18]]
console.log(merge([[1,4],[4,5]]))
// Output: [[1,5]]
console.log(merge([]))
// Output: []
console.log(merge([[-11,40],[1,4],[4,5]]))
// Output: [[-11,40]]