-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhouse_robber.py
44 lines (34 loc) · 875 Bytes
/
house_robber.py
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
'''
URL = https://leetcode.com/problems/house-robber/
'''
from typing import List
class Solution:
# def rob(self, nums: List[int]) -> int:
#
# cache = {}
#
# return self.rob_helper(nums, index=len(nums)-1, cache=cache)
#
# def rob_helper(self, nums, index, cache):
#
# if index < 0:
# return 0
#
# if index in cache:
# return cache[index]
#
# curr = self.rob_helper(nums, index-2, cache) + nums[index]
# prev = self.rob_helper(nums, index-1, cache)
#
# result = max(curr, prev)
#
# cache[index] = result
#
# return result
def rob(self, nums: List[int]) -> int:
curr, prev = 0, 0
for num in nums:
temp = curr
curr = max(prev + num, curr)
prev = temp
return curr