Skip to content

Commit 6571ded

Browse files
committed
solve: house robber
1 parent 381f54e commit 6571ded

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

house-robber/evan.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def rob(self, nums: List[int]) -> int:
6+
house_length = len(nums)
7+
8+
if house_length == 0:
9+
return 0
10+
if house_length == 1:
11+
return nums[0]
12+
if house_length == 2:
13+
return max(nums[0], nums[1])
14+
15+
dp = [nums[0], max(nums[0], nums[1])]
16+
17+
for index in range(2, house_length):
18+
dp.append(max(dp[index - 1], dp[index - 2] + nums[index]))
19+
20+
return dp[-1]

0 commit comments

Comments
 (0)