当前位置:网站首页>Leetcode 213: looting II
Leetcode 213: looting II
2022-07-03 07:28:00 【Zhu Ya who can learn】
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 .

Ideas
This topic and 198. raid homes and plunder houses It's almost the same , The only difference is that it's a ring .
For an array , There are three main situations when forming a ring :
- Situation 1 : Consider not including leading and trailing elements

- Situation two : Consider including the first element , No tail elements

- Situation three : Consider including tail elements , Does not contain the first element

Notice that I'm using " consider ", For example, case 3 , Although it is considered to include tail elements , But you don't have to choose the tail element ! For case three , take nums[1] and nums[3] Is the biggest .
And case two and Situation three It includes case one , So just consider case 2 and case 3 .
So that's the analysis , This question is actually relatively simple .
class Solution {
public int rob(int[] nums) {
if (nums == null || nums.length == 0)
return 0;
int len = nums.length;
if (len == 1)
return nums[0];
if(len ==2)
return Math.max(nums[0],nums[1]);
return Math.max(robAction(nums, 0, len - 2), robAction(nums, 1, len-1));
}
int robAction(int[] nums, int start, int end) {
int first = nums[start], second = Math.max(nums[start], nums[start + 1]);
for (int i = start + 2; i <= end; i++) {
int temp = second;
second = Math.max(first + nums[i], second);
first = temp;
}
return second;
// int[] dp = new int[nums.length];
// dp[start] = nums[start];
// dp[start + 1] = Math.max(nums[start], nums[start + 1]);
// for (int i = start + 2; i <= end; i++) {
// dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]);
// }
// return dp[end];
}
}The first method can save some memory .
边栏推荐
- Vertx's responsive redis client
- Advanced API (UDP connection & map set & collection set)
- sharepoint 2007 versions
- Dora (discover offer request recognition) process of obtaining IP address
- HCIA notes
- Comparison of advantages and disadvantages between most complete SQL and NoSQL
- [untitled]
- La différence entre le let Typescript et le Var
- Recursion, Fibonacci sequence
- VMware network mode - bridge, host only, NAT network
猜你喜欢

《指環王:力量之戒》新劇照 力量之戒鑄造者亮相

Sorting, dichotomy

【开发笔记】基于机智云4G转接板GC211的设备上云APP控制

Qtip2 solves the problem of too many texts

File operation serialization recursive copy

昇思MindSpore再升级,深度科学计算的极致创新

Map interface and method

7.2 brush two questions

带你全流程,全方位的了解属于测试的软件事故
![[set theory] Stirling subset number (Stirling subset number concept | ball model | Stirling subset number recurrence formula | binary relationship refinement relationship of division)](/img/d8/b4f39d9637c9886a8c81ca125d6944.jpg)
[set theory] Stirling subset number (Stirling subset number concept | ball model | Stirling subset number recurrence formula | binary relationship refinement relationship of division)
随机推荐
HISAT2 - StringTie - DESeq2 pipeline 进行bulk RNA-seq
2021-07-18
“百度杯”CTF比赛 2017 二月场,Web:爆破-1
Chapter VI - Containers
为什么说数据服务化是下一代数据中台的方向?
PAT甲级真题1166
不出网上线CS的各种姿势
Sorting, dichotomy
Advanced API (UDP connection & map set & collection set)
Understanding of class
[solved] unknown error 1146
Interview questions about producers and consumers (important)
VMWare网络模式-桥接,Host-Only,NAT网络
Margin left: -100% understanding in the Grail layout
Docker builds MySQL: the specified path of version 5.7 cannot be mounted.
Custom generic structure
Realize the reuse of components with different routing parameters and monitor the changes of routing parameters
Web router of vertx
【已解决】win10找不到本地组策略编辑器解决方法
Summary of abnormal mechanism of interview