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.
2 parents 805771a + 20ab1a4 commit a3ef3ccCopy full SHA for a3ef3cc
leetcode/python/Dynamic Programming/198.House Robber.py
@@ -0,0 +1,16 @@
1
+class Solution(object):
2
+ def rob(self, nums):
3
+ odd_max = even_max = 0
4
+
5
+ for i in range(len(nums)):
6
+ if i % 2 == 0: # odd case as index for first house is 0
7
+ odd_max = max(odd_max + nums[i], even_max) # record max val for house values in odd position
8
+ else:
9
+ even_max = max(even_max + nums[i], odd_max) # record max val for house values in even position
10
11
+ return max(odd_max, even_max)
12
13
14
+if __name__ == '__main__':
15
+ nums = [2,7,9,3,1]
16
+ print(Solution().rob(nums))
0 commit comments