`

Leetcode - 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.

[分析]

一维动态规划,记num[i]对应的最大安全盗金为safe[i], 递推式为safe[i] = max(safe[i-1], safe[i-2] + num[i])。

实现时仅需一个三元数组,循环使用.

// version1: 思路不够清晰准确,递推式写错但撞大运实现ok,可读性不好的初版
// 后追加注释
public class Solution {
    public int rob(int[] num) {
        if (num == null || num.length == 0) 
            return 0;
        int safe = 0; // num[i-2] 处能获取的最大盗金,num[i-2]本身不一定被盗
        int adj = 0; // num[i-1]能获取的最大盗金,num[i-1]本身不一定被盗
        int curr = 0; // 盗取num[i]所获的最大盗金
        int max = 0; // num[i]处能获取的最大盗金=max(curr, adj)
        for (int i = 0; i < num.length; i++) {
            curr = safe + num[i];
            if (curr > max)
                max = curr;
            safe = adj;
            adj = max;
        }
        return max;
    }
}
// version 2
// 递推式:safe[i] = max(safe[i-1], safe[i-2] + num[i])
// safe[2]表示num[i]处的最多盗金
// safe[1]表示num[i-1],即在与num[i]相邻的前面一户能拿的最大盗金
// safe[0]表示num[i-2]处最大盗金
public class Solution {
     public int rob(int[] num) {
        if (num == null || num.length == 0) 
            return 0;
        int n = num.length;
        if (n == 1)
            return num[0];
        else if (n == 2)
            return num[0] > num[1] ? num[0] : num[1];
        
        int[] safe = {num[0], (num[1] > num[0]? num[1] : num[0]), 0};
        for (int i = 2; i < num.length; i++) {
            safe[2] = Math.max(safe[1], safe[0] + num[i]);
            safe[0] = safe[1];
            safe[1] = safe[2];
        }
        return safe[2];
     }
}

    

 

 

分享到:
评论
4 楼 likesky3 2015-04-03  
赞yb君的代码精简,我会采纳你的意见,面试时可以先写safe[n]版,有时间再向面试官指出可以用更少的空间实现。当知道有更优化的方案时,我总想一次做到,完成时间自然会相应延迟些,我要有意识地培养first work than optimize的意识
3 楼 qb_2008 2015-04-02  
现在看起来好多了!如果想再优化,有一点代码可以去掉。
// 递推式:safe[i] = max(safe[i-1], safe[i-2] + num[i])  
// safe[2]表示num[i]处的最多盗金  
// safe[1]表示num[i-1],即在与num[i]相邻的前面一户能拿的最大盗金  
// safe[0]表示num[i-2]处最大盗金  
public class Solution {  
     public int rob(int[] num) {  
        if (num == null)   
            return 0;            
        int[] safe = {0, 0, 0};  // Does java initialize variables?: int[] safe = new int[3];
        for (int i = 0; i < num.length; i++) {  
            safe[2] = Math.max(safe[1], safe[0] + num[i]);  
            safe[0] = safe[1];  
            safe[1] = safe[2];  
        }  
        return safe[2];  
     }
}


另外,我感觉面试中更注重时间复杂度,对空间复杂度并不太注意,
所以我会选择直接用safe[n]的数组来递推,这样代码简单,正确性高,完成时间快。
// safe[i] is max amount of money we can rob from [0-i] houses.
// safe[i] = max(safe[i-1], safe[i-2] + num[i])   
public class Solution {  
     public int rob(int[] num) {  
        if (num == null || num.length == 0)   
            return 0;            
        else if (num.length == 1)
            return num[0];
        else if (num.length == 2)
            return Math.max(num[0], num[1]);

        int[] safe = new int[num.length];
        safe[0] = num[0];
        safe[1] = Math.max(safe[0], num[1]);
        for (int i = 2; i < num.length; i++) {
             safe[i] = Math.max(safe[i-1], safe[i-2] + num[i]);
        }  
        return safe[num.length - 1];  
     }
}
2 楼 likesky3 2015-04-02  
谢谢yb君的意见,我自己也觉得没描述清楚,而且递推式确实不准确,已更正
1 楼 qb_2008 2015-04-01  
safe[i]的定义为偷完前i户最大所得值。完整递推式应为safe[i] = max(safe[i - 1], safe[i - 2] + num[i])。
代码中把safe, adj, curr, max四个变量混在一起,着实难以理解。没有注释都看不懂,可不可以让可读性更好些?

相关推荐

    最大公共字符串leetcode-HouseRobber-Problem-LeetCode-:HouseRobber-问题-LeetCode-

    HouseRobber-问题-LeetCode- @Faizansayeed28 代码 /** * 问题陈述- 你是一名职业劫匪,计划抢劫街道上的房屋。 每个房子都有一定数量的钱 藏起来,阻止你抢劫他们的唯一限制是相邻的房子有安全系统 连接,如果同一...

    LeetCode:Leetcode-解决方案

    House Robber [198]6. Range Sum Query - Immutable [303]7. Counting Bits [338]8. Palindromic Substrings [647]9. Maximum Length of Pair Chain [646]10. Integer Break [343]11. Count Numbers with Unique ...

    leetcode和oj-OJ_Solution:leetcode、poj等OJ解决方案

    leetcode 和 oj OJ_Solution Solutions for OJ such as leetcode, poj and so on 版本 ...https://leetcode.com/problems/two-sum/ ...java/houserobber https://leetcode.com/problems/house-robber/

    lrucacheleetcode-LeetCode:复制和思考

    lru cache leetcode LeetCode copy and think 加油学习 1. 时间轴 时间 题目 描述 知识点 ...House Robber 动态规划 如何确定子问题 20200525 20200525 20200525 20200525 20200525 20200525 $1 234

    leetcode最大蓄水量-leetcode:记录自己leetocde的过程

    House Robber 学会了数组的创建,以及动态规划最基本的题目 2021.2.2 322 兑换钱币 学会了 Arrays.fill 的使用,以及查看源码,返回 记录在哔哩哔哩 中 2021.2.3 53 最大子数字之和 先更新max,再将负数赋值为0 62 ...

    gasstationleetcode-leetcode:LeetcodeOJ解决方案

    leetcode 【演示记录】 报告 展示 2017/03/06 1.二和,167.二和二 2107/03/06 15.3 总和,16.3 总和最近,18.4 总和,11.最多水的容器 2017/03/09 62.Unique Paths, 63.Unique Paths II, 64.Minimum Path Sum 2017/...

    lrucacheleetcode-LeetCode:CppSourceCode的LeetCode解决方案

    lru cache leetcode LeetCode 解决题目的总数: 136/1753 微信公众号: 工程师Ruojhen 算法-动态规划 题号 名称 English Name 题解 53 最大子序和 Maximum Subarray 70 ...House Robber 213 打家劫舍Ⅱ

    LeetCode判断字符串是否循环-data-structure-and-algo:C++中的数据结构和算法

    HouseRobber(#198), ClimbingStairs(#70), CoinChange(#322), EditDistance(#72) 图 : 集合的合并(直接求并、按大小求并和按深度求并)、根搜寻(路径压缩) : 有向图和无向图的邻接表表示方法 : 图的深度优先和广度...

    prac:编码实践

    编码实践 编码实践 模式:二进制搜索: 二进制搜索 二进制搜索: : 排序数组的上限: : ... 众议院强盗2: ://leetcode.com/problems/house-robber-ii/discuss/59921/9-lines-0ms-O(1)-Space-C++-sol

    cpp-算法精粹

    House Robber III Range Sum Query - Immutable Range Sum Query 2D - Immutable 图 Clone Graph 位操作 Reverse Bits Repeated DNA Sequences Number of 1 Bits Gray Code Single Number Single Number II Single ...

Global site tag (gtag.js) - Google Analytics