当前位置:网站首页>力扣198-213 打家劫舍Ⅰ、Ⅱ——动态规划
力扣198-213 打家劫舍Ⅰ、Ⅱ——动态规划
2022-07-17 17:15:00 【张怼怼√】
198—打家劫舍(一)
题目描述
你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。
给定一个代表每个房屋存放金额的非负整数数组,计算你 不触动警报装置的情况下 ,一夜之内能够偷窃到的最高金额。
解题思路
- 因为相邻房间不能连续偷盗,所以采用间隔偷盗;
- 需要选择偷盗金额高的进行偷盗,最终可以偷盗到比较大的金额;
- 在第 k 个房屋面前,面临两种情况:
- 如果偷第 k 间房屋,那么 k-1 间房屋就不能进行偷窃,这时的最大金额应该是 k-2 的最大金额加上当前第 k 间房屋的财富;
- 如果不偷第 k 间房屋,那么这时的最大金额应该是 k-1 的最大金额。
- 所以在每个房屋面前,我们有两种选择,我们所获得的最大金额应该在这两种情况中取较大值;
- 所以状态转移方程就可以写为:

放一组力扣官方的示意图,方便我们理解题意:






输入输出示例

代码
需要创建两个指针来表示当前房屋的前面两个房屋的最大金额,然后动态更新两个指针变量,也可以理解为动态区间法。
class Solution {
public int rob(int[] nums) {
int len = nums.length;
int sum = 0;
int p = 0;//记录房屋 nums[i- 2] 状态的偷窃到的最大金额;
int q = 0;//记录房屋 nums[i- 1] 状态的偷窃到的最大金额;
for(int i = 0; i < len; i++ ){
sum = Math.max(p+nums[i], q);//记录房屋当前 nums[i] 状态的偷窃到的最大金额;
p = q;
q = sum;
}
return q;
}
}213—打家劫舍(二)
题目描述
你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金。这个地方所有的房屋都 围成一圈 ,这意味着第一个房屋和最后一个房屋是紧挨着的。同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警 。
给定一个代表每个房屋存放金额的非负整数数组,计算你 在不触动警报装置的情况下 ,今晚能够偷窃到的最高金额。
解题思路
- 这两道题目其实思路是差不多的,只是这道题目假设所有的房屋是连在一起闭环的;
- 那么我们就可以理解为:偷首就不能偷尾,偷尾就不能偷首;
- 所以我们就可以把原来的环状数组拆分为两个数组,一条是 0 ——n - 1,另一条是 1—— n ;
- 然后在两种方案里面选择结果较大的就是此题的解。

输入输出示例

代码
代码或许可以再精简一下~
class Solution {
public int rob(int[] nums) {
if(nums.length == 1)return nums[0];
int p1 = 0;
int q1 = 0;
for(int i = 0; i < nums.length-1; i++){
int sum1 = Math.max(p1 + nums[i],q1);
p1 = q1;
q1 = sum1;
}
int p2 = 0;
int q2 = 0;
for(int i = 1; i < nums.length; i++){
int sum2 = Math.max(p2 + nums[i],q2);
p2 = q2;
q2 = sum2;
}
return Math.max(q1,q2);
}
}边栏推荐
- 云犀&腾讯云达成战略合作,加速拓展全球直播市场
- 10 minutes to customize the pedestrian analysis system, detection and tracking, behavior recognition, human attributes all in one!
- Impulse function, step function, ramp function and impulse response
- Possible problems in inserting Excel data into MySQL database
- Interview difficulties: difficulties in implementing distributed session, this is enough!
- Do you still need to release the database connection manually with typeorm
- Uio-66 | fe3o4/cu3 (BTC) 2 metal organic framework (MOF) nanocomposites supported on silver nanoparticles | nagdf4:yb, er upconversion nanoparticles @zif-8
- Harmonyos quick start: Hello World
- 又错了,字节对齐及#pragma pack的使用
- 506. Relative ranking
猜你喜欢

2022 global developer salary exposure: China ranks 19th, with an average annual salary of $23790

2022全球开发者薪资曝光:中国排第19名,平均年薪23,790美元

MOF customized materials | ultra thin MOF nanobelts | magnetic Fe3O4 @cd-mof nanocomposites | zif-8/ graphene composite nanoparticles

Acwing4405. 统计子矩阵

Structure memory alignment, bit segment, union

Lazy to the bone, I am too lazy to write articles in CSDN. I write articles based on selenium simulation

Li Hongyi machine learning 1 Introduction of this course

关于TCP/IP协议漏洞的安全措施

Cloud health management system based on STM32 (using Alibaba cloud Internet of things platform)

Basic database operations in MySQL
随机推荐
Array de duplication array sorting maximum sum class array conversion filter array
Competition notes: numpy learning notes
最懂你的服装设计师是AI?让用户 “凭心意” 生成数字服装#Adidas OZWORLD
10 minutes to customize the pedestrian analysis system, detection and tracking, behavior recognition, human attributes all in one!
Amino metal organic framework material Fe MOF, fe-mil-88nh2 | Zr based metal organic framework catalyst (pt-uio-66) | Qiyue biology
When will the deflationary market reverse? How should we operate? 2020-03-13
Learning record: call TFTLCD
Harmonyos quick start: Hello World
Do you still need to release the database connection manually with typeorm
Gold nanoparticles modified mil-101 framework material (AuNPs / mil-101) / loaded cof-tppa-1 (Au NPs / cof-tppa-1) | Qiyue reagent
减半行情会不会来?有何投资机会?2020-03-11
LeetCode_前缀和_中等_523.连续的子数组和
2022 safety officer-c certificate title and answer
MYCAT divides the database and table according to the hash value of the string range
How can MySQL delete data tables and associated data tables
RingBuffer
整理了一份通用的内存管理驱动代码
Is the career direction of test / development programmers over 35 a turning point in the workplace?
Porphyrin encapsulated organometallic frame materials [email protected] |
Acwing786. 第k个数