insert_interval
1# @leet start 2class Solution: 3 def insert( 4 self, intervals: list[list[int]], newInterval: list[int] 5 ) -> list[list[int]]: 6 """ 7 This question asks us to insert an interval that's unsorted into an array 8 of intervals. 9 We can do what the problem says and treat it as merge intervals. 10 """ 11 if not intervals: 12 return [newInterval] 13 intervals.append(newInterval) 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 prev_end >= curr_start: 20 res[-1] = [prev_start, max(prev_end, curr_end)] 21 else: 22 res.append([curr_start, curr_end]) 23 24 return res 25 26 27# @leet end 28 29 30def test(): 31 assert 2 + 2 == 4
class
Solution:
3class Solution: 4 def insert( 5 self, intervals: list[list[int]], newInterval: list[int] 6 ) -> list[list[int]]: 7 """ 8 This question asks us to insert an interval that's unsorted into an array 9 of intervals. 10 We can do what the problem says and treat it as merge intervals. 11 """ 12 if not intervals: 13 return [newInterval] 14 intervals.append(newInterval) 15 intervals.sort() 16 res = [intervals[0]] 17 18 for curr_start, curr_end in intervals[1:]: 19 prev_start, prev_end = res[-1] 20 if prev_end >= curr_start: 21 res[-1] = [prev_start, max(prev_end, curr_end)] 22 else: 23 res.append([curr_start, curr_end]) 24 25 return res
def
insert( self, intervals: list[list[int]], newInterval: list[int]) -> list[list[int]]:
4 def insert( 5 self, intervals: list[list[int]], newInterval: list[int] 6 ) -> list[list[int]]: 7 """ 8 This question asks us to insert an interval that's unsorted into an array 9 of intervals. 10 We can do what the problem says and treat it as merge intervals. 11 """ 12 if not intervals: 13 return [newInterval] 14 intervals.append(newInterval) 15 intervals.sort() 16 res = [intervals[0]] 17 18 for curr_start, curr_end in intervals[1:]: 19 prev_start, prev_end = res[-1] 20 if prev_end >= curr_start: 21 res[-1] = [prev_start, max(prev_end, curr_end)] 22 else: 23 res.append([curr_start, curr_end]) 24 25 return res
This question asks us to insert an interval that's unsorted into an array of intervals. We can do what the problem says and treat it as merge intervals.
def
test():