"Leetcode" 213. House robber II

Source: Internet
Author: User

House robber II

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.

Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.

The difference with House robber is that nums[0] and nums[n-1] cannot be included,

Thus the equivalent of nums[0...n-2] and nums[1...n-1] takes a larger value between the two.

classSolution { Public:    intRob (vector<int>&nums) {        if(Nums.empty ())return 0; if(nums.size () = =1)            returnnums[0]; Vector<int>nums1 (nums); Vector<int>nums2 (nums);        Nums1.erase (Nums1.begin ());        Nums2.pop_back (); returnMax (Originrob (NUMS1), Originrob (NUMS2)); }    intOriginrob (vector<int>&nums) {        if(Nums.empty ())return 0; intPrev2 =0; intPREV1 = nums[0];  for(inti =1; I < nums.size (); i + +)        {            intcur = max (prev2+Nums[i], PREV1); Prev2=Prev1; Prev1=cur; }        returnPrev1; }};

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