-
Notifications
You must be signed in to change notification settings - Fork 0
/
56-merge-intervals.js
43 lines (42 loc) · 1.08 KB
/
56-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
36
37
38
39
40
41
42
43
/**
* @param {number[][]} intervals
* @return {number[][]}
*/
// var merge = function (intervals) {
// let result = [];
// intervals.sort((a, b) => a[0] - b[0]);
// for (let i = 0; i < intervals.length - 1; i++) {
// if (intervals[i][1] < intervals[i + 1][0]) {
// result.push(intervals[i]);
// continue;
// }
// if (intervals[i][1] >= intervals[i + 1][0]) {
// intervals[i + 1][0] = intervals[i][0];
// }
// if (intervals[i][1] > intervals[i + 1][1]) {
// intervals[i + 1] = intervals[i];
// }
// }
// result.push(intervals[intervals.length - 1]);
// return result;
// };
var merge = function (intervals) {
intervals.sort((a, b) => a[0] - b[0]);
let result = [intervals[0]];
for (let i = 1; i < intervals.length; i++) {
if (result[result.length - 1][1] >= intervals[i][0]) {
result[result.length - 1][1] = Math.max(result[result.length - 1][1], intervals[i][1]);
} else {
result.push(intervals[i]);
}
}
return result;
};
console.error(
merge([
[1, 3],
[2, 6],
[8, 10],
[15, 18],
])
);