【LeetCode】213. House Robber ||

本篇博客介绍了LeetCode 213题——House Robber II的解题思路,重点在于解决环形排列的房子如何避免触发警报。博主通过分析得出两种情况,并利用动态规划的方法分别处理这两种情况,最终实现AC代码的编写与优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

213. House Robber II

Description:

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, 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: [2,3,2]
Output: 3
Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2),
             because they are adjacent houses.

Example 2:

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. 

解题思路:

分析:

由于最后一个房子和第一个房子相邻,那么就会出现两种情况:

1)抢劫第一个房子的时候,不能抢劫最后一个房子。这种情况下,就是从输入序列中去除最后一个房子,也就是求输入序列的子序列中抢劫钱数最多,即求序列[6, 6, 0, 3, 1]。

2)抢劫最后一个房子的时候,不能抢劫第一个房子。这种情况下,就是从输入序列去除第一个房子,也就是求输入序列的子序列中抢劫钱数最多,即求序列[6, 0, 3, 1, 9]。

因此:

通过对上面的两种情况的分析,每一种情况都是这道题(【LeetCode】198. House Robber)的输入,我们可以把这道题的结果封装成一个函数,对上面两种情况分别进行输入计算即可。

(1)动态规划解法:

已经AC的代码:

class Solution:

    def __init__(self):
        self.arr = []

    def rob(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if len(nums) == 0:
            return 0
        if len(nums) == 1:
            return nums[0]
        if len(nums) == 2:
            return max(nums[0], nums[1])
        self.arr = [0 for _ in range(len(nums))]
        return max(self.getRob(nums, self.arr, 0, len(nums)-2),
                   self.getRob(nums, self.arr, 1, len(nums)-1))

    def getRob(self, nums, arr, start, end):
        """
        :param nums: list[int]
        :param arr:  list[int]
        :param start: int
        :param end: int
        :return:
        """
        arr[start] = nums[start]
        arr[start + 1] =max(nums[start], nums[start + 1])
        for i in range(start+2,end + 1):
            arr[i] = max(nums[i] + arr[i - 2], arr[i - 1])
        return  arr[end]

solution = Solution()
# test 1
# nums = []
# test 2
# nums = [2, 3]
# test 3
nums = [2, 3, 2]
print(solution.rob(nums))

​​AC之后,感觉自己写代码不够简洁,参考https://leetcode.com/problems/house-robber-ii/discuss/196728/python-dp-solution,优化代码后为:

已经AC的代码:

class Solution:

    def rob(self, nums):
        """
        :param nums: list[int]
        :return:
        """
        if len(nums) == 0: return  0
        if len(nums) == 1: return  nums[0]
        if len(nums) == 2: return  max(nums)
        def getRob(start, stop, nums):
            n = nums[start: stop + 1]
            dp = [0] * len(n)
            dp[0], dp[1] = n[0], max(n[0], n[1])
            for i in range(2, len(n)):
                dp[i] = max(dp[i - 1], dp[i - 2] + n[i])
            return dp[-1]
        return  max(getRob(0, len(nums) - 2, nums), getRob(1, len(nums) - 1, nums))

solution = Solution()
# test 1
# nums = []
# test 2
# nums = [2, 3]
# test 3
nums = [2, 3, 2]
print(solution.rob(nums))

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值