File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution :
2+ def insert (self , intervals : List [List [int ]], newInterval : List [int ]) -> List [List [int ]]:
3+ left = []
4+ right = []
5+
6+ for interval in intervals :
7+ if interval [1 ] < newInterval [0 ]:
8+ left .append (interval )
9+ elif interval [0 ] > newInterval [1 ]:
10+ right .append (interval )
11+ else :
12+ newInterval [0 ] = min (newInterval [0 ], interval [0 ])
13+ newInterval [1 ] = max (newInterval [1 ], interval [1 ])
14+
15+ return left + [newInterval ] + right
16+
17+ ## TC: O(n), SC: O(n)
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def merge (self , intervals : List [List [int ]]) -> List [List [int ]]:
3+ def sort_key (interval ):
4+ return interval [0 ]
5+
6+ intervals .sort (key = sort_key )
7+
8+ result = [intervals [0 ]]
9+
10+ for start , end in intervals [1 :]:
11+ lastEnd = result [- 1 ][1 ]
12+
13+ if start <= lastEnd :
14+ result [- 1 ][1 ] = max (lastEnd , end )
15+ else :
16+ result .append ([start , end ])
17+
18+ return result
19+
20+ ## TC: O(nlogn), SC: O(n)
You can’t perform that action at this time.
0 commit comments