6 Line Python using DP


  • 0
    class Solution(object):
        def rob(self, nums):
            if not nums:
                return 0
            dp = [ 0 for i in xrange(len(nums)) ]
            for i in xrange(len(nums)):
                dp[i] = nums[0] if i == 0 else ( max(nums[0], nums[1]) if i == 1 else max(dp[i - 1], dp[i - 2] + nums[i]) )
            return dp[-1]
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.