1. 程式人生 > >轉 小白刷LeetCode 198.house robber

轉 小白刷LeetCode 198.house robber

題目:

ou 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 and it 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 tonight without alerting the police. --------------------- 

class Solution:
    def rob(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if len(nums)==0:
            return 0
        elif len(nums)<2:   #避免只有一個元素的陣列,導致程式報錯
            return nums[0]
        money = [0]*len(nums)
        money[0],money[1] = nums[0],max(nums[0],nums[1])
        for i in range(2,len(nums)):
            money[i] = max(nums[i]+money[i-2],money[i-1])
        return money[-1]