LeetCode House Robber i问题求解

2人阅读 评论(0) 收藏 举报
分类:

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.


可以使用最基本的DP算法求解,也可以使用滚动数组进行优化。

基础解法:

public class Solution {
    /**
     * @param A: An array of non-negative integers
     * @return: The maximum amount of money you can rob tonight
     */
    public long houseRobber(int[] arr) {
        // write your code here
        if (arr == null || arr.length == 0) {
            return 0l;
        }
        long[] f =  new long[arr.length + 1];
        f[0] = 0;
        f[1] = arr[0];
        for (int i = 2; i < f.length; i++) {
            f[i] = Math.max(f[i - 1], f[i - 2] + arr[i - 1]);
        }
        return f[arr.length];
    }
}
使用滚动数组进行优化的方法:
class Solution {
    public int rob(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int[] f = new int[3];
        f[0] = 0;
        f[1] = nums[0];
        for (int i = 1; i < nums.length; i++) {
            f[(i + 1) % 3] = Math.max(f[i % 3], f[(i - 1) % 3] + nums[i]);
        }
        return f[nums.length % 3];
    }
}


查看评论

leetcode 213 : House Robber II

leetcode 213 : House Robber II
  • xudli
  • xudli
  • 2015-05-21 07:36:47
  • 9732

Leetcode 198: House Robber

House Robber Total Accepted: 642 Total Submissions: 2562You are a professional robber planning to r...
  • xudli
  • xudli
  • 2015-04-01 02:51:09
  • 4494

198. House Robber [easy] (Python)

题目链接 https://leetcode.com/problems/house-robber/ 题目原文 You are a professional robber planning t...
  • coder_orz
  • coder_orz
  • 2016-06-01 14:39:22
  • 1568

leetCode198-House Robber

链接:https://leetcode.com/problems/house-robber/ 这道理可以看做是状态压缩,每两个数字看做是一行,状态有3个,故需要F[N][3]的数组,F[i][j]就表...
  • Lu597203933
  • Lu597203933
  • 2015-04-01 18:28:58
  • 7707

LeetCode213:House Robber II

Note: This is an extension of House Robber. After robbing those houses on that street, the thief ...
  • u012501459
  • u012501459
  • 2015-06-13 11:14:36
  • 1308

leetcode 337. House Robber III-动态规划|Java|Python简洁高效

原题链接:337. House Robber III 【思路】 和 House Robber 十分类似,是Dynamic Programming问题。这里使用递归来实现,数组rob来存储。rob[0...
  • happyaaaaaaaaaaa
  • happyaaaaaaaaaaa
  • 2016-03-13 21:06:45
  • 4788

[leetcode-337]House Robber III(java)

原题:House RobberIII对于这种树形结构,应该很容易想到使用递归的方法,这里的难点在于递归的时候相邻点不能同时访问,因此我写成了第一种做法,也ac了,但是这样并不好,因为它对很多点都会重复...
  • zdavb
  • zdavb
  • 2016-03-27 22:19:41
  • 888

[leetcode] 337. House Robber III

The thief has found himself a new place for his thievery again. There is only one entrance to this a...
  • TstsUgeg
  • TstsUgeg
  • 2016-03-12 20:20:56
  • 2466

LeetCode337. House Robber III

题目: https://leetcode.com/problems/house-robber-iii/ The thief has found himself a new place for hi...
  • codeTZ
  • codeTZ
  • 2016-03-23 20:34:40
  • 1171

Leetcode_198_House Robber

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/47680663 You are a profes...
  • pistolove
  • pistolove
  • 2015-08-15 12:02:28
  • 2478
    个人资料
    持之以恒
    等级:
    访问量: 25万+
    积分: 8639
    排名: 2855
    最新评论