-
Notifications
You must be signed in to change notification settings - Fork 10
/
56_merge-intervals.js
63 lines (59 loc) · 1.48 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
52
53
54
55
56
57
58
59
60
61
62
63
/**
*
* Problem:
* Given a list of intervals, merge all the overlapping intervals to produce a list
* that has only mutually exclusive intervals.
* https://leetcode.com/problems/merge-intervals/
*
* Example 1:
* Intervals: [[1,4], [2,5], [7,9]]
* Output: [[1,5], [7,9]]
* Explanation: Since the first two intervals [1,4] and [2,5] overlap, we merged them
* into one [1,5].
*
* Example 2:
* Intervals: [[6,7], [2,4], [5,9]]
* Output: [[2,4], [5,9]]
* Explanation: Since the intervals [6,7] and [5,9] overlap, we merged them into one
* [5,9].
*
* Time: O(nlog(n))
* Space: O(n) <- sorting
*
* @param {number[][]} intervals
* @return {number[][]}
*/
function mergeIntervals(intervals) {
if (intervals.length === 0) {
return [];
}
// sort the intervals by their start O(nlog(n))
intervals.sort((a, b) => a[0] - b[0]);
const result = [intervals[0]];
// O(n)
for (let i = 1; i < intervals.length; i++) {
const lastInterval = result[result.length - 1];
const newInterval = intervals[i];
if (newInterval[0] <= lastInterval[1]) {
// overlap
lastInterval[1] = Math.max(lastInterval[1], newInterval[1]);
} else {
// do not overlap
result.push(newInterval);
}
}
return result;
}
// Test
const result1 = mergeIntervals([
[1, 4],
[2, 5],
[7, 9],
]);
result1.forEach((i) => console.log(i)); // [[1,5], [7,9]]
const result2 = mergeIntervals([
[6, 7],
[2, 4],
[5, 9],
]);
result2.forEach((i) => console.log(i)); // [[2,4], [5,9]]