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.
链接: 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的更多相关文章
- 198. House Robber,213. House Robber II
198. House Robber Total Accepted: 45873 Total Submissions: 142855 Difficulty: Easy You are a profess ...
- 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:二叉树下的不能相邻,求能 ...
- 【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 打家劫舍之二
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
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
- 动态规划 - 213. House Robber II
URL: https://leetcode.com/problems/house-robber-ii/ You are a professional robber planning to rob ho ...
- 213. House Robber II(动态规划)
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
随机推荐
- linux 常用软件安装-目录
nginx apache php mysql oracle tomcat memcached mongodb sqlserver
- ngx_cdecl
ngx_cdecl 作为跨平台用,现在理解有限,以后补充 _cdecl 是C Declaration的缩写(declaration,声明),表示C语言默认的函数调用方法:所有参数从右到左依次入栈,这些 ...
- oracle pl/sql的操作大全
--删除该用户及下面的所有关联 DROP USER fspdrs CASCADE; --创建一个用户 create user fspdrs identified " default tabl ...
- 【Qt】数据库连接池
请查看公孙二狗的文章 数据库连接池
- 关于html5 -- plus Webview模块管理应用窗口界面
Webview模块管理应用窗口界面,通过plus.webview可获取应用界面管理对象. 方法: all:获取所有的webview窗口 close:关闭webview窗口 create:创建新的web ...
- nginx-url重写
location /game_web{ if (!-e $request_filename){//请求不是文件或者目录 rewrite ^/game_web/(\/init/$ last; break ...
- 使用Dreamweaver批量删除PHP项目中的单行注释和多行注释
1.删除单行注释 打开Dreamweaver的查找工具,选择正则替换如图: 里面的//.*是正则匹配单行注释的表达式 2.删除多行注释 同样用正则查找匹配,直接上图咯: 其中正则表达式为/\*[ ...
- http中的KeepAlive
为什么要使用KeepAlive? 终极的原因就是需要加快客户端和服务端的访问请求速度.KeepAlive就是浏览器和服务端之间保持长连接,这个连接是可以复用的.当客户端发送一次请求,收到相应以后,第二 ...
- 【转】用perl写的单位电脑信息采集程序
perl,后来我又改过了增加了一些交互和数据库检测的功能.主要用于收集ip.mac.姓名.房间,后来又加入了维修记录的功能.服务器端接受数据并存入数据库中. 代码如下: 主要用于收集ip.mac.姓名 ...
- GridView ItemCommand
GridView ItemCommand中取某行某列的值方法,这里提供两个常用的: 一.用CommandArgument属性取值页面如下: <asp:TemplateColumn HeaderT ...