当前位置:网站首页>Leetcode 213: 打家劫舍 II
Leetcode 213: 打家劫舍 II
2022-07-03 07:21:00 【会学习的朱丫】
你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金。这个地方所有的房屋都 围成一圈 ,这意味着第一个房屋和最后一个房屋是紧挨着的。同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警 。
给定一个代表每个房屋存放金额的非负整数数组,计算你 在不触动警报装置的情况下 ,今晚能够偷窃到的最高金额。

思路
这道题目和198.打家劫舍是差不多的,唯一区别就是成环了。
对于一个数组,成环的话主要有如下三种情况:
- 情况一:考虑不包含首尾元素

- 情况二:考虑包含首元素,不包含尾元素

- 情况三:考虑包含尾元素,不包含首元素

注意我这里用的是"考虑",例如情况三,虽然是考虑包含尾元素,但不一定要选尾部元素! 对于情况三,取nums[1] 和 nums[3]就是最大的。
而情况二 和 情况三 都包含了情况一了,所以只考虑情况二和情况三就可以了。
分析到这里,本题其实比较简单了。
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];
}
}第一种方法可以省点内存。
边栏推荐
- Jeecg request URL signature
- Inno setup production and installation package
- Interfaces and related concepts
- [vscode - vehicle plug-in reports an error] cannot find module 'xxx' or its corresponding type declarations Vetur(2307)
- SecureCRT password to cancel session recording
- Deep learning parameter initialization (I) Xavier initialization with code
- docket
- [untitled]
- 在 4EVERLAND 上存储 WordPress 媒体内容,完成去中心化存储
- Circuit, packet and message exchange
猜你喜欢

New stills of Lord of the rings: the ring of strength: the caster of the ring of strength appears

POI excel percentage

Wireshark software usage

Win 10 find the port and close the port

Pits encountered in the use of El checkbox group

Basic components and intermediate components

Basic knowledge about SQL database
![[set theory] partition (partition | partition example | partition and equivalence relationship)](/img/f0/c3c82de52d563f3b81d731ba74e3a2.jpg)
[set theory] partition (partition | partition example | partition and equivalence relationship)

JMeter test result output
![PdfWriter. GetInstance throws system Nullreferenceexception [en] pdfwriter GetInstance throws System. NullRef](/img/65/1f28071fc15e76abb37f1b128e1d90.jpg)
PdfWriter. GetInstance throws system Nullreferenceexception [en] pdfwriter GetInstance throws System. NullRef
随机推荐
Wireshark software usage
Specified interval inversion in the linked list
Store WordPress media content on 4everland to complete decentralized storage
JMeter test result output
Split small interface
Resthighlevelclient gets the mapping of an index
"Baidu Cup" CTF game 2017 February, Web: blast-1
TypeScript let与var的区别
"Moss ma not found" solution
Pits encountered in the use of El checkbox group
Operation and maintenance technical support personnel have hardware maintenance experience in Hong Kong
Take you through the whole process and comprehensively understand the software accidents that belong to testing
File operation serialization recursive copy
Arduino Serial系列函数 有关print read 的总结
PAT甲级真题1166
[Fiddler actual operation] how to use Fiddler to capture packets on Apple Mobile Phones
【最詳細】最新最全Redis面試大全(50道)
Visit Google homepage to display this page, which cannot be displayed
JS monitors empty objects and empty references
Talk about floating