题目:

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.

Hide Tags

Dynamic Programming 

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

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

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

  2. 198. House Robber(动态规划)

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

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

  4. [LeetCode] 198. House Robber 打家劫舍

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

  5. Leetcode 198 House Robber

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

  6. Java for LeetCode 198 House Robber

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

  7. (easy)LeetCode 198.House Robber

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

  8. 【LeetCode】198 - House Robber

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

  9. Java [Leetcode 198]House Robber

    题目描述: You are a professional robber planning to rob houses along a street. Each house has a certain ...

随机推荐

  1. echo & print

    在实际使用中, print 和 echo 两者的功能几乎是完全一样.可以这么说,凡是有一个可以使用的地方,另一个也可以使用.但是,两者之间也还是一个非常重要的区别:在 echo 函数中,可以同时输出多 ...

  2. [转]Nuget挂了的解决方法

    今天用Nuget下一个程序包时,发现Nuget挂了:未能解析此远程名称:'nuget.org'.第一反应就是方校长抖威风了,挂个代理上 http://nuget.org 试了下,果然好好的. 用命令n ...

  3. 隐藏index.php - ThinkPHP完全开发手册 - 3.1

      为了更好的实现SEO优化,我们需要隐藏URL地址中的index.php,由于不同的服务器环境配置方法区别较大,apache环境下面的配置我们可以参考5.9 URL重写来实现,就不再多说了,这里大概 ...

  4. css圆角 四边投影

    -moz-border-radius: 30px;-webkit-border-radius: 30px; border-radius:30px; -webkit-box-shadow:0 0 10p ...

  5. [DevExpress]GridControl 列头绘制Checkbox

    关键代码: /// <summary> /// 为列头绘制CheckBox /// </summary> /// <param name="view" ...

  6. PNG兼容IE6解决方法

    虽然说现在早就不用ie6浏览器了,可以还是有一小部分还在使用 ,刚好公司也有要求~~~ <p> E6不兼容png图片,确实让网页的图片质量大大下降,为了兼容万恶的IE6,总结了下面几种方法 ...

  7. Sublime text2 常用插件集锦

    No.01 – EmmetEmmet 是一个前端开发的利器,其前身是Zen Coding.它让编写 HTML 代码变得简单.Emmet 的基本用法是:输入简写形式,然后按 Tab 键.关于 Emmet ...

  8. Spark Streaming揭秘 Day32 WAL框架及实现

    Spark Streaming揭秘 Day32 WAL框架及实现 今天会聚焦于SparkStreaming中非常重要的数据安全机制WAL(预写日志). 设计要点 从本质点说,WAL框架是一个存储系统, ...

  9. OSI与TCP/IP模型之比较

    摘要:OSI参考模型和Internet模型(或称TCP/IP模型)作为计算网络发展过程影响力大的两大模型,它们共同之处是:都采用了层次结构的概念,从分析两者的异同入手,找出OSI的消亡和Interne ...

  10. 干净的 ef for Oracle appconfg配置

    <?xml version="1.0" encoding="utf-8"?> <configuration> <configSec ...