198. House Robber

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 systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.



def rob(nums):
       
    L = len(nums)
    max_amounts = [0]*3                  
    max_amount = 0
   
    for i in range(L):
        j = (i + 3) % 3
        max_amounts[j] = max(max_amounts[j-2] + nums[i], max_amounts[j-3] + nums[i])
        max_amount = max(max_amount, max_amounts[j])            
   
    return max_amount
   

Comments

Popular posts from this blog

347. Top K Frequent Elements

849. Maximize Distance to Closest Person

674. Longest Continuous Increasing Subsequence