-
Notifications
You must be signed in to change notification settings - Fork 0
/
leetcode56.js
35 lines (33 loc) · 914 Bytes
/
leetcode56.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
/**
* @param {number[][]} intervals
* @return {number[][]}
* https://leetcode.com/problems/merge-intervals/
* https://leetcode.com/submissions/detail/545772358/
*/
var merge = function (intervals) {
intervals.sort((a, b) => {
return a[0] - b[0];
});
let result = intervals.reduce((prev, curr) => {
if (prev.length === 0) {
prev.push(curr);
return prev;
} else {
//the current value: prev[prev.length - 1]
let currentPair = prev[prev.length - 1];
//when range has no overlapping
if (currentPair[1] < curr[0]) {
prev.push(curr);
return prev;
} else {
// there is overlapping
prev[prev.length - 1] = [
Math.min(currentPair[0], currentPair[1], curr[0], curr[1]),
Math.max(currentPair[0], currentPair[1], curr[0], curr[1]),
];
return prev;
}
}
}, []);
return result;
};