← Back to companies
[ OK ] Loaded —
[ INFO ]
$ cd
$ ls -lt
01
02
03
04
05
$ ls -lt
01
02
03
04
05
user@intervues:~/$
Question: Given an array of intervals where intervals[i] = [start_i, end_i], merge all overlapping intervals, and return the merged intervals.
Examples:
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].
Input: intervals = [[1,4],[4,5]] Output: [[1,5]] Explanation: Since intervals [1,4] and [4,5] overlap, merge them into [1,5].
Constraints:
Hints:
Solution: `python class Solution: def merge(self, intervals): if not intervals: return []
# Sort intervals based on the start time
intervals.sort(key=lambda x: x[0])
merged = [intervals[0]]
for current in intervals[1:]:
prev = merged[-1]
# Check if there is an overlap
if current[0] <= prev[1]:
# Merge the current interval with the previous one
merged[-1] = [prev[0], max(prev[1], current[1])]
else:
# No overlap, just add the current interval
merged.append(current)
return merged
`
Explanation:
merged with the first interval.merged.merged.merged.Source: