代码随想录算法训练营day39|动态规划part07

server/2024/9/23 6:39:06/

第一题:198. House Robber 

这题的dp和滚动数组的解法都值得学习一下。

// 动态规划
class Solution {public int rob(int[] nums) {if (nums == null || nums.length == 0) return 0;if (nums.length == 1) return nums[0];int[] dp = new int[nums.length];dp[0] = nums[0];dp[1] = Math.max(dp[0], nums[1]);for (int i = 2; i < nums.length; i++) {dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]);}return dp[nums.length - 1];}
}// 使用滚动数组思想,优化空间
// 分析本题可以发现,所求结果仅依赖于前两种状态,此时可以使用滚动数组思想将空间复杂度降低为3个空间
class Solution {public int rob(int[] nums) {int len = nums.length;if (len == 0) return 0;else if (len == 1) return nums[0];else if (len == 2) return Math.max(nums[0],nums[1]);int[] result = new int[3]; //存放选择的结果result[0] = nums[0];result[1] = Math.max(nums[0],nums[1]);for(int i=2;i<len;i++){result[2] = Math.max(result[0]+nums[i],result[1]);result[0] = result[1];result[1] = result[2];}return result[2];}
}// 进一步对滚动数组的空间优化 dp数组只存与计算相关的两次数据
class Solution {public int rob(int[] nums) {if (nums.length == 1)  {return nums[0];}// 初始化dp数组// 优化空间 dp数组只用2格空间 只记录与当前计算相关的前两个结果int[] dp = new int[2];dp[0] = nums[0];dp[1] = Math.max(nums[0],nums[1]);int res = 0;// 遍历for (int i = 2; i < nums.length; i++) {res = Math.max((dp[0] + nums[i]) , dp[1] );dp[0] = dp[1];dp[1] = res;}// 输出结果return dp[1];}
}

第二题:213. House Robber II 

class Solution {public int rob(int[] nums) {if (nums == null || nums.length == 0)return 0;int len = nums.length;if (len == 1)return nums[0];return Math.max(robAction(nums, 0, len - 1), robAction(nums, 1, len));}int robAction(int[] nums, int start, int end) {int x = 0, y = 0, z = 0;for (int i = start; i < end; i++) {y = z;z = Math.max(y, x + nums[i]);x = y;}return z;}
}

第三题:337. House Robber III

class Solution {// 1.递归去偷,超时public int rob(TreeNode root) {if (root == null)return 0;int money = root.val;if (root.left != null) {money += rob(root.left.left) + rob(root.left.right);}if (root.right != null) {money += rob(root.right.left) + rob(root.right.right);}return Math.max(money, rob(root.left) + rob(root.right));}// 2.递归去偷,记录状态// 执行用时:3 ms , 在所有 Java 提交中击败了 56.24% 的用户public int rob1(TreeNode root) {Map<TreeNode, Integer> memo = new HashMap<>();return robAction(root, memo);}int robAction(TreeNode root, Map<TreeNode, Integer> memo) {if (root == null)return 0;if (memo.containsKey(root))return memo.get(root);int money = root.val;if (root.left != null) {money += robAction(root.left.left, memo) + robAction(root.left.right, memo);}if (root.right != null) {money += robAction(root.right.left, memo) + robAction(root.right.right, memo);}int res = Math.max(money, robAction(root.left, memo) + robAction(root.right, memo));memo.put(root, res);return res;}// 3.状态标记递归// 执行用时:0 ms , 在所有 Java 提交中击败了 100% 的用户// 不偷:Max(左孩子不偷,左孩子偷) + Max(右孩子不偷,右孩子偷)// root[0] = Math.max(rob(root.left)[0], rob(root.left)[1]) +// Math.max(rob(root.right)[0], rob(root.right)[1])// 偷:左孩子不偷+ 右孩子不偷 + 当前节点偷// root[1] = rob(root.left)[0] + rob(root.right)[0] + root.val;public int rob3(TreeNode root) {int[] res = robAction1(root);return Math.max(res[0], res[1]);}int[] robAction1(TreeNode root) {int res[] = new int[2];if (root == null)return res;int[] left = robAction1(root.left);int[] right = robAction1(root.right);res[0] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]);res[1] = root.val + left[0] + right[0];return res;}
}


http://www.ppmy.cn/server/100510.html

相关文章

Qt自定义控件:关于大佬“飞扬青云“的自定义UI控件的使用教程(MinGw,MSVC)

前言 最近在搞自定义控件&#xff0c;无意间发现大佬飞扬青云的开源项目&#xff0c;Qt/C编写超精美自定义控件 这里先贴出大佬项目地址和博客 码云&#xff1a;wwlzq5/qucsdk (gitee.com)&#xff08;旧版下载地址Qt4.7到Qt5.14&#xff09; github&#xff1a;https://git…

MongoDB数据类型介绍

MongoDB作为一种高性能、开源、无模式的文档型数据库&#xff0c;支持丰富的数据类型&#xff0c;以满足各种复杂的数据存储需求。本文将详细介绍MongoDB支持的主要数据类型&#xff0c;包括数值类型、字符串类型、日期和时间类型、布尔类型、二进制类型、数组、对象以及其他扩…

CsvExport:一个.Net高性能、低内存的CSV导出开源库

在我们项目开发中&#xff0c;导出CSV数据功能是非常常见的。 今天推荐一个高性能、低内存的CSV导出开源库。 01 项目简介 CsvExport是一个基于C#非常简单和快速的CSV导出开源库。 该开源库的核心特点&#xff1a; 导出功能兼容性高&#xff08;自动检测分隔符&#xff0c;…

Element UI导航菜单刷新就复原问题解决方法~

1、首先要知道为什么一刷新就复原了&#xff0c;是因为default-active属性设置的是默认值&#xff0c;是一个死值&#xff0c;一旦刷新就会复原&#xff0c;造成高亮不能保持&#xff0c;那么怎么解决呢&#xff1f; 2、很简单&#xff0c;无需像一些博主一样绑定path。思路&a…

机械行业数字化生产供应链产品解决方案(七)

在机械行业的数字化生产供应链产品解决方案中&#xff0c;通过全面部署物联网&#xff08;IoT&#xff09;传感器、智能分析平台和自动化控制系统&#xff0c;实现对生产设备的实时监控和数据采集&#xff0c;并结合大数据和人工智能技术进行深度分析&#xff0c;从而优化生产调…

达梦数据库(十) -------- mybatis-plus 整合达梦时,自动生成的 sql 语句报错

一丶【问题描述】&#xff1a; mybatis-plus 整合达梦时&#xff0c;应用系统项目的 sql 语句中包含数据库关键字&#xff0c;导致 mybatis-plus 自动生成的 sql 语句会报错&#xff0c;如下图所示&#xff1a; 二丶【问题解决】&#xff1a; 问题原因&#xff1a;mybatis-pl…

BUG分析以及BUG定位

一般来说bug大多数存在于3个模块&#xff1a; 1、前台界面&#xff0c;包括界面的显示&#xff0c;兼容性&#xff0c;数据提交的判断&#xff0c;页面的跳转等等&#xff0c;这些bug基本都是一眼可见的&#xff0c;不太需要定位&#xff0c;当然也不排除一些特殊情况&#xf…

【6大设计原则】依赖倒置原则:构建灵活软件架构的基石 - 通过代码实例深入解析

1.引言 1.1为什么要学习依赖倒置原则 在软件开发过程中&#xff0c;我们经常需要对代码进行修改和扩展。如果代码之间的耦合度过高&#xff0c;那么在进行修改或扩展时&#xff0c;可能会对其他部分的代码产生影响&#xff0c;甚至引发错误。这就要求我们在编写代码时&#xf…