Skip to content

Commit

Permalink
Merge pull request #600 from mangodm-web/main
Browse files Browse the repository at this point in the history
[mangodm-web] Week14 Solutions
  • Loading branch information
mangodm-web authored Nov 17, 2024
2 parents 187e613 + 6a37b83 commit e6551e4
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
42 changes: 42 additions & 0 deletions binary-tree-level-order-traversal/mangodm-web.py
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
35 changes: 35 additions & 0 deletions house-robber-ii/mangodm-web.py
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])

0 comments on commit e6551e4

Please sign in to comment.