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 135d981 commit 2d3019aCopy full SHA for 2d3019a
1 file changed
house-robber/doh6077.py
@@ -0,0 +1,30 @@
1
+# Week1
2
+# 198. House Robber
3
+# Tabulation
4
+# Dynamic Programming
5
+
6
7
+# two options: either rober or not
8
9
+class Solution:
10
+ def rob(self, nums: List[int]) -> int:
11
+ # Top Down DP
12
13
+ n = len(nums)
14
15
+ if n ==1:
16
+ return nums[0]
17
+ if n ==2:
18
+ return max(nums[0], nums[1])
19
+ memo = {0:nums[0], 1: max(nums[0], nums[1])}
20
+ def helper(i):
21
22
+ if i in memo:
23
+ return memo[i]
24
+ else:
25
+ memo[i]= max(nums[i] + helper(i-2), helper(i-1))
26
27
28
+ return helper(n-1)
29
30
0 commit comments