题目:

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. C# 导出 Excel

    /// <summary> /// 导出Excel /// </summary> public void ExportExcel() { #region 添加引用 Micros ...

  2. 解决svn “clean up" 失败

    解决方法:清空svn的队列 1.下载sqlite3.exe 2.找到你项目的.svn文件,查看是否存在wc.db 3.将sqlite3.exe放到.svn的同级目录 4.启动cmd执行sqlite3 ...

  3. 如何开发 Grunt 插件

    创建 grunt 插件 准备工作:(node/npm/git 安装,在此不做赘述) yeoman generator 可以自动生成一个插件模板. 安装 yo npm install -g yo 安装 ...

  4. Spark Streaming揭秘 Day5 初步贯通源码

    Spark Streaming揭秘 Day5 初步贯通源码 引子 今天,让我们从Spark Streaming最重要的三个环节出发,让我们通过走读,逐步贯通源码,还记得Day1提到的三个谜团么,让我们 ...

  5. Linux分类笔记(一)-权限管理

    Linux分类笔记(一) 权限管理 普通权限 文件的普通权限 对一个普通的文件使用ls -ll命令后,看到下面的输出内容   而对于文件权限中的每一位,又分别代表了以下的意思 文件类型又有以下几类: ...

  6. android.support.v4.widget.DrawerLayout使用

    activity_main.xml布局如下: <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas ...

  7. install ruby and ruby gem

    sudo apt-get install ruby #find an folder and: git clone https://github.com/rubygems/rubygems.git cd ...

  8. 八、mysql视图、存储过程、函数以及时间调度器

    .create or replace view emp_view as select * from t4 ;给t4表创建一个名为emp_view的视图 .drop view emp_view 删除视图 ...

  9. 【POJ2104】kth num

    You are working for Macrohard company in data structures department. After failing your previous tas ...

  10. 【BZOJ2152】聪聪可可

    Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一般情况下石头剪刀布就好 ...