-
-
Notifications
You must be signed in to change notification settings - Fork 244
[hu6r1s] WEEK 12 Solutions #1939
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
8 commits
Select commit
Hold shift + click to select a range
c305d53
feat: Solve same-tree problem
hu6r1s d649798
feat: Solve remove-nth-node-from-end-of-list problem
hu6r1s 3214067
feat: number-of-connected-components-in-an-undirected-graph problem
hu6r1s 2615f35
feat: Solve non-overlapping-intervals problem
hu6r1s d3e9117
feat: Solve meeting-rooms problem
hu6r1s 3fa23d0
feat: Solve lowest-common-ancestor-of-a-binary-search-tree problem
hu6r1s 3fa02e1
feat: Solve kth-smallest-element-in-a-bst problem
hu6r1s 880f228
feat: Solve insert-interval problem
hu6r1s 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,18 @@ | ||
| class Solution: | ||
| def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]: | ||
| intervals.append(newInterval) | ||
| intervals.sort() | ||
| output = [intervals[0]] | ||
| for x, y in intervals[1:]: | ||
| if output[-1][1] >= x: | ||
| output[-1][1] = max(y, output[-1][1]) | ||
| else: | ||
| output.append([x, y]) | ||
| return output | ||
|
|
||
|
|
||
| """ | ||
| newInterval을 집어넣은 다음 정렬을 해주면 인덱스 0에 있는 값 기준으로 정렬됨 | ||
| 대소 비교를 해서 이전 배열의 인덱스 1이 현재 배열의 인덱스 0보다 크거나 같으면 | ||
| 값을 변경해주고 아니라면 값을 그냥 추가한다. | ||
| """ |
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,25 @@ | ||
| # 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: | ||
| values = [] | ||
| def dfs(node): | ||
| if not node: | ||
| return | ||
|
|
||
| dfs(node.left) | ||
| values.append(node.val) | ||
| dfs(node.right) | ||
|
|
||
|
|
||
| dfs(root) | ||
| return values[k-1] | ||
|
|
||
| """ | ||
| 이진 탐색 트리는 왼쪽은 val보다 작고 오른쪽은 val보다 큼 | ||
| 그렇기에 중위순회 방식으로 하면 자연스럽게 정렬된 상태로 배열에 들어가게 된다. | ||
| """ |
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 @@ | ||
| # Definition for a binary tree node. | ||
| # class TreeNode: | ||
| # def __init__(self, x): | ||
| # self.val = x | ||
| # self.left = None | ||
| # self.right = None | ||
|
|
||
| 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 root.val < p.val and root.val < q.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,7 @@ | ||
| class Solution: | ||
| def canAttendMeetings(self, intervals: List[List[int]]) -> bool: | ||
| for i in range(len(intervals)): | ||
| for j in range(i + 1, len(intervals)): | ||
| if intervals[i][0] < intervals[j][1] and intervals[j][0] < intervals[i][1]: | ||
| return False | ||
| return True |
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,13 @@ | ||
| class Solution: | ||
| def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int: | ||
| intervals.sort() | ||
| cnt = 0 | ||
| pre_end = intervals[0][1] | ||
| for i in range(1, len(intervals)): | ||
| start, end = intervals[i] | ||
| if pre_end > start: | ||
| cnt += 1 | ||
| pre_end = min(end, pre_end) | ||
| else: | ||
| pre_end = end | ||
| return cnt |
43 changes: 43 additions & 0 deletions
43
number-of-connected-components-in-an-undirected-graph/hu6r1s.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,43 @@ | ||
| class Solution: | ||
| # def countComponents(self, n: int, edges: List[List[int]]) -> int: | ||
| # graph = [[] for _ in range(n)] | ||
| # for node, adj in edges: | ||
| # graph[node].append(adj) | ||
| # graph[adj].append(node) | ||
|
|
||
| # visited = set() | ||
|
|
||
| # def dfs(node): | ||
| # visited.add(node) | ||
| # for adj in graph[node]: | ||
| # if adj not in visited: | ||
| # dfs(adj) | ||
|
|
||
| # cnt = 0 | ||
| # for node in range(n): | ||
| # if node not in visited: | ||
| # cnt += 1 | ||
| # dfs(node) | ||
| # return cnt | ||
|
|
||
|
|
||
| def countComponents(self, n: int, edges: List[List[int]]) -> int: | ||
| graph = [[] for _ in range(n)] | ||
| for node, adj in edges: | ||
| graph[node].append(adj) | ||
| graph[adj].append(node) | ||
|
|
||
| cnt = 0 | ||
| visited = set() | ||
| for node in range(n): | ||
| if node in visited: | ||
| continue | ||
| cnt += 1 | ||
| queue = deque([node]) | ||
| while queue: | ||
| node = queue.pop() | ||
| visited.add(node) | ||
| for adj in graph[node]: | ||
| if adj not in visited: | ||
| queue.append(adj) | ||
| return 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,20 @@ | ||
| # Definition for singly-linked list. | ||
| # class ListNode: | ||
| # def __init__(self, val=0, next=None): | ||
| # self.val = val | ||
| # self.next = next | ||
| class Solution: | ||
| def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]: | ||
| length = 0 | ||
| node = head | ||
| while node: | ||
| length += 1 | ||
| node = node.next | ||
|
|
||
| dummy = ListNode(None, head) | ||
| node = dummy | ||
| for _ in range(length - n): | ||
| node = node.next | ||
|
|
||
| node.next = node.next.next | ||
| return dummy.next |
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,15 @@ | ||
| # 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 isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool: | ||
| if not p and not q: | ||
| return True | ||
| if not p or not q: | ||
| return False | ||
| if p.val != q.val: | ||
| return False | ||
| return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right) | ||
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.
2번째 if문과 3번째 if문을 합쳐도 될 거 같아 보이는데, 이렇게는 안 되나요?
물론 가독성은 분리했을 때 더 나은 거 같긴 해요.