-
-
Notifications
You must be signed in to change notification settings - Fork 306
[doh6077] WEEK 15 solutions #2338
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
11 commits
Select commit
Hold shift + click to select a range
033d4e9
Week 13 Kth Smallest Element in a BST Solution
doh6077 135d981
Week 14: 102. Binary Tree Level Order Traversal Solution
doh6077 2d3019a
Week1 House Robber solution
doh6077 c47dcb1
Merge branch 'DaleStudy:main' into main
doh6077 604bb50
줄바꿈 누락
doh6077 146fb43
Week 15: 572. Subtree of Another Tree Solution
doh6077 92a8812
Apply suggestion from @DaleSeo
doh6077 24ba0c6
Apply suggestion from @DaleSeo
doh6077 342f8d8
Refactor kthSmallest to use the nonlocal keyword for tracking the result
doh6077 52dfd63
Week 15: Rotate Image Solution
doh6077 04fcf31
Week 15: 5. Longest Palindromic Substring Solution
doh6077 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,30 @@ | ||
| from collections import deque | ||
| # 102. Binary Tree Level Order Traversal | ||
| class Solution: | ||
| def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: | ||
| # 1. Edge Case: Empty Tree | ||
| if not root: | ||
| return [] | ||
|
|
||
| result = [] | ||
| queue = deque([root]) | ||
|
|
||
| while queue: | ||
| # 2. Capture the number of nodes at the current level | ||
| level_size = len(queue) | ||
| current_level_nodes = [] | ||
|
|
||
| for _ in range(level_size): | ||
| current_node = queue.popleft() | ||
| current_level_nodes.append(current_node.val) | ||
|
|
||
| # 3. Add children to the queue for the NEXT level | ||
| if current_node.left: | ||
| queue.append(current_node.left) | ||
| if current_node.right: | ||
| queue.append(current_node.right) | ||
|
|
||
| # Add the finished level to our result | ||
| result.append(current_level_nodes) | ||
|
|
||
| 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,28 @@ | ||
| # Week1 | ||
| # 198. House Robber | ||
| # Tabulation | ||
| # Dynamic Programming | ||
|
|
||
|
|
||
| # two options: either rober or not | ||
|
|
||
| class Solution: | ||
| def rob(self, nums: List[int]) -> int: | ||
| # Top Down DP | ||
|
Member
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. Top-Down DP로 잘 작성되었지만, 시간이 되신다면 Bottom-Up DP로 메모리 최적화해를 시도해보셔도 좋을 것 같아요. |
||
|
|
||
| n = len(nums) | ||
|
|
||
| if n == 1: | ||
| return nums[0] | ||
| if n == 2: | ||
| return max(nums[0], nums[1]) | ||
| memo = {0: nums[0], 1: max(nums[0], nums[1])} | ||
| def helper(i): | ||
|
|
||
| if i in memo: | ||
| return memo[i] | ||
| else: | ||
| memo[i]= max(nums[i] + helper(i-2), helper(i-1)) | ||
| return memo[i] | ||
|
|
||
| return helper(n-1) | ||
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 @@ | ||
| # 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) | ||
| def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: | ||
| ans = [] | ||
| final = 0 | ||
| def dfs(node): | ||
| nonlocal final | ||
| if not node: | ||
| return | ||
| dfs(node.left) | ||
| ans.append(node.val) | ||
| if len(ans) == k: | ||
| final = node.val | ||
| if len(ans) < k: | ||
| dfs(node.right) | ||
| dfs(root) | ||
| return final |
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,30 @@ | ||
| # 5. Longest Palindromic Substring | ||
| # https://leetcode.com/problems/longest-palindromic-substring/ | ||
|
|
||
|
|
||
| class Solution: | ||
| def longestPalindrome(self, s: str) -> str: | ||
| #Expand Around Center | ||
| res = "" | ||
| resLen = 0 | ||
|
|
||
| for i in range(len(s)): | ||
| # odd length | ||
| l, r = i, i | ||
| while l >= 0 and r < len(s) and s[l] == s[r]: | ||
| if (r - l + 1) > resLen: | ||
| res = s[l:r+1] | ||
| resLen = r - l + 1 | ||
| l -= 1 | ||
| r += 1 | ||
|
|
||
| # even length | ||
| l, r = i, i + 1 | ||
| while l >= 0 and r < len(s) and s[l] == s[r]: | ||
| if (r - l + 1) > resLen: | ||
| res = s[l:r+1] | ||
| resLen = r - l + 1 | ||
| l -= 1 | ||
| r += 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,29 @@ | ||
| # https://leetcode.com/problems/rotate-image/ | ||
| # 48. Rotate Image | ||
| class Solution: | ||
| def rotate(self, matrix: List[List[int]]) -> None: | ||
| """ | ||
| Do not return anything, modify matrix in-place instead. | ||
| """ | ||
| # First Try - Thought process: | ||
| # group values by column index (j) to rebuild rows | ||
| # Brute force: create an extra 2D matrix and then copy it back into matrix | ||
| # Needs optimization: this approach uses extra space | ||
| matrix_result = [[] for _ in range(len(matrix))] | ||
| for i in reversed(matrix): | ||
| for j, value in enumerate(i): | ||
| matrix_result[j].append(value) | ||
| for r in range(len(matrix)): | ||
| matrix[r] = matrix_result[r] | ||
|
|
||
| # Youtube Solution - Transpose Operation + hr | ||
| # Swap i, js | ||
| n = len(matrix) | ||
| for i in range(n): | ||
| for j in range(i + 1, n): | ||
| matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j] | ||
|
|
||
| # Reflection | ||
| for i in range(n): | ||
| for j in range(n // 2): | ||
| matrix[i][j], matrix[i][n - j - 1] = matrix[i][n - j - 1], matrix[i][j] |
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 @@ | ||
| from collections import deque | ||
| from typing import Optional | ||
|
|
||
| # class TreeNode: | ||
| # def __init__(self, val=0, left=None, right=None): | ||
| # self.val = val | ||
| # self.left = left | ||
| # self.right = right | ||
| # 572. Subtree of Another Tree | ||
| # https://leetcode.com/problems/subtree-of-another-tree/description/ | ||
| class Solution: | ||
| def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool: | ||
| # Use BFS => Queue | ||
| # Edge cases | ||
| if subRoot is None: | ||
| return True | ||
| if root is None: | ||
| return False | ||
|
|
||
| def helper(node: Optional[TreeNode], subNode: Optional[TreeNode]) -> bool: | ||
| if node is None and subNode is None: | ||
| return True | ||
| if node is None or subNode is None: | ||
| return False | ||
| if node.val != subNode.val: | ||
| return False | ||
| return helper(node.left, subNode.left) and helper(node.right, subNode.right) | ||
|
|
||
| q = deque([root]) | ||
|
|
||
| while q: | ||
| curr = q.popleft() | ||
|
|
||
| # Only attempt full compare when root values match | ||
| if curr.val == subRoot.val and helper(curr, subRoot): | ||
| return True | ||
|
|
||
| if curr.left: | ||
| q.append(curr.left) | ||
| if curr.right: | ||
| q.append(curr.right) | ||
|
Comment on lines
+31
to
+41
Member
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. 이 문제는 보통 재귀만으로 해결하는 경우가 많은데, 이렇게 BFS로 각 노드를 순회하며 서브트리 비교를 하는 방법이 신선하네요. |
||
|
|
||
| return False | ||
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.
변수명이 좀 길어도 코드를 읽기가 참 편하네요 👍