Skip to content

Commit a3ef3cc

Browse files
authored
Merge pull request larissalages#266 from Vedant-S/patch-3
Added Leetcode 198 House Robber Solution
2 parents 805771a + 20ab1a4 commit a3ef3cc

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)