当前位置:网站首页>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]);
}
}
边栏推荐
- Character conversion PTA
- Global and Chinese markets of tantalum heat exchangers 2022-2028: Research Report on technology, participants, trends, market size and share
- Nanjing: full use of electronic contracts for commercial housing sales
- IIC bus realizes client device
- Nangou Gili hard Kai font TTF Download with installation tutorial
- Text组件新增内容通过tag_config设置前景色、背景色
- The countdown to the launch of metaverse ape is hot
- Nail error code Encyclopedia
- 二叉树(三)——堆排序优化、TOP K问题
- What about data leakage? " Watson k'7 moves to eliminate security threats
猜你喜欢
随机推荐
Metaverse Ape猿界应邀出席2022·粤港澳大湾区元宇宙和web3.0主题峰会,分享猿界在Web3时代从技术到应用的文明进化历程
二叉树(二)——堆的代码实现
H5c3 advanced - player
70. Climbing Stairs. Sol
【无标题】
Methods modified by static
Postman core function analysis - parameterization and test report
Business introduction of Zhengda international futures company
Draw a red lantern with MATLAB
Request preview display of binary data and Base64 format data
Unity Max and min constraint adjustment
第一讲:蛇形矩阵
FBO and RBO disappeared in webgpu
What if the files on the USB flash disk cannot be deleted? Win11 unable to delete U disk file solution tutorial
Golang writes the opening chapter of selenium framework
Binary tree (III) -- heap sort optimization, top k problem
Record several frequently asked questions (202207)
记录几个常见问题(202207)
实战:fabric 用户证书吊销操作流程
What changes has Web3 brought to the Internet?








![Sparse array [matrix]](/img/62/27b02deeeaa5028a16219ef51ccf82.jpg)
