当前位置:网站首页>Li Kou 198-213 looting Ⅰ, Ⅱ - Dynamic Planning
Li Kou 198-213 looting Ⅰ, Ⅱ - Dynamic Planning
2022-07-19 13:03:00 【Tension √】
198— raid homes and plunder houses ( One )
Title Description
You are a professional thief , Plan to steal houses along the street . There is a certain amount of cash in every room , The only restriction on your theft is that adjacent houses are equipped with interconnected anti-theft systems , If two adjacent houses are broken into by thieves on the same night , The system will automatically alarm .
Given an array of non negative integers representing the storage amount of each house , Count you Without triggering the alarm , The maximum amount that can be stolen overnight .
Their thinking
- Because adjacent rooms cannot be stolen continuously , So use interval theft ;
- You need to choose the one with high theft amount to steal , Finally, you can steal a relatively large amount ;
- In the k In front of a house , Faced with two situations :
- If you steal the first k A house , that k-1 A house cannot be stolen , At this time, the maximum amount should be k-2 The maximum amount of plus the current number k The wealth of a house ;
- If you don't steal the first k A house , that At this time, the maximum amount should be k-1 The maximum amount of .
- So in front of every house , We have two options , The maximum amount we can get should be the larger of these two cases ;
- therefore State transition equation It can be written as :
Put a set of official schematic diagram of force buckle , It is convenient for us to understand the meaning of the question :
I / O example
Code
You need to create two pointers to represent the maximum amount of the first two houses of the current house , Then dynamically update the two pointer variables , It can also be understood as dynamic interval method .
class Solution {
public int rob(int[] nums) {
int len = nums.length;
int sum = 0;
int p = 0;// Record house nums[i- 2] Status of the maximum amount stolen ;
int q = 0;// Record house nums[i- 1] Status of the maximum amount stolen ;
for(int i = 0; i < len; i++ ){
sum = Math.max(p+nums[i], q);// Record the current house nums[i] Status of the maximum amount stolen ;
p = q;
q = sum;
}
return q;
}
}
213— raid homes and plunder houses ( Two )
Title Description
You are a professional thief , Plan to steal houses along the street , There is a certain amount of cash in every room . All the houses in this place are Make a circle , This means that the first house and the last house are next to each other . meanwhile , Adjacent houses are equipped with interconnected anti-theft system , If two adjacent houses are broken into by thieves on the same night , The system will automatically alarm .
Given an array of non negative integers representing the storage amount of each house , Count you Without triggering the alarm device , The maximum amount you can steal tonight .
Their thinking
- In fact, the ideas of these two topics are similar , But this problem assumes that all houses are connected in a closed loop ;
- Then we can understand it as : If you steal the head, you can't steal the tail , If you steal the tail, you can't steal the first ;
- So we can split the original circular array into two arrays , One is 0 ——n - 1, The other is 1—— n ;
- Then choose the solution of the problem with the greater result among the two schemes .
I / O example
Code
The code may be a little more streamlined ~
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);
}
}
边栏推荐
- 【Pygame 学习笔记】6.Cursor 鼠标光标
- Preparation Notes: Matplotlib learning notes a
- Is the career direction of test / development programmers over 35 a turning point in the workplace?
- Three minutes to understand the primary key, foreign key, non empty, unique and default constraints in mysql, and how to create a table
- If you merge cells by Excel, export excelutils
- 竞赛笔记:Numpy学习笔记
- The combination of fastadmin with and filed causes field failure
- Nombre minimal d'échanges
- LeetCode 0118. 杨辉三角
- [C language programming 7] BTB model
猜你喜欢
AI is the designer who knows you best? Let users generate digital clothing "by heart" Adidas ozworld
C语言进阶——自定义类型:结构体 枚举 联合
最懂你的服装设计师是AI?让用户 “凭心意” 生成数字服装#Adidas OZWORLD
Yunxi and Tencent cloud have reached a strategic cooperation to accelerate the expansion of the global live broadcast market
市场“不确定性”中的投资逻辑 2020-03-18
Yu Meimei, Ji Gongdu
Is the career direction of test / development programmers over 35 a turning point in the workplace?
【错误记录/selectpicker】dropdown menu显示位置出现偏移
Yunxi focuses on store broadcast solutions to accelerate global layout
Ultrasonic sensor (ch101 & ch201) - Ⅱ
随机推荐
In depth sorting: summary of machine learning modeling and parameter adjustment methods
Differences between get requests and post requests and usage examples
通货收缩的市场何时反转?我们该如何操作?2020-03-13
Acwing786. The kth number
Att & CK actual combat series - red team actual combat (-)
C语言进阶——自定义类型:结构体 枚举 联合
JS operation string string string
Mycat2 builds MySQL master-slave separation
深度梳理:机器学习建模调参方法总结
懒到骨子里了,我在CSDN写文章都懒得自己写了,基于selenium模拟写文章
音频控制常见BUG注意事项
Will the market halve? What investment opportunities are there? 2020-03-11
[try to hack] ARP and ARP deception
Uio-66 | fe3o4/cu3 (BTC) 2 metal organic framework (MOF) nanocomposites supported on silver nanoparticles | nagdf4:yb, er upconversion nanoparticles @zif-8
Ultrasonic sensor (ch101 & ch201) - Ⅱ
二叉树2—对称性递归问题
jvm自学总结
虞美人·寄公度
回顾2008年金融危机,做长期主义投资者 2020-03-19
Ultrasonic sensor (ch101 & ch201) - I