House Robber——LeetCode

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.

题目大意就是,要抢劫一条街,不能抢连续的两家,否则会报警,只能隔着抢或者跳着抢,输出可以抢到的最大值。这属于入门的动规题,可以理解为一个无上限的有限制的01背包题。

我的思路是:

  使用数组F[1...n]表示,抢劫到第i家时的最大获利为F[i],那么根据题意可以写出递推公式F[i]=max{F[i-1],F[i-2]+num[i]},其中num[i]是抢第i家可以获利多少。

下面是递归和非递归两种实现:

int[] max = new int[2000];

    public int rob(int[] num) {

        if (num == null || num.length == 0) {
            return 0;
        }
        if (num.length == 1) {
            return num[0];
        }
        if (num.length == 2) {
            return Math.max(num[0], num[1]);
        }
        Arrays.fill(max, -1);
        return choose(num.length - 1, num);
    }

    public int choose(int k, int[] num) {
        if (k == 0)
            return num[k];
        if (k < 0) {
            return 0;
        }
        if (max[k] == -1) {
            max[k] = Math.max(choose(k - 1, num), choose(k - 2, num) + num[k]);
        }
        return max[k];
    }

非递归:

 public int rob2(int[] num) {

        if (num == null || num.length == 0) {
            return 0;
        }
        if (num.length == 1) {
            return num[0];
        }
        if(num.length==2){
            return Math.max(num[0],num[1]);
        }
        max[0]=num[0];
        max[1]=Math.max(num[0],num[1]);
        for(int i=2;i<num.length;i++)
        {
            max[i]=Math.max(max[i-1],max[i-2]+num[i]);
        }
        return max[num.length-1];
    }
时间: 2025-01-14 05:46:15

House Robber——LeetCode的相关文章

House Robber -- leetcode

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 autom

(Easy) House Robber LeetCode

class Solution { public int rob(int[] nums) { if(nums.length<=0 || nums ==null){ return 0; } if( nums.length ==1){ return nums[0]; } if(nums.length ==2){ return Max(nums[0],nums[1]); } int[] dp = new int[nums.length]; dp[0] = nums[0]; dp[1] = Max(num

Solution to LeetCode Problem Set

Here is my collection of solutions to leetcode problems. LeetCode - Course Schedule LeetCode - Reverse Linked List LeetCode - Isomorphic Strings LeetCode - Count Primes LeetCode - Remove Linked List Elements LeetCode - Happy Number LeetCode - Bitwise

[LeetCode][JavaScript]House Robber

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 

LeetCode House Robber III

原题链接在这里:https://leetcode.com/problems/house-robber-iii/ 题目: The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent hous

LeetCode中 House Robber问题随笔

先附上题目链接,有两问: https://leetcode.com/problems/house-robber/ https://leetcode.com/problems/house-robber-ii/ 第一问题目如下: You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constrain

【Leetcode】House Robber

题目链接:https://leetcode.com/problems/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 ho

leetcode:House Robber(动态规划dp1)

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 autom

[动态规划] leetcode 213 House Robber II

problem: https://leetcode.com/problems/house-robber-ii/ 多状态转换dp.我的方法是维护了四个状态.用两趟dp的基本思想也是多个状态. class Solution { public: int rob(vector<int>& nums) { int n = nums.size(); vector<int> robber(n + 1, 0); vector<int> norobber(n + 1, 0); v