LeetCode 213. House Robber II (动态规划)

题目

和这道题目 https://www.cnblogs.com/dacc123/p/12295924.html 一样,改进了一点,就是首尾也是相邻的。

那么我们在DP的时候,还要考虑第一个房子有没有被抢劫的情况。所以状态数组变成了DP[i][j][k],j表示i个房子是否抢劫,k表示第1个房子是否抢劫。

class Solution {
public:
    int dp[100005][2][2];
    int rob(vector<int>& nums) {

        if(nums.size()==0)
            return 0;
        if(nums.size()==1)
            return nums[0];
        dp[0][0][0]=0;
        dp[0][1][1]=nums[0];

        dp[0][0][1]=-1;
        dp[0][1][0]=-1;

        for(int i=1;i<nums.size();i++)
        {
            if(dp[i-1][0][1]==-1)
                dp[i][1][1]=-1;
            else
                dp[i][1][1] = dp[i-1][0][1] + nums[i];

            dp[i][1][0] = dp[i-1][0][0] + nums[i];

            dp[i][0][1] = max(dp[i-1][1][1],dp[i-1][0][1]);

            dp[i][0][0] = max(dp[i-1][1][0],dp[i-1][0][0]);
        }

        return max(dp[nums.size()-1][1][0],max(dp[nums.size()-1][0][1],dp[nums.size()-1][0][0]));

    }
};

原文地址:https://www.cnblogs.com/dacc123/p/12311104.html

时间: 2024-08-27 07:40:33

LeetCode 213. House Robber II (动态规划)的相关文章

leetCode 213. House Robber II | Medium | Dynamic Programming

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 a

[动态规划] 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

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

Java for 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】213. House Robber II

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 arrang

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

动态规划 - 213. House Robber II

URL: 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. All houses at this place are arranged in a circle. That means the first house i

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