8000 added House Robber · 0ff5ec/Programming@ee8d814 · GitHub
[go: up one dir, main page]

Skip to content
< 8000 /div>

Commit ee8d814

Browse files
committed
added House Robber
1 parent ed17a2d commit ee8d814

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

rob.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution(object):
2+
def rob(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
"""
7+
if len(nums) == 0:
8+
return 0
9+
if len(nums) == 1:
10+
return nums[0]
11+
if len(nums) == 2:
12+
return max(nums)
13+
profit = [0]*len(nums)
14+
profit[0] = nums[0]
15+
profit[1] = max(nums[0], nums[1])
16+
for i in range(2,len(nums)):
17+
profit[i] = max(profit[i-2] + nums[i], profit[i-1])
18+
print profit
19+
return profit[-1]

0 commit comments

Comments
 (0)
0