题目:

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.

链接: http://leetcode.com/problems/house-robber-ii/

题解:

乍一看感觉比较棘手,于是去看了discuss。这个问题比较tricky,但想清楚以后就很简单。因为House 1和House n相连,所以我们要么rob House 1,要么rob House n,两者不可兼得。于是我们只要比较rob(nums, 0, n - 2)与rob(nuns,1, n - 1)这两个值就可以了,其他部分和House Rob基本一样,都是使用DP。 (和House Rob一起要好好思考如何构建辅助函数)

Time Complexity - O(n), Space Complexity - O(n)

public class Solution {
public int rob(int[] nums) {
if(nums == null || nums.length == 0)
return 0;
if(nums.length == 1)
return nums[0];
return Math.max(rob(nums, 0, nums.length - 1), rob(nums, 1, nums.length));
} private int rob(int[] nums, int lo, int hi) {
int pre = 0, prePre = 0, max = 0; for(int i = lo; i < hi; i++) {
if(i - 2 < lo)
prePre = 0;
if(i - 1 < lo)
pre = 0;
max = Math.max(nums[i] + prePre, pre);
prePre = pre;
pre = max;
} return max;
}
}

二刷:

二刷就比较顺了,就是第一个房子的取舍问题。我们可以建立一个辅助方法来决定我们dp的范围。

这里要注意的是nums.length == 1的时候我们可以直接返回nums[0]。

Java:

public class Solution {
public int rob(int[] nums) {
if (nums == null) return 0;
if (nums.length == 1) return nums[0];
return Math.max(rob(nums, 0, nums.length - 2), rob(nums, 1, nums.length - 1));
} private int rob(int[] nums, int lo, int hi) {int res = 0, robLastHouse = 0, notRobLast = 0;
for (int i = lo; i <= hi; i++) {
res = Math.max(robLastHouse, notRobLast + nums[i]);
notRobLast = robLastHouse;
robLastHouse = res;
}
return res;
}
}

三刷:

Java:

public class Solution {
public int rob(int[] nums) {
if (nums == null || nums.length == 0) return 0;
if (nums.length == 1) return nums[0];
return Math.max(rob(nums, 0, nums.length - 2), rob(nums, 1, nums.length - 1));
} private int rob(int[] nums, int lo, int hi) {
if (nums == null || lo > hi) return 0;
int robLast = 0, notRobLast = 0, res = 0;
for (int i = lo; i <= hi; i++) {
res = Math.max(robLast, notRobLast + nums[i]);
notRobLast = robLast;
robLast = res;
}
return res;
}
}

Reference:

https://leetcode.com/discuss/36544/simple-ac-solution-in-java-in-o-n-with-explanation

https://leetcode.com/discuss/36770/9-lines-0ms-o-1-space-c-solution

https://leetcode.com/discuss/57601/good-performance-dp-solution-using-java

213. House Robber II的更多相关文章

  1. 198. House Robber,213. House Robber II

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

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

  3. 【LeetCode】213. House Robber II

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

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

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

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

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

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

  7. LeetCode 213. House Robber II

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

  8. 动态规划 - 213. House Robber II

    URL: https://leetcode.com/problems/house-robber-ii/ You are a professional robber planning to rob ho ...

  9. 213. House Robber II(动态规划)

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

随机推荐

  1. HTML5+J2EE实现文件异步上传

    P.S. HTML5经过了W3C的8年努力,终于正式推广了.这次升级最大的就是升级了XMLHTTPRequest,让它变成了XMLHTTPRequest Level II(这有啥奇怪的?).这个对象现 ...

  2. 深度探索C++对象模型读书笔记(2)

    以下测试平台均为vs 2012 指向Data Member的指针测试(1) #include <stdio.h> class Base1 { public: int val1; int v ...

  3. 使用WebJar管理css、JavaScript文件

    Web前端使用了越来越多的JS或CSS,如jQuery, Backbone.js 和Bootstrap.一般情况下,我们是将这些Web资源拷贝到Java的目录下,通过手工进行管理,这种通方式容易导致文 ...

  4. SQL 存储过程加事务的使用

    create proc USP_CUTTING_TATABLET_PULL_FINISH ( @name NVARCHAR(20) ) as SET XACT_ABORT ON--设置全盘回滚 BEG ...

  5. $设置背景图片的css

    $('.d-game-pic').css('background-image', 'url(' + App.getImg(gameDetail.desc_pic) + ')');

  6. ECSHOP如何解决购物车中商品自动消失问题

    最近有客户反映关于ECShop购物车的问题:需要加入多个商品到购物车时,发现之前加入到购物车的商品都自动消失了,只有最后一次加入购物车的商品在里面.那么,这是什么原因呢? 因为ECShop的SESSI ...

  7. CentOS7安装nagios并配置出图详解

    目录 开始之前 系统环境 监控内容 所需软件包 台机器,全都按照CentOS7最小化模式安装系统 系统版本号 [root@localhost ~]# cat  /etc/redhat-release ...

  8. FPGA初学心得

    有三种方法在模块中产生逻辑:1.使用连续赋值语句“assign”:2.用实例元件 3.用“always”块.所以在always块中赋值不能使用assign,而是直接给变量赋值就行. reg与wire的 ...

  9. oracle的function和procedure返回值给shell

    本文演示两个关于如何在shell中调用oracle的function和procedure,并将返回值返回给shell. 1.首在package中创建function和procedure,脚本如下: G ...

  10. 【 Quartz】使用 JobListener (任务监听器可实现) 我想在一个任务执行后在执行第二个任务怎么办呢

    http://liuzidong.iteye.com/blog/1147528 Quartz之JobExecutionException 博客分类: Java Quartz quartzjobexec ...