题目:

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. 判断IFeatureClass图形是否含有Z值信息,若有为IPoint赋Z值

    判断IFeatureClass图形是否含有Z值信息 IFeatureClass featureClass = this.pLayer.FeatureClass; string shapeFieldNa ...

  2. 关键字 explicit

    C++中, 一个参数的构造函数(或者除了第一个参数外其余参数都有默认值的多参构造函数), 承担了两个角色. 1 是个构造器 ,2 是个默认且隐含的类型转换操作符. 所以, 有时候在我们写下如 AAA ...

  3. 简单使用packetbeat

    简单使用packetbeat 标签:packetbeat elasticsearch 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在前面两篇文章中记录了使用logstash来收集mysql的慢 ...

  4. javascript之DOMReady

    DOMReady实现策略    * 在页面的DOM树创建完成后(即HTML解析第一步完成)就触发,而无需等待其他资源的加载,即DOMReady实现策略    * 支持DOMContentLoaded事 ...

  5. SQL做日历

    DECLARE @DATE DATETIME SET @DATE=GETDATE() SELECT SUN -DAY(@DATE),@DATE))=@DATE THEN '*' ELSE '' END ...

  6. WinForms 实现气泡提示窗口

    [实例说明] 气泡提示因为他的美观又好被大多数用户所接收,用户所喜爱的就是程序员要实现的. 本实例实现了任务栏气泡提示,运行本实例,效果图如下所示: 单击提示.气泡提示就会显示,单击“关闭”气泡又会消 ...

  7. 详解AngularJS中的filter过滤器用法

    系统的学习了一下angularjs,发现angularjs的有些思想根php的模块smarty很像,例如数据绑定,filter.如果对smarty比较熟悉的话,学习angularjs会比较容易一点.这 ...

  8. slider jq小插件

    html代码 <div class="r_list r_1" style="display:block;"> <div class=" ...

  9. Hello World 的makefile模板及其分析

    makefile模板: ifeq ($(KERNELRELEASE),) //判断KERNELRELEASE是否为空,只有执行make的当前目录为内核源代码目录时,该变量才不为空. KERNELDIR ...

  10. 地形图比例尺、等高距和DEM分辨率关系

    地表面的形态是很复杂的,不同地貌类型的形态是由它的相对高度、地面坡度以及所处的地势所决定的,它们是影响等高距的主要因素。从等高距计算公式可以看出,当地图比例尺和图上等高线间的最小距离简称等高线间距确定 ...