leetcode No213. House Robber II

Posted Dufre.WC

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode No213. House Robber II相关的知识,希望对你有一定的参考价值。

Question

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.

Algorithm

Before this problem, maybe you can see 198. House Robber.
You need to noticed All houses at this place are arranged in a circle.

For example, The circle is

0 -  1  -  2  -  3
|                | 
n - n-1 - n-2 - ...

The solution is now the maximum of:

  1. Rob houses 0 to n - 2;
  2. Rob houses 1 to n - 1.

And this question back to House Robber.

Code

C++

class Solution 
public:
    int rob_helper(vector<int>& nums, int left, int right) 
        int a = 0;
        int b = 0;

        for(int i=left; i<=right; i++)
            if(i%2==0)
                a = max(a+nums[i], b);
            
            else
                b = max(b+nums[i], a);
            
        

        return max(a,b);
    
    int rob(vector<int>& nums) 
        if (nums.empty())
            return 0;
        if (nums.size() == 1)
            return nums[0];
        int res1 = rob_helper(nums, 0, nums.size()-2);
        int res2 = rob_helper(nums, 1, nums.size()-1);
        
        return res1>res2?res1:res2;
    
;

Java

class Solution 
    public int rob_helper(int[] nums, int left, int right) 
        int a = 0;
        int b = 0;
        
        for(int i=left; i<=right; i++)
            if(i%2==0)
                a = Math.max(a+nums[i], b);
            
            else
                b = Math.max(b+nums[i], a);
            
        
        
        return Math.max(a,b);        
    
    public int rob(int[] nums) 
        if (nums == null)
            return 0;
        if (nums.length == 1)
            return nums[0];
        
        int res1 = rob_helper(nums, 0, nums.length-2);
        int res2 = rob_helper(nums, 1, nums.length-1);
        
        return Math.max(res1, res2);
    

Similar Question

以上是关于leetcode No213. House Robber II的主要内容,如果未能解决你的问题,请参考以下文章

1线性DP 213. 打家劫舍 II

LeetCode 213. House Robber II

LeetCode 213. House Robber II

LeetCode 198, 213 House Robber

LeetCode 213. House Robber II

[动态规划] leetcode 213 House Robber II