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的更多相关文章

  1. [LeetCode] 213. House Robber II 打家劫舍 II

    Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...

  2. [LeetCode] 213. House Robber II 打家劫舍之二

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  3. 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 ...

  4. [leetcode] #213 House Robber II Medium (medium)

    原题链接 比子母题House Robber多了一个条件:偷了0以后,第n-1间房子不能偷. 转换思路为求偷盗[0,n-1)之间,以及[1,n)之间的最大值. 用两个DP,分别保存偷不偷第0间房的情况. ...

  5. 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:二叉树下的不能相邻,求能 ...

  6. 198. House Robber,213. House Robber II

    198. House Robber Total Accepted: 45873 Total Submissions: 142855 Difficulty: Easy You are a profess ...

  7. 【LeetCode】213. House Robber II

    House Robber II Note: This is an extension of House Robber. After robbing those houses on that stree ...

  8. 【刷题-LeetCode】213. House Robber II

    House Robber II You are a professional robber planning to rob houses along a street. Each house has ...

  9. 【LeetCode】213. House Robber II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/house-rob ...

随机推荐

  1. C++ multimap容器访问同一键值元素的不同方法

    multimap是一种多元map容器,允许一个键对应多个值. 本文介绍了 multimap访问同一键值元素的三种不同方法,详细看下面代码: typedef multimap<string,int ...

  2. Memory Dump 分析器

    Visual Studio 2013 新功能 Memory Dump 分析器   TechEd2013 发现新功能 12月5日和6日,在国家会议中心参加了微软的 TechEd2013 技术大会,了解了 ...

  3. 新认识:SDF数据库

    新认识:SDF数据库 一.SDF数据库初探 SDF是一个标准缩略数据库格式.这个数据库包含扩展名为.sdf的文件并且以结构化文件格式进行数据存储.这些SDF文件通常用于在不同数据库应用之间移动数据.它 ...

  4. 使用celery之怎么让celery跑起来

    celery 官网帮助文档  http://docs.celeryproject.org/en/latest/index.html 前言 自从发了上次的文章使用celery之深入celery配置, 有 ...

  5. dapper 扩展插件: Rainbow

    dapper 扩展插件: Rainbow dapper 是一个效率非常高的orm  框架 ,效率要远远大于 我们大微软的EF .    它只有一个类文件,非常之小. 1,首先下载dapper  这里下 ...

  6. FastDFS php API

    <?php if (!class_exists('FastDFS', false)) { define('FDFS_PROTO_PKG_LEN_SIZE', 8); define('FDFS_P ...

  7. Make Things Move -- Javascript html5版(一)文件目录结构和工具方法准备

    从这一篇开始,就来开始我们的make things move之旅吧 在此之前,要知道ActionScript(AS)的语法和JS是不一样的,AS是相对于JS而言更好的支持了面向对象的特性,所以我们可以 ...

  8. Changing the working directory of VIM

    Sometimes we want to open another file in the same folder with current editing file, what we can do ...

  9. struts2文件上传大小限制问题

    struts2默认文件上传大小为2M,如需修改默认大小,解决方法如下: <struts> <constant name="struts.multipart.maxSize& ...

  10. IP地址分类(转)

    IP地址分类以及C类IP地址的子网划分 国际规定:把所有的IP地址划分为 A,B,C,D,E A类地址:范围从0-127,0是保留的并且表示所有IP地址,而127也是保留的地址,并且是用于测试环回用的 ...