[动态规划] 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);
        vector<int> nofirst_robber(n + 1, 0);
        vector<int> nofirst_norobber(n + 1, 0);
        for(int i = 0;i < n;i++)
        {
            if(i == 0)
            {
                nofirst_robber[i + 1] = nofirst_norobber[i];
                nofirst_norobber[i + 1] = max(nofirst_robber[i], nofirst_norobber[i]);
            }
            else
            {
                nofirst_robber[i + 1] = nofirst_norobber[i] + nums[i];
                nofirst_norobber[i + 1] = max(nofirst_robber[i], nofirst_norobber[i]);
            }
            if(i == n - 1 && i != 0)
            {
                robber[i + 1] = norobber[i];
                norobber[i + 1] = max(robber[i], norobber[i]);
            }
            else
            {
                robber[i + 1] = norobber[i] + nums[i];
                norobber[i + 1] = max(robber[i], norobber[i]);
            }
        }
        return max({robber[n], norobber[n], nofirst_robber[n], nofirst_norobber[n]});
    }
};

原文地址:https://www.cnblogs.com/fish1996/p/11331152.html

时间: 2024-11-09 22:17:26

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

题目 和这道题目 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()=

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

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

[leedcode 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 arearranged in a circle. T