前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Q198 House Robber

Q198 House Robber

作者头像
echobingo
发布2018-04-25 16:54:37
4760
发布2018-04-25 16:54:37
举报
文章被收录于专栏:Bingo的深度学习杂货店

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.

解题思路:

此题的意思是,给定一个非负整数列表,从中选取元素,使得元素两两不相邻,找到满足条件的这些元素的最大和。

假设列表长度为 n,先观察规律:

代码语言:javascript
复制
当 n = 0 时,列表为空,返回 0
当 n = 1 时,列表只有一个元素,返回 nums[0]
当 n = 2 时,列表有两个元素,返回 max(nums[0], num[1])
当 n = 3 时,列表返回的下标组合可能为 [0,2] 和 [1]
当 n = 4 时,列表返回的下标组合可能为 [0,2] 、[1,3] 和 [0,3]
当 n = 5 时,列表返回的下标组合可能为 [0,2,4] 、[1,3] 、[0,3] 和 [1,4]
当 n = 6 时,列表返回的下标组合可能为 [0,2,4] 、 [1,3,5]、[0,3,5]、[1,4] 和 [0,2,5]
.....

以 n = 6 为例,[0,2,4] 和 [1,4] 均可以由 n = 5 时得到; [1,3,5]、[0,3,5] 和 [0,2,5] 可以由 n = 4 的每个元素加上 [5] 得到。

由此可以得到递归规律:

代码语言:javascript
复制
设 k 为列表最大下标,则有:
f(0) = nums[0]
f(1) = max(num[0], num[1])
f(k) = max(f(k-2) + nums[k], f(k-1))

因此,此题可以使用递归求解。时间复杂度 O(n),空间复杂度 O(1)。

Python实现:
代码语言:javascript
复制
class Solution(object):
    def rob(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        high = len(nums) - 1
        if high == -1:
            return 0
        elif high == 0:
            return nums[0]
        elif high == 1:
            return max(nums[0], nums[1])
        else:
            return max(self.rob(nums[:high-1]) + nums[high], self.rob(nums[:high]))

这种方法得到的答案是正确的,但是在提交的时候会超时。因此,需要改写为非递归的方法,如下:

代码语言:javascript
复制
class Solution(object):
    def rob2(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        # pre2 相当于 f(k-2), pre1 相当于 f(k-1), now 相当于 f(k)
        pre2 = pre1 = now = 0  
        for num in nums:
            pre2 = pre1
            pre1 = now
            now = max(pre2 + num, pre1)
        return now

a = [6,3,10,8,2,10,3,5,10,5,3]  
b = Solution()
print(b.rob2(a))  # 39  # 下标 [0, 2, 5, 8, 10] = 6 + 10 + 10 + 10 + 3 = 39

这种将递归方法改写为非递归方法与斐波那契数列改写方法类似,可以参考题目 Q70 Climbing Stairs

观察上述非递归方法,可以更近一步简化,即 pre2 = pre1; pre1 = now 可以合并为一句 pre2 = now, 这时 now = max(pre2 + num, pre1) 变为 now = max(pre2 + num, now):

代码语言:javascript
复制
class Solution(object):
    def rob3(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        last = now = 0 
        for num in nums:
            last, now = now, max(last + num, now)
        return now

a = [6,3,10,8,2,10,3,5,10,5,3]  
b = Solution()
print(b.rob3(a))  # 39  # 下标 [0, 2, 5, 8, 10] = 6 + 10 + 10 + 10 + 3 = 39
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018.02.28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 解题思路:
  • Python实现:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档