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 ...
随机推荐
- 膜拜 2014-2 (献给L之三)
文/安然 深深的夜静静的想你细细的品味满满的甜蜜爱,心灵的对话让我流泪战栗谢谢你给我这么弥足珍贵的体会不能一生相随又有什么关系你一直都在我的爱就永不停息此生足以——献给心底的爱
- [GeekBand] STL与泛型编程(1)
在C++语法的学习过程中,我们已经对模板有了基本的了解.泛型编程就是以模板为工具的.泛化的编程思想.本篇文章介绍了一些在之前的文章中没有涉及到的一些模板知识.泛型编程知识和几种容器.关于模板的一些重复 ...
- SQL SERVER 遇到Unable to find the requested .Net Framework Data Provider. It may not be installed. (System.Data)
今天新装的SQLSERVER 2012 EXPRESS 用于客户端程序 安装完成后打开登陆SQLSERVER 一切正常 当查看表定义.视图结构时,弹出一下内容 Unable to find the r ...
- [Linux]学习笔记(1)
说到Linux就不得不提UNIX,因为Linux是从UNIX系统发展来的,两系统极为相似,可以在UNIX操作系统上使用的功能都可以在Linux上使用,只可能有少许的差异: UNIX系统中所有的东西都是 ...
- phpcms v9 自定义分页 带下拉跳转
<?php function new_pages($num, $curr_page, $perpage = 20, $urlrule = '', $array = array(),$setpag ...
- phpcms v9二级栏目生成到根目录后三级栏目无法访问的解决办法
这个栏目问题折腾了我一天多,可怜我这个美工又不会程序!!! 我的问题是: 我的栏目设置如下: 一级栏目[生成到根目录(是)]> 二级栏目[生成到根目录(是)] > 三级栏目[生成到根目录( ...
- linux 输入子系统(1)----系统概述
输入设备的工作中,只是中断.读键值/坐标值是设备相关的,而输入事件的缓冲区管理以及字符设备驱动的file_operations接口则对输入设备是通用的,基于此,内核设计了input输入子系统,由核心层 ...
- 1078. Hashing (25)
时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of this problem is simp ...
- 【BZOJ 2301】[HAOI2011]Problem b
Description 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. Input 第一行一个整数 ...
- Qt智能指针简明说明
下面的智能指针分别对应boost库,Qt库,c++11的智能指针 boost::scoped_ptr QScopedPointer unique_ptr 在其生命期结束后会自动删除它所指的对象(确定 ...