Skip to content

Commit 5111968

Browse files
committed
solve : house robber
1 parent 5c40967 commit 5111968

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

house-robber/samthekorean.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# TC : O(n)
2+
# SC : O(1)
3+
class Solution:
4+
def rob(self, nums):
5+
n = len(nums)
6+
if n == 0:
7+
return 0
8+
if n == 1:
9+
return nums[0]
10+
11+
dp = [-1] * n
12+
dp[0] = nums[0]
13+
if n > 1:
14+
dp[1] = max(nums[0], nums[1])
15+
16+
for i in range(2, n):
17+
dp[i] = max(dp[i - 1], nums[i] + dp[i - 2])
18+
19+
return dp[-1]

0 commit comments

Comments
 (0)