merge_intervals
1# @leet start 2class Solution: 3 def merge(self, intervals: list[list[int]]) -> list[list[int]]: 4 """ 5 Merge intervals takes any overlapping intervals and coalesces them into one interval. 6 To do so, we first sort the intervals, and then compare the intervals pairwise. 7 We want to look at the previous interval and the current interval. 8 If the current interval's start is <= the previous end, then we know they are overlapping. 9 In such a case, pick the minimum start and the maximum end to combine the two intervals. 10 Set this as the previous interval. 11 Otherwise, add the interval since they dont have any overlap. 12 """ 13 intervals.sort() 14 res = [intervals[0]] 15 16 for curr_start, curr_end in intervals[1:]: 17 prev_start, prev_end = res[-1] 18 if curr_start <= prev_end: 19 res[-1][0] = min(prev_start, curr_start) 20 res[-1][1] = max(prev_end, curr_end) 21 else: 22 res.append([curr_start, curr_end]) 23 24 return res 25 26 27# @leet end 28sol = Solution() 29 30 31def test(): 32 assert sol.merge([[1, 3], [2, 6], [8, 10], [15, 18]]) == [[1, 6], [8, 10], [15, 18]] 33 assert sol.merge([[1, 4], [4, 5]]) == [[1, 5]]
class
Solution:
3class Solution: 4 def merge(self, intervals: list[list[int]]) -> list[list[int]]: 5 """ 6 Merge intervals takes any overlapping intervals and coalesces them into one interval. 7 To do so, we first sort the intervals, and then compare the intervals pairwise. 8 We want to look at the previous interval and the current interval. 9 If the current interval's start is <= the previous end, then we know they are overlapping. 10 In such a case, pick the minimum start and the maximum end to combine the two intervals. 11 Set this as the previous interval. 12 Otherwise, add the interval since they dont have any overlap. 13 """ 14 intervals.sort() 15 res = [intervals[0]] 16 17 for curr_start, curr_end in intervals[1:]: 18 prev_start, prev_end = res[-1] 19 if curr_start <= prev_end: 20 res[-1][0] = min(prev_start, curr_start) 21 res[-1][1] = max(prev_end, curr_end) 22 else: 23 res.append([curr_start, curr_end]) 24 25 return res
def
merge(self, intervals: list[list[int]]) -> list[list[int]]:
4 def merge(self, intervals: list[list[int]]) -> list[list[int]]: 5 """ 6 Merge intervals takes any overlapping intervals and coalesces them into one interval. 7 To do so, we first sort the intervals, and then compare the intervals pairwise. 8 We want to look at the previous interval and the current interval. 9 If the current interval's start is <= the previous end, then we know they are overlapping. 10 In such a case, pick the minimum start and the maximum end to combine the two intervals. 11 Set this as the previous interval. 12 Otherwise, add the interval since they dont have any overlap. 13 """ 14 intervals.sort() 15 res = [intervals[0]] 16 17 for curr_start, curr_end in intervals[1:]: 18 prev_start, prev_end = res[-1] 19 if curr_start <= prev_end: 20 res[-1][0] = min(prev_start, curr_start) 21 res[-1][1] = max(prev_end, curr_end) 22 else: 23 res.append([curr_start, curr_end]) 24 25 return res
Merge intervals takes any overlapping intervals and coalesces them into one interval. To do so, we first sort the intervals, and then compare the intervals pairwise. We want to look at the previous interval and the current interval. If the current interval's start is <= the previous end, then we know they are overlapping. In such a case, pick the minimum start and the maximum end to combine the two intervals. Set this as the previous interval. Otherwise, add the interval since they dont have any overlap.
sol =
<Solution object>
def
test():