You are given an array of time intervals (start, end) for meetings. Merge the overlapping intervals and return an array of non-overlapping intervals representing the consolidated meeting times. For example:
[[1,3],[2,6],[8,10],[15,18]]
, the expected output is [[1,6],[8,10],[15,18]]
. The intervals [1,3]
and [2,6]
overlap and are merged into [1,6]
.[[1,4],[4,5]]
, the expected output is [[1,5]]
. The intervals [1,4]
and [4,5]
overlap and are merged into [1,5]
.[[1,4],[0,4]]
, the expected output is [[0,4]]
. The intervals [1,4]
and [0,4]
overlap and are merged into [0,4]
.[[1,4],[0,2],[3,5]]
, the expected output is [[0,5]]
. The intervals [1,4]
and [0,2]
overlap and are merged into [0,4]
. Then [0,4]
and [3,5]
are merged into [0,5]
.Explain your approach. Write a function that takes a list of intervals and returns a list of merged intervals. What is the time and space complexity of your solution?
This problem involves merging overlapping intervals in a list. I'll start by explaining the approach, then provide a Python function to solve it, and finally analyze the time and space complexity.
def merge_intervals(intervals):
if not intervals:
return []
# Sort the intervals based on the start time
intervals.sort(key=lambda x: x[0])
merged = []
for interval in intervals:
# If the merged list is empty or the current interval does not overlap
# with the last interval in the merged list, append it
if not merged or interval[0] > merged[-1][1]:
merged.append(interval)
else:
# Otherwise, there is overlap, so we merge the current interval
# with the last interval in the merged list
merged[-1][1] = max(merged[-1][1], interval[1])
return merged
# Example Usage:
intervals1 = [[1,3],[2,6],[8,10],[15,18]]
print(f"Input: {intervals1}, Merged: {merge_intervals(intervals1)}")
intervals2 = [[1,4],[4,5]]
print(f"Input: {intervals2}, Merged: {merge_intervals(intervals2)}")
intervals3 = [[1,4],[0,4]]
print(f"Input: {intervals3}, Merged: {merge_intervals(intervals3)}")
intervals4 = [[1,4],[0,2],[3,5]]
print(f"Input: {intervals4}, Merged: {merge_intervals(intervals4)}")
sort()
method uses Timsort, which has a space complexity of O(n) in the worst case.