-
-
Notifications
You must be signed in to change notification settings - Fork 305
[Seoya0512] WEEK 13 solutions #2322
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
+86
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 @@ | ||
| ''' | ||
| Time Complexity : O(N) | ||
| - newInterval을 삽입할 위치 탐색 O(N) | ||
| - Intervals 병합을 위해 for문으로 모든 구간을 순회 O(N) | ||
| - O(N) + O(N) = O(N) | ||
|
|
||
| Space Complexity : O(N) | ||
| - output 리스트에 모든 구간을 저장 O(N) | ||
|
|
||
| ''' | ||
| class Solution: | ||
| def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]: | ||
| idx = 0 | ||
| while idx < len(intervals) and intervals[idx][0] < newInterval[0]: | ||
| idx +=1 | ||
| intervals.insert(idx, newInterval) # newInterval을 삽입할 위치 탐색 (O(N)) | ||
|
|
||
| # Intervals 병합 | ||
| output = [intervals[0]] | ||
| for interval in intervals[1:]: | ||
| # 이전 구간과 겹치는 경우 | ||
| if output[-1][1] >= interval[0]: | ||
| output[-1][1] = max(output[-1][1], interval[1]) | ||
| else: | ||
| output.append(interval) | ||
| return output | ||
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,29 @@ | ||
| ''' | ||
| Time Complexity: O(N) | ||
| - dfs 함수가 모든 노드를 중위순회 방식으로 방문함므로 O(N) 소요 | ||
|
|
||
| Space Complexity: O(N) | ||
| - aligned_arr 리스트에 모든 노드의 값을 저장하므로 O(N) 소요 | ||
| - 재귀 호출 스택이 최대 트리의 높이만큼 쌓일 수 있으므로 최악의 경우 O(N) 소요 | ||
| ''' | ||
|
|
||
| # Definition for a binary tree node. | ||
| # class TreeNode: | ||
| # def __init__(self, val=0, left=None, right=None): | ||
| # self.val = val | ||
| # self.left = left | ||
| # self.right = right | ||
| class Solution: | ||
| def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: | ||
| aligned_arr = [] | ||
|
|
||
| def dfs(node): | ||
| if not node: | ||
| return | ||
| dfs(node.left) | ||
| aligned_arr.append(node.val) | ||
| dfs(node.right) | ||
|
|
||
| dfs(root) | ||
|
|
||
| return aligned_arr[k - 1] |
12 changes: 12 additions & 0 deletions
12
lowest-common-ancestor-of-a-binary-search-tree/Seoya0512.py
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,12 @@ | ||
| ''' | ||
| Time Complexity: O(H) | ||
| - H는 트리의 높이 | ||
| ''' | ||
|
|
||
| class Solution: | ||
| def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': | ||
| if p.val < root.val and q.val < root.val: | ||
| return self.lowestCommonAncestor(root.left, p, q) | ||
| if p.val > root.val and q.val > root.val: | ||
| return self.lowestCommonAncestor(root.right, p, q) | ||
| return root |
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 @@ | ||
| ''' | ||
| Time Complexity: O(N log N) | ||
| - intervals.sort() 는 O(N log N) 소요 | ||
| - for loop 는 O(N) 소요 | ||
| - 전체적으로 O(N log N) + O(N) = O(N log N) | ||
|
|
||
| Space Complexity: O(1) | ||
| - end_time, start_time 상수 변수 추가로 사용 | ||
| ''' | ||
| def can_attend_meetings(self, intervals: List[Interval]) -> bool: | ||
| intervals.sort() | ||
| end_time = intervals[0][1] | ||
|
|
||
| for i in range(1, len(intervals)): | ||
| start_time = intervals[i][0] | ||
| if end_time > start_time: | ||
| return False | ||
| end_time = intervals[i][1] | ||
| return True |
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.
저는 분기를 이용해 앞/겹침/뒤 케이스를 처리했는데, 삽입 위치를 index 로 처리하신 점이 흥미로웠습니다.