当前位置:网站首页>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 .
边栏推荐
- I. D3.js hello world
- Final, override, polymorphism, abstraction, interface
- 20220319
- Common analysis with criteria method
- 4279. Cartesian tree
- 《指环王:力量之戒》新剧照 力量之戒铸造者亮相
- Various postures of CS without online line
- New stills of Lord of the rings: the ring of strength: the caster of the ring of strength appears
- The underlying mechanism of advertising on websites
- docker建立mysql:5.7版本指定路径挂载不上。
猜你喜欢
Leetcode 198: 打家劫舍
691. Cube IV
昇思MindSpore再升级,深度科学计算的极致创新
Wireshark software usage
Various postures of CS without online line
【已解决】SQLException: Invalid value for getInt() - ‘田鹏‘
JS monitors empty objects and empty references
1. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log
TCP cumulative acknowledgement and window value update
3311. Longest arithmetic
随机推荐
TCP cumulative acknowledgement and window value update
2. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log
Leetcode 213: 打家劫舍 II
[plus de détails] dernière entrevue complète redis (50)
Topic | synchronous asynchronous
【最详细】最新最全Redis面试大全(50道)
Sorting, dichotomy
Advanced API (local simulation download file)
SecureCRT password to cancel session recording
2021-07-18
Vertx's responsive redis client
Raspberry pie update tool chain
I. D3.js hello world
pgAdmin 4 v6.11 发布,PostgreSQL 开源图形化管理工具
Jeecg menu path display problem
Common problems in io streams
LeetCode
PdfWriter. GetInstance throws system Nullreferenceexception [en] pdfwriter GetInstance throws System. NullRef
Summary of abnormal mechanism of interview
Various postures of CS without online line