Skip to content

[SAM] Week 13 solutions #199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 14 additions & 0 deletions meeting-rooms-ii/samthekorean.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด ๋ฌธ์ œ๋„ ์‹œ๊ฐ„๋ณต์žก๋„์™€ ๊ณต๊ฐ„๋ณต์žก๋„ ํ‘œ๊ธฐ ๋ถ€ํƒ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค : )

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# TS : O(n log n)
# SC : O(n)
class Solution:
def minMeetingRooms(self, intervals: List[List[int]]) -> int:
pairs = []
for start, end in intervals:
pairs += [(start, 1), (end, -1)]
pairs.sort()

max_cnt, cnt = 0, 0
for pair in pairs:
cnt += pair[1]
max_cnt = max(cnt, max_cnt)
return max_cnt
19 changes: 19 additions & 0 deletions merge-intervals/samthekorean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# TC : O(nlog n)
# SC : O(n)
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
merged = []
intervals.sort(key=lambda x: x[0])

prev = intervals[0]

for interval in intervals[1:]:
if interval[0] <= prev[1]:
prev[1] = max(prev[1], interval[1])
else:
merged.append(prev)
prev = interval

merged.append(prev)

return merged
16 changes: 16 additions & 0 deletions non-overlapping-intervals/samthekorean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# TC : O(n log n)
# SC : O(n)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ํ˜น์‹œ O(n)์ด ์–ด๋–ป๊ฒŒ ๋‚˜์™”์„๊นŒ์š”? O(1) ์ธ ๊ฒƒ ๊ฐ™์•„์„œ์š”!

class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
res = 0

intervals.sort(key=lambda x: x[1])
prev_end = intervals[0][1]

for i in range(1, len(intervals)):
if prev_end > intervals[i][0]:
res += 1
else:
prev_end = intervals[i][1]

return res
16 changes: 16 additions & 0 deletions rotate-image/samthekorean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# TC : O(n^2)
# SC : O(1)
class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
top, bottom = 0, len(matrix) - 1

while top < bottom:
left, right = top, bottom
for i in range(bottom - top):
topLeft = matrix[top][left + i]
matrix[top][left + i] = matrix[bottom - i][left]
matrix[bottom - i][left] = matrix[bottom][right - i]
matrix[bottom][right - i] = matrix[top + i][right]
matrix[top + i][right] = topLeft

top, bottom = top + 1, bottom - 1