当前位置:网站首页>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]);
}
}
边栏推荐
- Go语言学习教程(十五)
- FBO and RBO disappeared in webgpu
- opencv 判断点在多边形内外
- Global and Chinese markets of tantalum heat exchangers 2022-2028: Research Report on technology, participants, trends, market size and share
- FBO and RBO disappeared in webgpu
- Promql demo service
- Platformio create libopencm3 + FreeRTOS project
- 傅里叶分析概述
- Understand the basic concept of datastore in Android kotlin and why SharedPreferences should be stopped in Android
- The countdown to the launch of metaverse ape is hot
猜你喜欢
Starting from 1.5, build a micro Service Framework -- log tracking traceid
Event trigger requirements of the function called by the event trigger
Three "factions" in the metauniverse
Analysis of the problem that the cookie value in PHP contains a plus sign (+) and becomes a space
How to quickly experience oneos
Metaverse Ape获Negentropy Capital种子轮融资350万美元
关于MySQL的30条优化技巧,超实用
Golang writes the opening chapter of selenium framework
傅里叶分析概述
分布式解决方案之TCC
随机推荐
傅里叶分析概述
The introduction to go language is very simple: String
I closed the open source project alinesno cloud service
Spectrum analysis of ADC sampling sequence based on stm32
[error record] file search strategy in groovy project (src/main/groovy/script.groovy needs to be used in the main function | groovy script directly uses the relative path of code)
Event trigger requirements of the function called by the event trigger
航海日答题小程序之航海知识竞赛初赛
a-tree 树的全部展开和收起
抖音__ac_signature
The difference between MVVM and MVC
我对新中台模型的一些经验思考总结
First, redis summarizes the installation types
Three "factions" in the metauniverse
H5c3 advanced - player
Evolution of APK reinforcement technology, APK reinforcement technology and shortcomings
Matlab draws a cute fat doll
Metaverse Ape猿界应邀出席2022·粤港澳大湾区元宇宙和web3.0主题峰会,分享猿界在Web3时代从技术到应用的文明进化历程
Request preview display of binary data and Base64 format data
Exponential weighted average and its deviation elimination
How to create a thread