Skip to content

Commit

Permalink
[Leo] 13th Week solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
leokim0922 committed Jul 26, 2024
1 parent 3ca64ba commit 475c43c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
17 changes: 17 additions & 0 deletions insert-interval/Leo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution:
def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
left = []
right = []

for interval in intervals:
if interval[1] < newInterval[0]:
left.append(interval)
elif interval[0] > newInterval[1]:
right.append(interval)
else:
newInterval[0] = min(newInterval[0], interval[0])
newInterval[1] = max(newInterval[1], interval[1])

return left + [newInterval] + right

## TC: O(n), SC: O(n)
20 changes: 20 additions & 0 deletions merge-intervals/Leo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
def sort_key(interval):
return interval[0]

intervals.sort(key=sort_key)

result = [intervals[0]]

for start, end in intervals[1:]:
lastEnd = result[-1][1]

if start <= lastEnd:
result[-1][1] = max(lastEnd, end)
else:
result.append([start, end])

return result

## TC: O(nlogn), SC: O(n)

0 comments on commit 475c43c

Please sign in to comment.