-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhouse robber.py
More file actions
48 lines (43 loc) · 1 KB
/
Copy pathhouse robber.py
File metadata and controls
48 lines (43 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
__author__ = 'Martin'
class Solution(object):
'''
def rob(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
return self.max(len(nums)-1, nums)
def max(self, i, nums):
if i < 0:
return 0
if i == 0:
return nums[0]
m1 = self.max(i-1, nums)
m2 = self.max(i-2, nums) + nums[i]
if m1 > m2:
return m1
else:
return m2
'''
'''
def rob(self, nums):
l = len(nums)
dp = [0] * (l+1)
if l:
dp[1] = nums[0]
for i in range(2, l+1):
dp[i] = max(dp[i-1], dp[i-2] + nums[i-1])
return dp[l]
'''
def rob(self, nums):
l = len(nums)
odd, even = 0, 0
for i in range(l):
if i % 2:
odd = max(even, odd + nums[i])
else:
even = max(odd, even + nums[i])
return max(odd, even)
s = Solution()
nums = [2,1]
print(s.rob(nums))