Medium
Given an array of intervals
where intervals[i] = [starti, endi]
, merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.
Example 1:
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] overlaps, merge them into [1,6].
Example 2:
Input: intervals = [[1,4],[4,5]]
Output: [[1,5]]
Explanation: Intervals [1,4] and [4,5] are considered overlapping.
Constraints:
1 <= intervals.length <= 104
intervals[i].length == 2
0 <= starti <= endi <= 104
Here are the steps to solve the task using the Solution
class:
Solution
class with a method named merge
that takes in a list of intervals.merge
method, sort the intervals based on their start times.merged_intervals
to store the merged intervals.merged_intervals
list is empty or if the current interval’s start time is greater than the end time of the last interval in the merged_intervals
list, append the current interval to merged_intervals
.merged_intervals
list by updating the end time of the last interval if the current interval’s end time is greater.merged_intervals
list.Here’s the implementation:
from typing import List
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
intervals.sort(key=lambda x: x[0])
merged_intervals = []
for interval in intervals:
if not merged_intervals or interval[0] > merged_intervals[-1][1]:
merged_intervals.append(interval)
else:
merged_intervals[-1][1] = max(merged_intervals[-1][1], interval[1])
return merged_intervals
# Example usage:
intervals1 = \[\[1, 3], [2, 6], [8, 10], [15, 18]]
intervals2 = \[\[1, 4], [4, 5]]
sol = Solution()
print(sol.merge(intervals1)) # Output: [[1, 6], [8, 10], [15, 18]]
print(sol.merge(intervals2)) # Output: [[1, 5]]