动态规划 - 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 is the neighbor of the last one. Meanwhile, 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.

Example 1:

Input: [2,3,2]
Output: 3
Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2),
             because they are adjacent houses.

Example 2:

Input: [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
             Total amount you can rob = 1 + 3 = 4.

解决方案:

Since it is a cyclied array, to make our dynamic programming alogrithm works, we need to break the cyclied array into two single-way arrays.

2-The priciple here is that either a particular house i-th can be robbed or not robbed at all.

For example 1 -> 3 -> 6 -> 5, we can break at the first position, so that the problem is divided to two parts,which are 1 -> 3 -> 6 (we choose to rob the first element, but not the last one.) and 3->6->5. Finally, we can reuse the House Robber I to solve these two sub-problems and pick out the optimal solution.

3-Here some of us may have the confusion that: we can rob house 1, it does not mean that we must rob house 1, whether we rob house 1 is depending on its next value. The correct complement of this statement is that: we should not(must not) rob house 1 ( and similarly, whether to rob the last house depends on the whole broken array.)

class Solution {
    public int rob(int[] nums) {
        if(nums == null || nums.length == 0) return 0;

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

        int len = nums.length;
        int[] amount = new int[len];
        //The first case: we can rob house 1, definitely, we cannot rob the last one
        amount[0] = nums[0];
        // whether we rob house 1 is depending the relative value
        amount[1] = Math.max(nums[0],nums[1]);
        for(int idx =2; idx < len-1; idx ++){
            amount[idx] = Math.max(amount[idx-1],amount[idx-2] + nums[idx]);
        }
        // the last element is zero. amount[len-1] by default is zero
        int totalSumCaseOne = amount[len-2];

        // The second case: we don‘t rob house 1 at all.
        amount = new int[len];
        amount[0] = 0;
        amount[1] = nums[1];
        for(int idx = 2 ;idx< len; idx++){
            amount[idx] = Math.max(amount[idx-1],amount[idx-2] + nums[idx]);
        }
        int totalSumCaseTwo = amount[len-1];

        //select the largest one
        return Math.max(totalSumCaseOne,totalSumCaseTwo);
    }
}

原文地址:https://www.cnblogs.com/frankcui/p/10480695.html

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

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

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

题目 和这道题目 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. 

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. 

[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

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.