LeetCode192: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.

这是道动态规划的问题。假设给定的vector为[2,3,4,1,6,5]

可以这么来定义DP的状态和状态转移方程:

A[0]表示抢劫前0户(必须抢第0户)的钱为2。

A[1]表示抢劫前1户(必须抢第1户)的钱为3。因为抢了第1户,所以不能抢第0户了。

A[2]表示抢劫前2户(必须抢第2户)的钱的A[0]+4。因为抢了第2户,所以不能抢第1户。

A[3]表示抢劫前3户(必须抢第3户)的钱为max{A[0],A[1]}+1。因为抢了第3户,所以不能抢第2户,只能取A[0],A[1]中的最大值。

这样状态转移方程为:

A[i]=max{A[j],0}+data[i]。0<=j<i-1。

抢劫数目最多的的钱为:max{A[i]}。0<=i<n

基于这个状态和状态转移方程的代码如下:

class Solution {
public:
    int rob(vector<int>& nums) {
        int length=nums.size();
        int *A=new int[length];//A[i] represent the money of rob the room of i and less index.
        int maxMoney=0;
        for(int i=0;i<nums.size();i++)
        {
            A[i]=0;
            for(int j=0;j<i-1;j++)
            {
                if(A[j]>A[i])
                    A[i]=A[j];
            }
            A[i]+=nums[i];
            if(A[i]>maxMoney)
                maxMoney=A[i];
        }
        delete [] A;
        return maxMoney;
    }
};
时间: 2024-08-09 14:37:17

LeetCode192:House Robber的相关文章

leetcode 213. House Robber II 抢劫房子II---------- java

Note: This is an extension of House Robber. After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. 

[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 autom

【leetcode】House Robber (middle)

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] House Robber python 解决方案

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

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 a

213. House Robber II

题目: Note: This is an extension of House Robber. After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circ

LeetCode 213. House Robber II

Note: This is an extension of House Robber. After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. 

LeetCode 198. 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 autom

198,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