代码随想录算法训练营Day48 (day47休息) | 动态规划(9/17) LeetCode 198.打家劫舍 213.打家劫舍II 337.打家劫舍III

2023-09-16 00:37:48

来到了新的一块内容:打家劫舍问题。

第一题

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 systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.

直觉告诉我,可能会涉及到一些贪心问题,为了保证利益最大,可能要找钱最多的地方优先动手。而当前房屋偷与不偷取决于,前一个房屋和前两个房屋是否被偷了。

仍然可以利用动态规划五部曲来解决。

  1. 确定dp数组(dp table)以及下标的含义
    1. dp[i]:考虑下标i(包括i)以内的房屋,最多可以偷窃的金额为dp[i]
  2. 确定递推公式
    1. 决定dp[i]的因素就是第i房间偷还是不偷。如果偷第i房间,那么dp[i] = dp[i - 2] + nums[i] ,即:第i-1房一定是不考虑的,找出 下标i-2(包括i-2)以内的房屋,最多可以偷窃的金额为dp[i-2] 加上第i房间偷到的钱。
    2. 如果不偷第i房间,那么dp[i] = dp[i - 1],即考 虑i-1房,然后dp[i]取最大值,即dp[i] = max(dp[i - 2] + nums[i], dp[i - 1]);
  3. dp数组如何初始化
    1. 从递推公式dp[i] = max(dp[i - 2] + nums[i], dp[i - 1]);可以看出,递推公式的基础就是dp[0] 和 dp[1]
    2. 从dp[i]的定义上来讲,dp[0] 一定是 nums[0],dp[1]就是nums[0]和nums[1]的最大值即:dp[1] = max(nums[0], nums[1]);
  4. 确定遍历顺序
    1. dp[i] 是根据dp[i - 2] 和 dp[i - 1] 推导出来的,那么一定是从前到后遍历!

  5. 举例推导dp数组
class Solution:
    def rob(self, nums: List[int]) -> int:
        if len(nums) == 0:  
            return 0
        if len(nums) == 1:  
            return nums[0]

        dp = [0] * len(nums)
        dp[0] = nums[0]  
        dp[1] = max(nums[0], nums[1]) 

        for i in range(2, len(nums)):
            dp[i] = max(dp[i - 2] + nums[i], dp[i - 1])

        return dp[-1] 

第二题

213. House Robber II

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 a security system connected, and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.

和前一题比较,这道题变成环状了,需要考虑下面三个因素:

对于一个数组,成环的话主要有如下三种情况:

  • 情况一:考虑不包含首尾元素
  • 情况二:考虑包含首元素,不包含尾元素
  • 情况三:考虑包含尾元素,不包含首元素
class Solution:
    def rob(self, nums: List[int]) -> int:
        if not nums:  # 如果没有房屋,返回0
            return 0

        if len(nums) == 1:  # 如果只有一个房屋,返回该房屋的金额
            return nums[0]

        # 情况二:不抢劫第一个房屋
        prev_max = 0  # 上一个房屋的最大金额
        curr_max = 0  # 当前房屋的最大金额
        for num in nums[1:]:
            temp = curr_max  # 临时变量保存当前房屋的最大金额
            curr_max = max(prev_max + num, curr_max)  # 更新当前房屋的最大金额
            prev_max = temp  # 更新上一个房屋的最大金额
        result1 = curr_max

        # 情况三:不抢劫最后一个房屋
        prev_max = 0  # 上一个房屋的最大金额
        curr_max = 0  # 当前房屋的最大金额
        for num in nums[:-1]:
            temp = curr_max  # 临时变量保存当前房屋的最大金额
            curr_max = max(prev_max + num, curr_max)  # 更新当前房屋的最大金额
            prev_max = temp  # 更新上一个房屋的最大金额
        result2 = curr_max

        return max(result1, result2)

第三题

 337. House Robber III

The thief has found himself a new place for his thievery again. There is only one entrance to this area, called root.

Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if two directly-linked houses were broken into on the same night.

Given the root of the binary tree, return the maximum amount of money the thief can rob without alerting the police.

本题一定是要后序遍历,因为通过递归函数的返回值来做下一步计算

class Solution:
    def rob(self, root: TreeNode) -> int:
        if root is None:
            return 0
        if root.left is None and root.right  is None:
            return root.val
        # 偷父节点
        val1 = root.val
        if root.left:
            val1 += self.rob(root.left.left) + self.rob(root.left.right)
        if root.right:
            val1 += self.rob(root.right.left) + self.rob(root.right.right)
        # 不偷父节点
        val2 = self.rob(root.left) + self.rob(root.right)
        return max(val1, val2)

更多推荐

【Hash表】判断字母异位词-力扣 242

💝💝💝欢迎来到我的博客,很高兴能够在这里和您见面!希望您在这里可以感受到一份轻松愉快的氛围,不仅可以获得有趣的内容和知识,也可以畅所欲言、分享您的想法和见解。推荐:kuan的首页,持续学习,不断总结,共同进步,活到老学到老导航檀越剑指大厂系列:全面总结java核心技术点,如集合,jvm,并发编程redis,kaf

计网第五章(运输层)(五)(TCP拥塞控制)

目录一、基本概念二、拥塞控制算法慢开始:拥塞避免:快重传:快恢复:一、基本概念若对网络中某一资源的需求超过了该资源所能提供的可用部分(供不应求),网络性能就会变坏。在计算机网络中的带宽、交换节点中的缓存和处理机等都是网络的资源。如果出现拥塞而不控制,整个网络的吞吐量(单位时间内从网络输出的分组数量)会随着输入负荷的增大

数据结构——查找(二叉排序树)

文章目录前言一、二叉排序树构造二叉排序树步骤构造二叉排序树步骤图二叉排序树的查找二叉排序树查找递归算法二叉排序树查找非递归算法二叉排序树的插入二叉排序树插入结点——递归算法二叉排序树插入结点——非递归算法二叉排序树的删除总结前言二叉排序树查找定义二叉排序树构造二叉排序树查找递归和非递归算法二叉排序树插入递归和非递归算法

【Python】pyecharts 模块 ⑥ ( 绘制柱状图 | pyecharts 绘制柱状图步骤 | 柱状图 x 轴 / y 轴 翻转 | 柱状图数据标签位置设置 )

文章目录一、pyecharts绘制基础柱状图1、pyecharts绘制柱状图步骤2、代码示例-pyecharts绘制柱状图二、柱状图其它设置1、柱状图x轴/y轴翻转2、柱状图数据标签位置设置pyecharts画廊网站:https://gallery.pyecharts.org/#/在该网站可查看官方示例一、pyecha

关于Allegro17.4 3d模型大小不匹配问题解决

文章目录问题概述问题原因解决办法问题概述Allegro17.4版本采用3DCanvas工具进行3D模型的映射,映射后,无需保存任何映射文件,只要指定好step文件路径,即可将模型映射信息保存在pcb封装文件中,方便快捷。映射流程如下:打开Allegro软件,菜单选择Setup->UserPreferencesEdito

基于Java+SpringBoot+Vue的在线音乐网站设计和实现

基于Java+SpringBoot+Vue的在线音乐网站设计和实现源码传送入口前言主要技术系统设计功能截图数据库设计代码论文目录订阅经典源码专栏Java项目精品实战案例《500套》源码获取源码传送入口前言大数据时代下,数据呈爆炸式地增长。为了迎合信息化时代的潮流和信息化安全的要求,利用互联网服务于其他行业,促进生产,已

介绍Spring Security框架,以及如何使用它实现应用程序的安全性

文章目录什么是SpringSecurity?SpringSecurity的工作原理如何使用SpringSecurity构建安全的应用程序步骤1:添加SpringSecurity依赖步骤2:配置SpringSecurity步骤3:配置安全性规则步骤4:创建用户和角色步骤5:创建自定义登录页面步骤6:运行应用程序总结🎈个

又一职业技术技能标准官宣!

为贯彻落实《关于深化人才发展体制机制改革的意见》,推动实施人才强国战略,促进专业技术人员提升职业素养、补充新知识新技能,实现人力资源深度开发,推动经济社会全面发展,根据《中华人民共和国劳动法》有关规定,工业和信息化部教育与考试中心联合有关部门组织并制定了《研发效能(DevOps)工程师国家职业技术认证》。其中包含两个方

美团笔试-小美的元素删除

小美的元素删除小美有一个数组,她希望删除k个元素,使得剩余的元素两两之间互为倍数关系。你能告诉小美有多少种删除方案吗?由于答案过大,请对10^9+7模。输入描述第一行输入两个整数n,k(1<=k<=n<=10^3)表示数组长度,删除的元素数量。第二行输入n,k个整数表示数组a(1<=ai<=10^9)。保证给定的数组中

Prometheus PromQL数据查询语言

PromQL简介PromQL(PrometheusQueryLanguage)是Prometheus内置的数据查询语言。支持用户进行实时的数据查询及聚合操作。Prometheus基于指标名称(metricsname)以及附属的标签集(labelset)唯一定义一条时间序列指标名称代表着监控目标上某类可测量属性的基本特征

C#中string类型是引用类型

.Net框架程序设计(修订版)中有这样一段描述:String类型直接继承自Object,这使得它成为一个引用类型,也就是说线程上的堆栈上不会驻留有任何字符串。名称CTS类型说明stringSystem.StringUnicode字符串stringstr1="hello";stringstr2="world";这是一个值

热文推荐