LeetCode 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 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.

Example 1:

Input: [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
             Total amount you can rob = 1 + 3 = 4.

Example 2:

Input: [2,7,9,3,1]
Output: 12
Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
             Total amount you can rob = 2 + 9 + 1 = 12.

Reference Answer

思路分析

一般来说,给定一个规则,让我们求任意状态下的解,都是用动态规划。这里的规则是劫匪不能同时抢劫相邻的屋子,即我们在累加时,只有两种选择:

  1. 如果选择了抢劫上一个屋子,那么就不能抢劫当前的屋子,所以最大收益就是抢劫上一个屋子的收益
  2. 如果选择抢劫当前屋子,就不能抢劫上一个屋子,所以最大收益是到上一个屋子的上一个屋子为止的最大收益,加上当前屋子里有的钱

所以,我们只要判断一下两个里面哪个大就行了,同时也是我们的递推式。另外我们可以做一点优化,本来我们是要用一个dp数组来保存之前的结果的。但实际上我们只需要上一次和上上次的结果,所以可以用两个变量就行了。

自己开始竟然天真地以为直接算奇数索引项与偶数索引项求和比较即可,too young too simpy,毕竟[2,1,1,3]的输出应该是5,而非4。

Code

class Solution(object):
    def rob(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if not nums:
            return 0
        if len(nums) == 1:
            return nums[0]
        pre_max = nums[0]
        current_max = max(nums[0], nums[1])
        for index,count in enumerate(nums[2:]):
            temp = current_max
            current_max = max(pre_max+count, temp)
            pre_max = temp
        return max(pre_max, current_max)
        # for index, count in enumerate(nums):
        #     if index % 2 == 0:
        #         nums_even += count
        #     else:
        #         nums_odd += count
        # return max(nums_even, nums_odd)
        
#         nums_odd = list(filter(lambda c: nums.index(c) % 2 == 0, nums))
#         nums_even = list(filter(lambda c: nums.index(c) % 2 == 1, nums))
#         return max(sum(nums_even), sum(nums_odd))

Note:

  • 这道题开始尝试使用filter函数得到只含奇数索引项与偶数索引项的list,模仿
    # filter函数和map相似,但是filter是返回布尔值去去输入列表进行判断
    res = list(filter(lambda c: res.count(c) == 1, res))
    
    用了nums_odd = list(filter(lambda c: nums.index(c) % 2 == 0, nums))宣告失败,因为filter函数是针对index进行的判别,故而输出的也是index值,而非index对应的list项;

参考资料

[1] https://segmentfault.com/a/1190000003811581

猜你喜欢

转载自blog.csdn.net/Dby_freedom/article/details/84719215
今日推荐