Skip to content

Commit

Permalink
solve : insert interval
Browse files Browse the repository at this point in the history
  • Loading branch information
SamTheKorean committed Jul 24, 2024
1 parent 3ca64ba commit 3ac5e06
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions insert-interval/samthekorean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# TC : O(n + m)
# SC : O(n + m)
# n is a size of intervals and m is a size of newInterval
class Solution:
def insert(
self, intervals: List[List[int]], newInterval: List[int]
) -> List[List[int]]:
result = []
i = 0

while i < len(intervals) and intervals[i][1] < newInterval[0]:
result.append(intervals[i])
i += 1

while i < len(intervals) and intervals[i][0] <= newInterval[1]:
newInterval[0] = min(newInterval[0], intervals[i][0])
newInterval[1] = max(newInterval[1], intervals[i][1])
i += 1

result.append(newInterval)

while i < len(intervals):
result.append(intervals[i])
i += 1

return result

0 comments on commit 3ac5e06

Please sign in to comment.