当前位置:网站首页>Topic5——198. 打家劫舍
Topic5——198. 打家劫舍
2022-04-22 18:01:00 【_卷心菜_】
题目:你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。
给定一个代表每个房屋存放金额的非负整数数组,计算你 不触动警报装置的情况下 ,一夜之内能够偷窃到的最高金额。
示例 1:
输入:[1,2,3,1]
输出:4
解释:偷窃 1 号房屋 (金额 = 1) ,然后偷窃 3 号房屋 (金额 = 3)。
偷窃到的最高金额 = 1 + 3 = 4 。
示例 2:
输入:[2,7,9,3,1]
输出:12
解释:偷窃 1 号房屋 (金额 = 2), 偷窃 3 号房屋 (金额 = 9),接着偷窃 5 号房屋 (金额 = 1)。
偷窃到的最高金额 = 2 + 9 + 1 = 12 。
提示:
1 <= nums.length <= 100
0 <= nums[i] <= 400
方法一:递归,自顶向下的方式
class Solution {
public int rob(int[] nums) {
int[] memo = new int[nums.length]; //存储nums[i...nums.length-1]的最大收益
Arrays.fill(memo, -1);
return selectHouse(nums, 0, memo);
}
private int selectHouse(int[] nums, int index, int[] memo) {
if(index >= nums.length)
return 0;
if(memo[index] != -1)
return memo[index];
for(int i = index; i < nums.length; i++) {
memo[index] = Math.max(memo[index], nums[i] + selectHouse(nums, i+2, memo));
}
return memo[index];
}
}
方法二:动态规划,自底向上的方式
- 考虑偷取[0~i]范围里的房子
class Solution {
public int rob(int[] nums) {
if(nums.length == 1)
return nums[0];
int[] memo = new int[nums.length]; //存储nums[0...i]的最大收益
memo[0] = nums[0];
if(nums.length >= 2)
memo[1] = Math.max(nums[1], nums[0]);
for(int i = 2; i < nums.length; i ++) {
memo[i] = Math.max(memo[i - 1], nums[i] + memo[i - 2]);
}
return memo[nums.length - 1];
}
}
- 考虑偷取[i~nums.length-1]范围里的房子
class Solution {
public int rob(int[] nums) {
int[] memo = new int[nums.length]; //存储nums[i...nums.length-1]的最大收益
Arrays.fill(memo, -1);
for(int i = nums.length - 1; i >= 0; i--) {
for(int j = i; j < nums.length; j++) {
memo[i] = Math.max(memo[i], nums[j] + (j + 2 < nums.length ? memo[j + 2] : 0));
}
}
return memo[0];
}
}
版权声明
本文为[_卷心菜_]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Thumb_/article/details/124339516
边栏推荐
- Continuously effective risk indicator: turbulence index
- 软考高项笔记 | PERT 三点估算
- golang-gin-websocket问题
- SAP ABAP FOR ALL ENTRIES 的用法
- What data indicators should app focus on?
- 还弄不懂相对路径和绝对路径,这篇文章带你简单剖析
- Go operation MySQL
- Notes on soft test high items | contents of detailed feasibility study
- L1-025 positive integer a + B (15 points)
- Future direction of digital shooting range
猜你喜欢

通过反射获得方法的参数实际名称

National information exchange model (NIEM) operation manual

Soft test high item notes | demand classification
![B树[概念]](/img/de/e52df3061a1615291de9b29c7b5c5c.png)
B树[概念]

Unable to translate SQLException with Error code ‘0‘, will now try the fallback translator

软考高项笔记 | 需求分类

Design the test paper storage scheme of ten million students management system

Spacy first routine (automatic annotation of Chinese text)

阿里云容器&服务网格产品技术动态(202203)

Soft test advanced item notes | software integration technology
随机推荐
2021年资产管理与托管银行行业发展研究报告
软考高项笔记 | 国家信息化体系六要素
Interface test mock practice (II) | complete batch manual mock in combination with JQ
通过反射获得方法的参数实际名称
Notes on soft test high items | six elements of national information system
接口协议之抓包分析 TCP 协议
设计千万级学生管理系统的考试试卷存储方案
Vscode + PHP debug + namespace guidelines
Soft test high item notes | PERT three-point estimation
Continuously effective risk indicator: turbulence index
Multithreaded notes | future interface function
Dpdk obtains and parses packets from ring queue and uses hashtable statistics
L1-025 positive integer a + B (15 points)
软考高项笔记 | 软件集成技术
软考高项笔记 | 项目的特点
"Programmer's life extension guide": follow the code farmer and live 20 more years!
An idea plug-in that doesn't work, but can install X
软考高项笔记 | 项目进度管理
软考高项笔记 | 项目评估的依据
如何彻底删除电脑上的文件?