LeetCode 213. House Robber II
Note: This is an extension of House Robber.
After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, the security system for these houses remain the same as for those in the previous street.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
【题目分析】
在House Robber的基础上,这个题目增加的一个限制就是所有的房屋构成了一个环,在这种情况下保证任不偷盗任意两间相邻的房子。
【思路】
看了别人的好多解法,其实说的都不明不白,我把我的想法分享给大家,肯定让你豁然开朗。
首先:如果房屋不构成一个圈,我们的解法有如下几种可能
1. 头部和尾部的房屋都被抢劫

2. 头部房屋被抢劫,尾部房屋没有被抢劫

3. 头部房屋没有被抢劫,尾部的房屋被抢劫

4. 头部和尾部的房屋都没有被抢劫

如果房屋形成一个环,我们的最优解就只能选择后面的三种情况,第二种情况我们要保证最后一个房屋不能被抢劫,第三种情况要保证第一个房屋不能被抢劫,第四种情况已经包含在前两种情况中了。其实就是在环的连接处保证其中一个不被偷的情况下,求出从剩下的房屋中最多能盗取的金钱数目。
因此在House Robber的基础上我们只要保证结果只能为上面第二和第三种情况即可。代码如下:
public class Solution {
public int rob(int[] nums) {
int len = nums.length;
if(len == 0) return 0;
if(len == 1) return nums[0];
int post2 = nums[len-1];
int post1 = Math.max(nums[len-1], nums[len-2]);
int lable1 = 0;
//保证为第一个房屋不被偷
for(int i = len-3; i >= 1; i--) {
int temp = post1;
post1 = Math.max(post1, nums[i] + post2);
post2 = temp;
}
int result1 = post1;
post2 = 0;
post1 = nums[len-2];
//保证最后一个房屋不被偷
for(int i = len-3; i >= 0; i--) {
int temp = post1;
post1 = Math.max(post1, nums[i] + post2);
post2 = temp;
}
int result2 = post1;
return Math.max(result1, result2);
}
}
LeetCode 213. House Robber II的更多相关文章
- [LeetCode] 213. House Robber II 打家劫舍 II
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
- [LeetCode] 213. House Robber II 打家劫舍之二
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- Java for LeetCode 213 House Robber II
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
- [leetcode] #213 House Robber II Medium (medium)
原题链接 比子母题House Robber多了一个条件:偷了0以后,第n-1间房子不能偷. 转换思路为求偷盗[0,n-1)之间,以及[1,n)之间的最大值. 用两个DP,分别保存偷不偷第0间房的情况. ...
- leetcode 198. House Robber 、 213. House Robber II 、337. House Robber III 、256. Paint House(lintcode 515) 、265. Paint House II(lintcode 516) 、276. Paint Fence(lintcode 514)
House Robber:不能相邻,求能获得的最大值 House Robber II:不能相邻且第一个和最后一个不能同时取,求能获得的最大值 House Robber III:二叉树下的不能相邻,求能 ...
- 198. House Robber,213. House Robber II
198. House Robber Total Accepted: 45873 Total Submissions: 142855 Difficulty: Easy You are a profess ...
- 【LeetCode】213. House Robber II
House Robber II Note: This is an extension of House Robber. After robbing those houses on that stree ...
- 【刷题-LeetCode】213. House Robber II
House Robber II You are a professional robber planning to rob houses along a street. Each house has ...
- 【LeetCode】213. House Robber II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/house-rob ...
随机推荐
- Apache HttpServer Installing the apache2.2 service <OS 5>拒绝访问. :Failed to open the WinNT service manager
Installing the apache2.2 service<OS 5>拒绝访问. :Failed to open the WinNT service manager 只需要于管理员 ...
- Django ORM 查询管理器
Django ORM 查询管理器 ORM 查询管理器 对于 ORM 定义: 对象关系映射, Object Relational Mapping, ORM, 是一种程序设计技术,用于实现面向对象编程语言 ...
- 【JS】布尔逻辑
0 是逻辑的 false 1 是逻辑的 true 空字符串是逻辑的 false null 是逻辑的 false NaN 是逻辑的 false 字符串 'false' 是逻辑的 true Boolean ...
- highlight高亮
玩转正则之highlight高亮 2013-10-07 05:16 by 靖鸣君, 584 阅读, 3 评论, 收藏, 编辑 程序员在编写代码的时候少不了和字符串以及“查询”打交道,两者的交集中有一个 ...
- 使用PetaPoco结合注入实现业务级事务
使用PetaPoco结合注入实现业务级事务 PetaPoco是一个轻量级ORM,我的MVC项目中使用它结合Repository模式,依靠Unity的生命周期管理对象,保证请求/线程级别的数据上下文 ...
- IceMx.Mvc 我的js MVC 框架七、完善植物大战僵尸(增加阳光的消费和获得)
话不多说,先上图 这次增加了阳光的消费和获得,增加了阳光的点击动画 重新排布了布局 有兴趣的话就研究下吧. 上一篇有朋友说让我把项目放到github上面维护,本人没用过这个,肯请朋友们帮小弟科普一下放 ...
- 一键搭键php网站环境的系统
QzzmServer v2.0正式版发布 首先,感谢网友的热情的测评及反馈,现QzzmServer 2.0正式版已发布.有些朋友反馈制作一个服务器专用版本,在下已将此列入计划中,敬请大家耐心等候. Q ...
- ASP.NET Web API的消息处理管道:"龙头"HttpServer
ASP.NET Web API的消息处理管道:"龙头"HttpServer 一般来说,对于构成ASP.NET Web API消息处理管道的所有HttpMessageHandler来 ...
- Entity Framework 之 Code First
使用NuGet助您玩转代码生成数据————Entity Framework 之 Code First [前言] 如果是Code First老鸟或者对Entity Framework不感兴趣,就不用浪费 ...
- IE11仿真文档模式默认IE5 IE7的调整办法
<meta http-equiv="X-UA-Compatible" content="IE=edge">