198. House Robber
题目:
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
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.
Credits:
Special thanks to @ifanchu for adding this problem and creating all test cases. Also thanks to @ts for adding additional test cases.
链接: http://leetcode.com/problems/house-robber/
题解:
一维DP, 当前最大值相当于Math.max(nums[i - 1],nums[i] + nums[i - 2]),处理一下边界情况就可以了。
Time Complexity - O(n), Space Complexity - O(1)。
public class Solution {
public int rob(int[] nums) {
if(nums == null || nums.length == 0)
return 0; for(int i = 0; i < nums.length; i++){
int temp1 = i - 1 < 0 ? 0 : nums[i - 1];
int temp2 = i - 2 < 0 ? 0 : nums[i - 2];
nums[i] = Math.max(temp1, nums[i] + temp2);
} return nums[nums.length - 1];
}
}
Update:
public class Solution {
public int rob(int[] nums) {
if(nums == null || nums.length == 0)
return 0;
int prePre = 0, pre = 0; for(int i = 0; i < nums.length; i++) {
prePre = i - 2 >= 0 ? nums[i - 2] : 0;
pre = i - 1 >= 0 ? nums[i - 1] : 0;
nums[i] = Math.max(nums[i] + prePre, pre);
} return nums[nums.length - 1];
}
}
Update:
做到了House Rob II,返回来思考,以下写会比较方便,不用更改原数组,用三个变量就可以了。跟Climb Stairs很像
public class Solution {
public int rob(int[] nums) {
if(nums == null || nums.length == 0)
return 0;
int prePre = 0, pre = 0, max = 0; for(int i = 0; i < nums.length; i++) {
if(i - 2 < 0)
prePre = 0;
if(i - 1 < 0)
pre = 0;
max = Math.max(nums[i] + prePre, pre);
prePre = pre;
pre = max;
} return max;
}
}
二刷:
dp依然不过关啊...
Java:
更改原数组的.
public class Solution {
public int rob(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int len = nums.length;
for (int i = 0; i < len; i++) {
int notRobLastHouse = i - 2 >= 0 ? nums[i - 2]: 0;
int robLastHouse = i - 1 >= 0 ? nums[i - 1] : 0;
nums[i] = Math.max(robLastHouse, notRobLastHouse + nums[i]);
}
return nums[len - 1];
}
}
分别记录robLast和notRobLast的
public class Solution {
public int rob(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int notRobLastHouse = 0;
int robLastHouse = 0;
for (int i = 0; i < nums.length; i++) {
if (i % 2 == 0) {
notRobLastHouse = Math.max(notRobLastHouse + nums[i], robLastHouse);
} else {
robLastHouse = Math.max(robLastHouse + nums[i], notRobLastHouse);
}
}
return Math.max(notRobLastHouse, robLastHouse);
}
}
类似Climb Stairs的
public class Solution {
public int rob(int[] nums) {
if(nums == null || nums.length == 0) {
return 0;
}
int prePre = 0, pre = 0, max = 0; for (int i = 0; i < nums.length; i++) {
max = Math.max(nums[i] + prePre, pre);
prePre = pre;
pre = max;
}
return max;
}
}
三刷:
Java:
dp - O(n) Space, 我们先建立一个长为len + 1的dp数组,dp[i]代表到原数组第i - 1位为止,最大的profit,然后设置dp[1] = nums[0],之后就可以用转移方程比较dp[i - 1]和dp[i - 2] + nums[i - 1]来求得dp[i]。最后返回dp[len]
public class Solution {
public int rob(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int len = nums.length;
int[] dp = new int[len + 1];
dp[1] = nums[0];
for (int i = 2; i <= len; i++) {
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i - 1]);
}
return dp[len];
}
}
dp O(1) Space : 同样我们可以压缩space complexity到O(1),可以使用和climbing stairs类似的方法,用三个变量来保存临时结果。robLast和notRobLast分别代表 i - 1步和i - 2步。
public class Solution {
public int rob(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int profit = 0, robLast = 0, notRobLast = 0;
for (int i = 0; i < nums.length; i++) {
profit = Math.max(robLast, notRobLast + nums[i]);
notRobLast = robLast;
robLast = profit;
}
return profit;
}
}
Update:
public class Solution {
public int rob(int[] nums) {
if (nums == null || nums.length == 0) return 0;
int robLast = 0, notRobLast = 0, res = 0;
for (int num : nums) {
res = Math.max(notRobLast + num, robLast);
notRobLast = robLast;
robLast = res;
}
return res;
}
}
Reference:
https://leetcode.com/discuss/30079/c-1ms-o-1-space-very-simple-solution
https://leetcode.com/discuss/30020/java-o-n-solution-space-o-1
https://leetcode.com/discuss/31878/java-dp-solution-o-n-runtime-and-o-1-space-with-inline-comment
198. House Robber的更多相关文章
- 198. House Robber,213. House Robber II
198. House Robber Total Accepted: 45873 Total Submissions: 142855 Difficulty: Easy You are a profess ...
- 198. House Robber(动态规划)
198. House Robber You are a professional robber planning to rob houses along a street. Each house ha ...
- 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] 198. House Robber 打家劫舍
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- Leetcode 198 House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- Java for LeetCode 198 House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- (easy)LeetCode 198.House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- 【LeetCode】198 - House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- Java [Leetcode 198]House Robber
题目描述: You are a professional robber planning to rob houses along a street. Each house has a certain ...
随机推荐
- Bootstrap学习笔记(三) 网格系统
4-1实现原理 网格系统的实现原理非常简单,仅仅是通过定义容器大小,平分12份(也有平分成24份或32份,但12份是最常见的),再调整内外边距,最后结合媒体查询,就制作出了强大的响应式网格系统.Boo ...
- Windows7旗舰版32激活码 OEM密钥
1.win7旗舰版32激活码如下: KH2J9-PC326-T44D4-39H6V-TVPBY TFP9Y-VCY3P-VVH3T-8XXCC-MF4YK 236TW-X778T-8MV9F-937G ...
- css圆角 四边投影
-moz-border-radius: 30px;-webkit-border-radius: 30px; border-radius:30px; -webkit-box-shadow:0 0 10p ...
- [原创] linux课堂-学习笔记-课程3.Linux目录结构介绍及内核与shell分析
一.目录说明 1.1 bin 一般用户,可执行的系统内置命令 1.2 sbin 系统管理员,可执行的系统内置命令 1.3 boot 启动文件目录,启动有关的文件都保存在此 1.4 dev 设备管理文件 ...
- json数据与字符串相互转化的例子
json与字符串之间的转换,本文分享一个小例子. json转成string[需要引用json2.js文件]: var arr=[{id:'id',name:'Spring'},{id:'id2' ...
- IP HELPER GetAdaptersAddresses 函数
自己做的一些笔记,XP以及以后的系统使用: MSDN 函数:http://msdn.microsoft.com/en-US/library/windows/desktop/aa365915(v=vs. ...
- 在制作joomla模板过程中遇到的问题
'''问题1.'''在jjc首页中两个通知公告和基建首页的两个模块中,当我点击查看文章标题是,而通知公告和最新动态页一直都还显示,发现文章一直在网站的下部,而不显示在它应该显示的main_rigth模 ...
- 针对PIL中ImageDraw.py报错的解决方案
linux mint 13开始就发现这个问题了,一直不知道怎么解决,今天突然发现了解决方案,来分享给大家 下面是修改对比,自己根据修改,这个是系统文件,需要root权限,路径/usr/lib/pyth ...
- 三、记一次失败的 CAS 搭建 之 服务端配置
==================================================================================================== ...
- excle,aspose.cells 公式字段值取不到 xmls转xml
问题: 一,单元格如果是公式的,读出值为0 aspose.cells 4.4.0.5版本 由于太低,读xmls后缀的excel文件时,发现如果此列是公式算出来的,值是获取不到的.获取到的值一直是0 二 ...