Leetcode 213. House robber II

Source: Internet
Author: User

Note:this is a extension of house robber.

After robbing those houses on that street, the thief have found himself a new place for his thievery so that he would not GE T too much attention. This time, all houses at the is arranged in a circle. That's means the first house was the neighbor of the last one. Meanwhile, the security system for these houses remain the same as for those in the previous street.

Given a list of non-negative integers representing the amount of money in each house, determine the maximum amount of mone Y you can rob tonight without alerting the police.

"Problem Analysis"

On the basis of house robber, one of the limitations of this topic is that all houses form a ring, in which case it is guaranteed that no two adjoining houses should be stolen.

Ideas

Look at the other people's many solutions, in fact, said are unclear, I put my ideas to share to you, certainly let you suddenly enlightened.

First of all: if the House does not constitute a circle, our solution has the following several possibilities

1. The head and tail houses were robbed.

2. The head of the house was robbed, the rear house was not robbed

3. The head House has not been robbed, the rear house is robbed

4. The head and tail of the House have not been robbed

If the house forms a ring, our optimal solution can only choose the following three cases, the second case we have to ensure that the last House can not be robbed, the third case to ensure that the first house can not be robbed, the fourth case has been included in the first two cases. In fact, in the connection of the ring to ensure that one is not stolen, to find out the maximum amount of money from the rest of the house can be stolen.

So on the basis of house robber we just have to make sure the results are only for the second and third cases above. The code is as follows:

1  Public classSolution {2      Public intRobint[] nums) {3         intLen =nums.length;4         if(len = = 0)return0;5         if(len = = 1)returnNums[0];6         7         intPost2 = nums[len-1];8         intPost1 = Math.max (nums[len-1], nums[len-2]);9         intLable1 = 0;Ten         //guaranteed to be the first house not to be stolen One          for(inti = len-3; I >= 1; i--) { A             inttemp =Post1; -Post1 = Math.max (Post1, nums[i] +post2); -Post2 =temp; the         } -         intRESULT1 =Post1; -          -Post2 = 0; +Post1 = Nums[len-2]; -         //To ensure that the last House is not stolen . +          for(inti = len-3; I >= 0; i--) { A             inttemp =Post1; atPost1 = Math.max (Post1, nums[i] +post2); -Post2 =temp; -         } -         intRESULT2 =Post1; -          -         returnMath.max (RESULT1, result2); in     } -}

Leetcode 213. House robber II

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.