My python solution


  • 1
    class Solution:
        # @param num, a list of integer
        # @return an integer
        def rob(self, num):
            if not num: return 0
            n = len(num)
            if n==1: return num[0]
            dp = [0]*n
            dp[0],dp[1] = num[0],max(num[1],num[0])
            for i in xrange(2,n):
                dp[i]=max(dp[i-1],dp[i-2]+num[i])
            return dp[n-1]

Log in to reply
 

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