Skip to content

Commit b9cd6b6

Browse files
author
MJ Kang
committed
house robber
1 parent a907534 commit b9cd6b6

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

house-robber/mandoolala.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from typing import List
2+
3+
class Solution:
4+
def rob(self, nums: List[int]) -> int:
5+
'''
6+
[Complexity]
7+
Time: O(n)
8+
Space: O(n)
9+
'''
10+
cnt = len(nums)
11+
12+
if cnt == 1:
13+
return nums[0]
14+
if cnt == 2:
15+
return max(nums[0], nums[1])
16+
17+
dp = [0] * cnt
18+
dp[0] = nums[0]
19+
dp[1] = max(nums[0], nums[1])
20+
21+
for i in range(2, cnt):
22+
# skip: dp[i-1]
23+
# rob: dp[i-2]+nums[i]
24+
dp[i] = max(dp[i - 1], dp[i - 2] + nums[i])
25+
return max(dp)

0 commit comments

Comments
 (0)