Merge Intervals
Clarify the problem:
- The problem requires us to merge overlapping intervals in a given list of intervals.
- We need to implement a function that takes in a list of intervals and returns a new list of merged intervals.
- An interval [a, b] is considered to be overlapping if there exists another interval [c, d] where a <= c <= b or c <= a <= d.
- The intervals in the input list may not be sorted, and the output should contain the merged intervals in sorted order.
Analyze the problem:
- Input: List of intervals represented as pairs of integers.
- Output: List of merged intervals.
- Constraints:
- The number of intervals in the input list is between 1 and 10^4.
- Each interval consists of two integers, where the start value is less than or equal to the end value.
Design an algorithm:
- We can solve this problem by sorting the intervals based on their start values.
- We initialize an empty result list to store the merged intervals.
- We iterate through the sorted intervals and compare each interval with the previous interval.
- If the current interval overlaps with the previous one, we merge them by updating the end value of the previous interval.
- If the current interval does not overlap, we add the previous interval to the result list and update the previous interval with the current interval.
- Finally, we add the last interval to the result list.
- The result list will contain the merged intervals.
Explain your approach:
- We will implement an algorithm that sorts and merges the intervals to find the merged intervals.
- First, we sort the intervals based on their start values.
- Then, we initialize an empty list called
merged_intervals
to store the merged intervals. - We iterate through the sorted intervals and compare each interval with the previous interval.
- If the current interval overlaps with the previous one, we merge them by updating the end value of the previous interval.
- If the current interval does not overlap, we add the previous interval to the
merged_intervals
list and update the previous interval with the current interval. - Finally, we add the last interval to the
merged_intervals
list. - The
merged_intervals
list will contain the merged intervals.
Write clean and readable code:
pythonclass Solution: def merge(self, intervals): intervals.sort(key=lambda x: x[0]) merged_intervals = [] prev_interval = intervals[0] for i in range(1, len(intervals)): current_interval = intervals[i] if current_interval[0] <= prev_interval[1]: prev_interval[1] = max(prev_interval[1], current_interval[1]) else: merged_intervals.append(prev_interval) prev_interval = current_interval merged_intervals.append(prev_interval) return merged_intervals
Test your code:
pythonsolution = Solution() intervals = [[1, 3], [2, 6], [8, 10], [15, 18]] merged = solution.merge(intervals) print(merged) # Output: [[1, 6], [8, 10], [15, 18]] intervals = [[1, 4], [4, 5]] merged = solution.merge(intervals) print(merged) # Output: [[1, 5]] intervals = [[1, 4], [0, 4]] merged = solution.merge(intervals) print(merged) # Output: [[0, 4]] intervals = [[1, 4], [0, 0]] merged = solution.merge(intervals) print(merged) # Output: [[0, 0], [1, 4]]
Optimize if necessary:
- The provided solution has a time complexity of O(n log n), where n is the number of intervals.
- The space complexity is O(n) to store the merged intervals.
- This solution is efficient and optimal for the given problem.
Handle error cases:
- The code assumes that the input for the
merge
function is valid. - If the input list is empty, the function will return an empty list.
- The code assumes that the input for the
Discuss complexity analysis:
- The time complexity of the solution is O(n log n), where n is the number of intervals. This is due to the sorting operation.
- The space complexity is O(n) to store the merged intervals.
- The solution is efficient and provides the correct output for the given problem.