当前位置: 首页 > news >正文

算法训练营第五十天 | LeetCode 198 打家劫舍、LeetCode 213 打家劫舍II、LeetCode 337 打家劫舍III

LeetCode 198 打家劫舍


代码如下:

class Solution {
public:int rob(vector<int>& nums) {vector<int> dp(nums.size() + 1, 0);dp[1] = nums[0];for (int i = 2; i <= nums.size(); i++) {dp[i] = max(dp[i - 1] ,dp[i - 2] + nums[i - 1]);}return dp[nums.size()];}
};

LeetCode 213 打家劫舍II


分情况进行讨论即可

class Solution {
public:int rob(vector<int>& nums) {if (nums.size() == 0) return 0;if (nums.size() == 1) return nums[0];int result1 = robRange(nums, 0, nums.size() - 2); // 情况二int result2 = robRange(nums, 1, nums.size() - 1); // 情况三return max(result1, result2);}int robRange(vector<int>& nums, int start, int end) {if (end == start) return nums[start];vector<int> dp(nums.size());dp[start] = nums[start];dp[start + 1] = max(nums[start], nums[start + 1]);for (int i = start + 2; i <= end; i++) {dp[i] = max(dp[i - 2] + nums[i], dp[i - 1]);}return dp[end];}
};

LeetCode 137 打家劫舍III


直接用简单版递归会超时

class Solution {public int innerRob(int state, TreeNode root) {if (root == null) return 0;if (state == 1) {return (innerRob(0, root.left) + innerRob(0, root.right));} else {return Math.max(root.val + innerRob(1, root.left) + innerRob(1, root.right), innerRob(0, root.left) + innerRob(0, root.right));}}public int rob(TreeNode root) {if (root == null) return 0;return Math.max(root.val + innerRob(1, root.left) + innerRob(1, root.right), innerRob(0, root.left) + innerRob(0, root.right));}
}

用哈希表记录下重复子问题就没问题了

class Solution {Map<TreeNode, Integer> choose = new HashMap<TreeNode, Integer>();Map<TreeNode, Integer> not_choose = new HashMap<TreeNode, Integer>();public void dfs (TreeNode root) {if (root == null) return;dfs(root.left);dfs(root.right);choose.put(root, root.val + not_choose.getOrDefault(root.left,0) + not_choose.getOrDefault(root.right,0));not_choose.put(root,Math.max(Math.max(choose.getOrDefault(root.left,0) + choose.getOrDefault(root.right,0), not_choose.getOrDefault(root.left,0) + not_choose.getOrDefault(root.right,0)),Math.max(choose.getOrDefault(root.left,0) + not_choose.getOrDefault(root.right,0), not_choose.getOrDefault(root.left,0) + choose.getOrDefault(root.right,0))));}public int rob(TreeNode root) {dfs(root);return Math.max(choose.getOrDefault(root,0), not_choose.getOrDefault(root,0));}
}

优化后代码如下:

class Solution {Map<TreeNode, Integer> choose = new HashMap<TreeNode, Integer>();Map<TreeNode, Integer> not_choose = new HashMap<TreeNode, Integer>();public void dfs (TreeNode root) {if (root == null) return;dfs(root.left);dfs(root.right);choose.put(root, root.val + not_choose.getOrDefault(root.left,0) + not_choose.getOrDefault(root.right,0));not_choose.put(root,Math.max(choose.getOrDefault(root.left,0), not_choose.getOrDefault(root.left,0)) + Math.max(choose.getOrDefault(root.right,0), not_choose.getOrDefault(root.right,0)));}public int rob(TreeNode root) {dfs(root);return Math.max(choose.getOrDefault(root,0), not_choose.getOrDefault(root,0));}
}

http://www.lryc.cn/news/364447.html

相关文章:

  • linux学习:进程通信 管道
  • 重大变化,2024软考!
  • DRIVEN|15分的CNN+LightGBM怎么做特征分类,适用于转录组
  • react 怎样配置ant design Pro 路由?
  • DBSCAN 算法【python,机器学习,算法】
  • MySQL之查询性能优化(六)
  • 生成树协议STP(Spanning Tree Protocol)
  • 03-3.1.1 栈的基本概念
  • 排序算法集合
  • pdf文件太大如何变小,苹果电脑压缩pdf文件大小工具软件
  • vite项目打包,内存溢出
  • Matlab解决施密特正交规范化矩阵(代码开源)
  • 自养号测评助力:如何打造沃尔玛爆款?
  • C语言编译与链接
  • 电子电器架构 --- 智能座舱技术分类
  • 提供操作日志、审计日志解决方案思路
  • 选择富唯智能的可重构装配系统,就是选择了一个可靠的合作伙伴
  • echarts tooltip太多显示问题解决方案
  • 【control_manager】无法加载,gazebo_ros2_control 0.4.8,机械臂乱飞
  • 深入对比:Transformer与LSTM的详细解析
  • lsof 命令
  • F5G城市光网,助力“一网通城”筑基数字中国
  • Ownips+Coze海外社媒数据分析实战指南
  • C#操作MySQL从入门到精通(10)——对查询数据进行通配符过滤
  • 厘米级精确定位,开启定位技术新时代
  • docker 存储 网络 命令
  • 【MATLAB源码-第222期】基于matlab的改进蚁群算法三维栅格地图路径规划,加入精英蚁群策略。包括起点终点,障碍物,着火点,楼梯。
  • 百度ERNIE系列预训练语言模型浅析(4)-总结篇
  • Ubuntu 20.04 LTS配置JDK、Git
  • 外汇天眼:Marqeta加速欧洲业务发展,华沙办公室正式开幕