-
-
Notifications
You must be signed in to change notification settings - Fork 195
[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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3ac5e06
solve : insert interval
SamTheKorean 5ea3072
solve : merge interval
SamTheKorean 8d2e6dc
solve : non overlapping intervals
SamTheKorean fffbaa9
solve : meeting rooms ii
SamTheKorean 2e7229f
fix : add time and space complexity
SamTheKorean d24538b
solve : rotate image
SamTheKorean File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# TC : O(n log n) | ||
# SC : O(n) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ด ๋ฌธ์ ๋ ์๊ฐ๋ณต์ก๋์ ๊ณต๊ฐ๋ณต์ก๋ ํ๊ธฐ ๋ถํ๋๋ฆฌ๊ฒ ์ต๋๋ค : )