-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergeInterval.js
More file actions
34 lines (28 loc) · 963 Bytes
/
mergeInterval.js
File metadata and controls
34 lines (28 loc) · 963 Bytes
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
/*
56. Merge Intervals
Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.
Example 1:
Input: intervals = [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
Explanation: Since intervals [1,3] and [2,6] overlap, merge them into [1,6].
*/
let interval = [[1,3],[2,6],[8,10],[15,18]]
function merge(interval){
//Sort the interval
interval.sort((a,b) => a[0]-b[0])
//Initialize the interval
let merged = [interval[0]]
//Iterate through the interval
for(let i=1; i<interval.length; i++){
let lastMerged = merge(merge.length-1)
let current = interval[i]
if(current[0] <= lastMerged[1]){
lastMerged = Math.max(lastMerged[1], current[1])
}
else {
merged.push(interval)
}
}
return merged
}
console.log(merge())