We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 91feb15 commit 821aa4aCopy full SHA for 821aa4a
house-robber/hi-rachel.py
@@ -1,4 +1,14 @@
1
# O(n) time, O(n) space
2
+# dp[i]는 i번째 집까지 봤을 때의 최대 누적 금액
3
+
4
+# 두 가지 선택지를 고려:
5
+# 1. 이 집을 턴다:
6
+# 이전 집은 털 수 없으니 dp[i-2] + nums[i]
7
+# 2. 이 집을 안 턴다:
8
+# 그냥 전 집까지의 최대 금액 유지: dp[i-1]
9
+# 두 가지 선택지 중 큰 걸 선택
10
+# **dp[i] = max(dp[i-1], dp[i-2] + nums[i])**
11
+# nums 길이가 2인 경우 range(2, 2)는 for문 안 돈다.
12
13
class Solution:
14
def rob(self, nums: List[int]) -> int:
@@ -13,7 +23,7 @@ def rob(self, nums: List[int]) -> int:
23
dp[i] = max(dp[i - 1], nums[i] + dp[i - 2])
24
15
25
return dp[-1]
16
-
26
17
27
18
28
# TS 코드
19
29
# function rob(nums: number[]): number {
0 commit comments