We sort the intervals by their starting values. Then we iterate through the sorted list and check if the current interval overlaps with the last merged interval in our results. If it overlaps, we update the end of the last merged interval. If it doesn't, we add it as a new interval.
- Sort
intervalsbased on the first element of each interval. - Initialize
merged = []. - Iterate through each
intervalin the sorted list. - If
mergedis empty or the last interval inmerged's end is less than the currentinterval's start:- Add the current
intervaltomerged.
- Add the current
- Else (they overlap):
- Update the end of the last interval in
mergedto be the maximum of its own end and the currentinterval's end.
- Update the end of the last interval in
- Return
merged.
- Time Complexity: O(N log N) due to sorting, where N is the number of intervals.
- Space Complexity: O(N) to store the result, or O(log N) for sorting space.
def merge_intervals(intervals):
if not intervals:
return []
# Sort intervals by start time
intervals.sort(key=lambda x: x[0])
merged = [intervals[0]]
for i in range(1, len(intervals)):
last_start, last_end = merged[-1]
curr_start, curr_end = intervals[i]
# If the current interval overlaps with the last merged interval
if curr_start <= last_end:
# Update the end of the last merged interval
merged[-1][1] = max(last_end, curr_end)
else:
# Otherwise, append the current interval
merged.append([curr_start, curr_end])
return merged