-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #600 from mangodm-web/main
[mangodm-web] Week14 Solutions
- Loading branch information
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,42 @@ | ||
from collections import deque | ||
from typing import List, Optional | ||
|
||
|
||
class TreeNode: | ||
def __init__(self, val=0, left=None, right=None): | ||
self.val = val | ||
self.left = left | ||
self.right = right | ||
|
||
|
||
class Solution: | ||
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: | ||
""" | ||
- Idea: ๋๋น ์ฐ์ ํ์(BFS)๋ฅผ ์ด์ฉํ์ฌ ์ด์ง ํธ๋ฆฌ๋ฅผ ๋จ๊ณ ๋ณ๋ก ์ํํ๋ค. | ||
- Time Complexity: O(n). n์ ํธ๋ฆฌ์ ๋ ธ๋ ์. | ||
๋ชจ๋ ๋ ธ๋๋ฅผ ํ๋ฒ์ฉ ๋ฐฉ๋ฌธํ๋ฏ๋ก O(n) ์๊ฐ์ด ๊ฑธ๋ฆฐ๋ค. | ||
- Space Complexity: O(n). n์ ํธ๋ฆฌ์ ๋ ธ๋ ์. | ||
๊ฐ์ฅ ์๋ ๋จ๊ณ์ ์๋ ๋ ธ๋๋ฅผ ์ ์ฅํ ๋ ๊ฐ์ฅ ๋ง์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฌ์ฉํ๊ณ , ์ด๋ n์ ๋น๋กํ๊ธฐ ๋๋ฌธ์ O(n) ๋งํผ์ ๋ฉ๋ชจ๋ฆฌ๊ฐ ํ์ํ๋ค. | ||
""" | ||
if not root: | ||
return [] | ||
|
||
result = [] | ||
queue = deque() | ||
queue.append(root) | ||
|
||
while queue: | ||
level = [] | ||
|
||
for i in range(len(queue)): | ||
node = queue.popleft() | ||
level.append(node.val) | ||
|
||
if node.left: | ||
queue.append(node.left) | ||
if node.right: | ||
queue.append(node.right) | ||
|
||
result.append(level) | ||
|
||
return result |
This file contains 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,35 @@ | ||
from typing import List | ||
|
||
|
||
class Solution: | ||
def rob(self, nums: List[int]) -> int: | ||
""" | ||
- Idea: i๋ฒ์งธ ์ง๊น์ง์ ์ต๋ ๊ธ์ก์ ๋ ๊ฐ์ง ์ค ๋ ํฐ ๊ฐ์ผ๋ก ๊ฒฐ์ ๋๋ค. | ||
1. (i-2๋ฒ์งธ ์ง๊น์ง์ ์ต๋ ๊ธ์ก) + i๋ฒ์งธ ์ง์ ๊ธ์ก | ||
2. (i-1๋ฒ์งธ ์ง๊น์ง์ ์ต๋ ๊ธ์ก) | ||
์ด๋ฅผ ์ด์ฉํด ๋์ ํ๋ก๊ทธ๋๋ฐ์ผ๋ก ๊ฐ ์ง๊น์ง์ ์ต๋ ๊ธ์ก์ ๊ณ์ฐํ๋ค. | ||
๋ค๋ง, ๋งจ ๋ง์ง๋ง ์ง๊ณผ ์ฒซ๋ฒ์งธ ์ง์ด ์ด์ด์ง ์ฌ์ดํด(cycle) ํํ์ด๊ธฐ ๋๋ฌธ์ | ||
์ฒซ๋ฒ์งธ ์ง๊ณผ ๋ง์ง๋ง ์ง์ด ๋์์ ๋๋๋ง์ ์๋ ์๋ค. | ||
์ด๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ์ํด ๋ ๊ฐ์ง ๊ฒฝ์ฐ๋ก ๋๋ ์ ๊ฐ๊ฐ ๊ณ์ฐํ๊ณ , ๋ ์ค ๋ ํฐ ๊ฐ์ ์ ํํ๋ค. | ||
1. ์ฒซ๋ฒ์งธ ์ง์ ํฌํจํ๊ณ ๋ง์ง๋ง ์ง์ ํฌํจํ์ง ์๋ ๊ฒฝ์ฐ | ||
2. ์ฒซ๋ฒ์งธ ์ง์ ํฌํจํ์ง ์๊ณ ๋ง์ง๋ง ์ง์ ํฌํจํ๋ ๊ฒฝ์ฐ | ||
- Time Complexity: O(n). n์ ์ง์ ๊ฐ์. | ||
๋ชจ๋ ์ง์ ํ๋ฒ์ฉ ์ํํด์ผ ํ๋ฏ๋ก O(n) ์๊ฐ์ด ๊ฑธ๋ฆฐ๋ค. | ||
- Space Complexity: O(n). n์ ์ง์ ๊ฐ์. | ||
๊ฐ ์ง๊น์ง์ ์ต๋ ๊ธ์ก์ ์ ์ฅํ๊ธฐ ์ํด ๋ฐฐ์ด์ ์ฌ์ฉํ๋ฏ๋ก O(n) ๋งํผ์ ๋ฉ๋ชจ๋ฆฌ๊ฐ ํ์ํ๋ค. | ||
""" | ||
if len(nums) == 1: | ||
return nums[0] | ||
|
||
dp1 = [0] + [0] * len(nums) | ||
dp1[1] = nums[0] | ||
|
||
dp2 = [0] + [0] * len(nums) | ||
dp2[1] = 0 | ||
|
||
for i in range(2, len(nums) + 1): | ||
dp1[i] = max(dp1[i - 1], dp1[i - 2] + nums[i - 1]) | ||
dp2[i] = max(dp2[i - 1], dp2[i - 2] + nums[i - 1]) | ||
|
||
return max(dp1[-2], dp2[-1]) |