博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
198. House Robber
阅读量:6669 次
发布时间:2019-06-25

本文共 5182 字,大约阅读时间需要 17 分钟。

题目:

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  for adding this problem and creating all test cases. Also thanks to  for adding additional test cases.

 

Hide Tags
 
 

链接:  

题解:

一维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

转载地址:http://rdlxo.baihongyu.com/

你可能感兴趣的文章
coursera课程Text Retrieval and Search Engines之Week 2 Overview
查看>>
BZOJ 2768: [JLOI2010]冠军调查 最小割
查看>>
L - 辗转相除法(第二季水)
查看>>
Android自己定义控件(状态提示图表)
查看>>
ThinkPHP项目笔记之控制器常用语法
查看>>
【solr基础教程之二】索引
查看>>
【转】Android自定义Adapter的ListView的思路及代码
查看>>
8VC Venture Cup 2016 - Final Round (Div. 2 Edition)B. sland Puzzle 水题
查看>>
hadoop之mapreduse 在Eclipse下的调试环境篇
查看>>
说好的加班呢
查看>>
如何在Ubuntu 16.04中创建GIF动图
查看>>
设计模式之七:模板方法模式(Template Method)
查看>>
Atitit。数据库 安全性 重要敏感数据加密存储解决方案
查看>>
android中的所谓观察者模式
查看>>
初识Hadoop
查看>>
(转)Lambda表达式详解
查看>>
Android SharedPreference的使用
查看>>
C#中的线程(四)高级话题
查看>>
在android中进行视频的分割
查看>>
LINUX 内核内存管理
查看>>