当前位置:网站首页>2022.02.13 - SX10-30. Home raiding II
2022.02.13 - SX10-30. Home raiding II
2022-07-05 22:42:00 【A CAI continues to work hard】
List of articles
1. subject
2. Ideas
(1) Dynamic programming
- Because the first and last one can only choose one to steal , Another family must not steal , therefore , You can delete the first or last one directly , This breaks the ring , Carry out dynamic planning respectively , Take the larger of the two times .
3. Code
public class Test {
public static void main(String[] args) {
}
}
class Solution {
public int rob(int[] nums) {
int n = nums.length;
if (n == 1) {
return nums[0];
}
if (n == 2) {
return Math.max(nums[0], nums[1]);
}
int[] dp = new int[n];
dp[0] = nums[0];
dp[1] = Math.max(nums[0], nums[1]);
for (int i = 2; i < n - 1; i++) {
dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]);
}
int temp = dp[n - 2];
dp[1] = nums[1];
dp[2] = Math.max(nums[1], nums[2]);
for (int i = 3; i < n; i++) {
dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]);
}
return Math.max(temp, dp[n - 1]);
}
}
边栏推荐
- 我把开源项目alinesno-cloud-service关闭了
- VOT Toolkit环境配置与使用
- Lesson 1: serpentine matrix
- FBO and RBO disappeared in webgpu
- d3dx9_ How to repair 31.dll_ d3dx9_ 31. Solution to missing DLL
- Promql demo service
- Search: Future Vision (moving sword)
- [secretly kill little buddy pytorch20 days] - [Day2] - [example of picture data modeling process]
- Shelved in TortoiseSVN- Shelve in TortoiseSVN?
- GWT module may need to be (RE) compiled reduce - GWT module may need to be (RE) compiled reduce
猜你喜欢
Solutions for unexplained downtime of MySQL services
Exponential weighted average and its deviation elimination
南京:全面启用商品房买卖电子合同
Paddy serving v0.9.0 heavy release multi machine multi card distributed reasoning framework
d3dx9_ How to repair 31.dll_ d3dx9_ 31. Solution to missing DLL
[error record] groovy function parameter dynamic type error (guess: groovy.lang.missingmethodexception: no signature of method)
Metaverse Ape获Negentropy Capital种子轮融资350万美元
點到直線的距離直線的交點及夾角
2022软件测试工程师涨薪攻略,3年如何达到30K
实战:fabric 用户证书吊销操作流程
随机推荐
Unique occurrence times of leetcode simple questions
Analysis of the problem that the cookie value in PHP contains a plus sign (+) and becomes a space
How can easycvr cluster deployment solve the massive video access and concurrency requirements in the project?
二叉树(二)——堆的代码实现
Record several frequently asked questions (202207)
The countdown to the launch of metaverse ape is hot
Promql demo service
Thinkphp5.1 cross domain problem solving
Boring boring
30 optimization skills about mysql, super practical
How to create a thread
I closed the open source project alinesno cloud service
南京:全面启用商品房买卖电子合同
Why does the C# compiler allow an explicit cast between IEnumerable&lt; T&gt; and TAlmostAnything?
Matlab draws a cute fat doll
What about data leakage? " Watson k'7 moves to eliminate security threats
Post-90s tester: "after joining Ali, this time, I decided not to change jobs."
Kubernetes Administrator certification (CKA) exam notes (IV)
分布式解决方案之TCC
thinkphp5.1跨域问题解决