You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected andit will automatically contact the police if two adjacent houses were broken into on the same night.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonightwithout alerting the police.
Method o(n) time, O(1) space
- keep track of rob_prev, not rob
class Solution(object):
def rob(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
rob_prev, not_rob_prev = 0, 0
for num in nums:
temp = rob_prev
rob_prev = not_rob_prev + num
not_rob_prev = max(not_rob_prev, temp)
return max(rob_prev, not_rob_prev)