-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path56 Merge Intervals.js
51 lines (43 loc) · 1.04 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
44
45
46
47
48
49
50
51
/**
* Given a collection of intervals, merge all overlapping intervals.
* 给出一个区间的集合,请合并所有重叠的区间。
*/
/**
* Example:
* Input: [[1,3],[2,6],[8,10],[15,18]]
* Output: [[1,6],[8,10],[15,18]]
* Explanation: Since intervals [1,3] and [2,6]
* overlaps, merge them into [1,6].
*/
/**
* Definition for an interval.
* function Interval(start, end) {
* this.start = start;
* this.end = end;
* }
*/
/**
* @param {Interval[]} intervals
* @return {Interval[]}
*/
const merge = (intervals) => {
let res = []
intervals.sort((i1, i2) => (i1.start > i2.start ? 1 : -1))
console.log(intervals)
if (intervals.length) {
res.push(intervals[0])
}
for (let i = 1; i < intervals.length; i++) {
let interval = intervals[i]
let last = res.pop()
if (interval.start > last.end) {
res.push(last)
res.push(interval)
} else {
last.end = Math.max(last.end, interval.end)
res.push(last)
}
}
return res
}
console.log(merge([[1, 3], [2, 6], [8, 10], [15, 18]]))