-
-
Notifications
You must be signed in to change notification settings - Fork 305
[doh6077] WEEK 12 solutions #2303
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
+96
−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,47 @@ | ||
| class Solution: | ||
| def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int: | ||
|
|
||
| # # first structure it as graph (dictionary) | ||
| # intervals = [[1,2],[2,3],[3,4],[1,3]] | ||
| # dict = {} | ||
| # for i in intervals: | ||
| # dict[i[0]] = [] | ||
| # for a,b in intervals: | ||
| # dict[a].append(b) | ||
|
|
||
| # #Overlapping edge cases | ||
| # # 1. [1,4], [2,3] -> not sure how to figure out | ||
| # # 2. [1,3], [1,2] , [3,4] -> check if the value has connection | ||
| # # 3. [1,2], [1,2], [1,2] ( same values) use hashset | ||
|
|
||
| # count = 0 | ||
| # visited = set() | ||
| # for key, value in dict.items(): | ||
| # if value not in visited: | ||
| # visited.append(value) | ||
| # else: | ||
| # count += 1 | ||
| # return count | ||
|
|
||
| # count = 0 | ||
| # visited = set() | ||
| # for a, b in intervals: | ||
| # if b not in visited: | ||
| # visited.add(b) | ||
| # else: | ||
| # count += 1 | ||
| # return count | ||
|
|
||
|
|
||
| # greedy Approach | ||
| intervals.sort() | ||
|
|
||
| res = 0 | ||
| prevEnd = intervals[0][1] | ||
| for start, end in intervals[1:]: | ||
| if start >= prevEnd: | ||
| prevEnd = end | ||
| else: | ||
| res += 1 | ||
| prevEnd = min(prevEnd, end) | ||
| return res |
|
Contributor
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. 👍 |
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,23 @@ | ||
| class Solution: | ||
| # 19. Remove Nth Node From End of List | ||
| def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]: | ||
| dummy = ListNode(0, head) | ||
|
|
||
| # Calculate Length | ||
| length = 0 | ||
| current = head | ||
| while current: | ||
| length += 1 | ||
| current = current.next | ||
|
|
||
| stop_index = length - n | ||
|
|
||
| current = dummy | ||
| for _ in range(stop_index): | ||
| current = current.next | ||
|
|
||
| # Delete the node | ||
| current.next = current.next.next | ||
|
|
||
| # Return the start of the list (dummy.next handles if head changed) | ||
| return dummy.next |
|
Contributor
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. 재귀 풀이 좋습니다! 달레님 풀이에 스택을 활용한 방법도 있으니 같이 보셔도 좋을듯 합니다. |
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 @@ | ||
| # | ||
| # 100. Same Tree | ||
| # https://leetcode.com/problems/same-tree/description/ | ||
|
|
||
|
|
||
| # 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: | ||
| #Time Complexity: O(N)- it visits every node once | ||
| #Space Complexity: O(H)- H is the height of the tree, which is the maximum depth of the recursion stack | ||
| def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool: | ||
| # DFS approach | ||
| def dfs(node1, node2): | ||
| if node1 is None and node2 is None: | ||
| return True | ||
| if node1 is None or node2 is None: | ||
| return False | ||
| if node1.val != node2.val: | ||
| return False | ||
| return dfs(node1.left,node2.left) and dfs(node1.right,node2.right) | ||
|
|
||
| return dfs(p, q) |
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.
저도 처음에는 시작점을 기준으로 정렬해 접근했지만, 종료 시점을 기준으로 한 그리디 전략이 더 적절하다고 판단하여 풀이를 변경했습니다. 👍