当前位置:网站首页>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];
}
}第一种方法可以省点内存。
边栏推荐
- [solved] sqlexception: invalid value for getint() - 'Tian Peng‘
- Docker builds MySQL: the specified path of version 5.7 cannot be mounted.
- SharePoint modification usage analysis report is more than 30 days
- JS monitors empty objects and empty references
- Raspberry pie update tool chain
- Dora (discover offer request recognition) process of obtaining IP address
- Homology policy / cross domain and cross domain solutions /web security attacks CSRF and XSS
- [most detailed] latest and complete redis interview book (50)
- The embodiment of generics in inheritance and wildcards
- Basic knowledge about SQL database
猜你喜欢

Deep learning parameter initialization (I) Xavier initialization with code

Inno Setup 制作安装包

Selenium key knowledge explanation

Mise en place d'un environnement de développement de fonctions personnalisées

“百度杯”CTF比赛 2017 二月场,Web:爆破-1

POI excel percentage

3311. 最长算术

dataworks自定義函數開發環境搭建

691. Cube IV

New stills of Lord of the rings: the ring of strength: the caster of the ring of strength appears
随机推荐
CentOS switches and installs mysql5.7 and mysql8.0
Advanced API (use of file class)
你开发数据API最快多长时间?我1分钟就足够了
Jeecg request URL signature
POI excel percentage
Flask Foundation
[attribute comparison] defer and async
Chrome 98 Private Network Access problem w/ disabled web security: Request had no target IP address
Interview questions about producers and consumers (important)
Topic | synchronous asynchronous
Distributed ID
The difference between typescript let and VaR
docker建立mysql:5.7版本指定路径挂载不上。
萬卷書 - 價值投資者指南 [The Education of a Value Investor]
4everland: the Web3 Developer Center on IPFs has deployed more than 30000 dapps!
Visit Google homepage to display this page, which cannot be displayed
2. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log
Distributed transactions
CentOS php7.3 installing redis extensions
La différence entre le let Typescript et le Var