-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpt_solution.js
29 lines (24 loc) · 928 Bytes
/
gpt_solution.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
const merge = intervals => {
// Step 1: Sort intervals based on start time
intervals.sort((a, b) => a[0] - b[0]);
// Step 2: Initialize result array
const mergedIntervals = [];
// Step 3: Iterate over intervals
for (let i = 0; i < intervals.length; i++) {
const currInterval = intervals[i];
// Step 4: If no overlap, add to result array
if (mergedIntervals.length === 0 || mergedIntervals[mergedIntervals.length - 1][1] < currInterval[0]) {
mergedIntervals.push(currInterval);
}
// Step 5: If overlap, update end time of previous merged interval
else {
mergedIntervals[mergedIntervals.length - 1][1] = Math.max(mergedIntervals[mergedIntervals.length - 1][1], currInterval[1]);
}
}
// Step 6: Return result array
return mergedIntervals;
};
// Example usage
const intervals = [[1, 3], [2, 6], [8, 10], [15, 18]];
const merged = merge(intervals);
console.log(merged);