Skip to content

Latest commit

 

History

History

HouseRobber

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

House Robber

This proble is easy to solve, like Min Cost Climbing Stairs, the better solution of Dynamic Programming is like below:

class Solution(object):
    def rob(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        length = len(nums)
        
        dp = [0] * length
        
        for i in xrange(length):
            dp[i] = max(nums[i] + (dp[i - 2] if i > 1 else 0), dp[i - 1] if i > 0 else 0)
            
        return dp[length - 1] if length > 0 else 0