-
-
Notifications
You must be signed in to change notification settings - Fork 335
[liza0525] WEEK 15 Solutions #2637
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
Changes from all commits
f21e536
b197155
90c654c
e3ade7e
399a0b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| class Solution: | ||
| def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]: | ||
| inorder_idx_map = { | ||
| val: idx | ||
| for idx, val in enumerate(inorder) | ||
| } | ||
|
|
||
| def dfs(pre_start, pre_end, in_start, in_end): | ||
| if pre_start > pre_end or in_start > in_end: | ||
| return None | ||
|
|
||
| node = TreeNode() | ||
| node.val = preorder[pre_start] | ||
|
|
||
| in_idx = inorder_idx_map[node.val] | ||
| left_size = in_idx - in_start | ||
|
|
||
| node.left = dfs( | ||
| pre_start + 1, pre_start + left_size, | ||
| in_start, in_idx - 1, | ||
| ) | ||
| node.right = dfs( | ||
| pre_start + left_size + 1, pre_end, | ||
| in_idx + 1, in_end, | ||
| ) | ||
|
|
||
| return node | ||
|
|
||
| return dfs(0, len(preorder) - 1, 0, len(inorder) - 1) |
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 각 경우에 대해 DP 배열을 사용하여 선형 배열에서 최대 금액을 구하는 방식으로, 배열 크기만큼 반복하며, 두 번 계산 후 최댓값을 선택합니다. 공간은 DP 배열에 의해 결정됩니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| class Solution: | ||
| def rob(self, nums: List[int]) -> int: | ||
| if len(nums) == 1: | ||
| return nums[0] | ||
|
|
||
| dp1 = [0 for _ in range(len(nums))] | ||
| dp2 = [0 for _ in range(len(nums))] | ||
|
|
||
| for i in range(len(nums)): | ||
| if i == 0 or i == 1: | ||
| dp1[i] = nums[i] | ||
| else: | ||
| dp1[i] = nums[i] + max(dp1[:i - 1]) | ||
|
|
||
| nums = nums[1:] + [nums[0]] | ||
|
|
||
| for i in range(len(nums)): | ||
| if i == 0 or i == 1: | ||
| dp2[i] = nums[i] | ||
| else: | ||
| dp2[i] = nums[i] + max(dp2[:i - 1]) | ||
|
|
||
| return max(dp1[:-1] + dp2[:-1]) |
|
liza0525 marked this conversation as resolved.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| class Solution: | ||
| def longestPalindrome(self, s: str) -> str: | ||
| res = "" | ||
| len_res = 0 | ||
| len_s = len(s) | ||
|
|
||
| for i in range(len_s): | ||
| left, right = i, i | ||
| while 0 <= left and right < len_s: | ||
| if s[left] == s[right]: | ||
| if right - left + 1 > len_res: | ||
| res = s[left:right + 1] | ||
| len_res = right - left + 1 | ||
| left -= 1 | ||
| right += 1 | ||
| else: | ||
| break | ||
|
|
||
| for i in range(len_s - 1): | ||
| left, right = i, i + 1 | ||
| while 0 <= left and right < len_s: | ||
| if s[left] == s[right]: | ||
| if right - left + 1 > len_res: | ||
| res = s[left:right + 1] | ||
| len_res = right - left + 1 | ||
| left -= 1 | ||
| right += 1 | ||
| else: | ||
| break | ||
|
|
||
| return res |
|
liza0525 marked this conversation as resolved.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| class Solution: | ||
| def rotate(self, matrix: List[List[int]]) -> None: | ||
| """ | ||
| Do not return anything, modify matrix in-place instead. | ||
| """ | ||
| len_matrix = len(matrix) | ||
|
|
||
|
|
||
| for i in range(len_matrix // 2): | ||
| for j in range(i, len_matrix - i - 1): | ||
| ( | ||
| matrix[i][j], | ||
| matrix[j][len_matrix - i - 1], | ||
| matrix[len_matrix - i - 1][len_matrix - j - 1], | ||
| matrix[len_matrix - j - 1][i], | ||
| ) = ( | ||
| matrix[len_matrix - j - 1][i], | ||
| matrix[i][j], | ||
| matrix[j][len_matrix - i - 1], | ||
| matrix[len_matrix - i - 1][len_matrix - j - 1], | ||
| ) | ||
|
Comment on lines
+9
to
+21
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. in-place 4-way swap을 튜플 할당으로 한 번에 처리해 주셔서, 임시 변수를 여러 개 두는 방식보다 훨씬 읽기 쉽고 깔끔한 코드가 된 것 같아요! |
||
|
liza0525 marked this conversation as resolved.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # 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 isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool: | ||
| def check_same(node, subnode): | ||
| if not node and not subnode: | ||
| return True | ||
| elif node and subnode: | ||
|
|
||
| if node.val != subnode.val: | ||
| return False | ||
|
|
||
| return ( | ||
| check_same(node.left, subnode.left) | ||
| and check_same(node.right, subnode.right) | ||
| ) | ||
| return False | ||
|
|
||
| def dfs(node): | ||
| if not node: | ||
| return False | ||
|
|
||
| if node.val == subRoot.val and check_same(node, subRoot): | ||
| return True | ||
|
|
||
| return dfs(node.left) or dfs(node.right) | ||
|
|
||
| return dfs(root) |
Uh oh!
There was an error while loading. Please reload this page.