[Lintcode] House Robber II Dagujieshe two

Source: Internet
Author: User
Tags lintcode

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

This is a extension of house robber.

Example

Nums = [3,6,4], return 6

Leetcode on the original topic, please see my previous blog house robber Ii.

Solution One:

classSolution { Public:    /** * @param nums:an array of non-negative integers. * Return:the maximum amount of money can rob tonight*/    intHouseRobber2 (vector<int>&nums) {        if(Nums.size () <=1)returnNums.empty ()?0: nums[0]; Vector<int> A = nums, B =Nums; A.erase (A.begin ());        B.pop_back (); returnMax (Rob (a), Rob (b)); }    intRob (vector<int> &nums) {        if(Nums.size () <=1)returnNums.empty ()?0: nums[0]; Vector<int> dp{nums[0], Max (nums[0], nums[1])};  for(inti =2; I < nums.size (); ++i) {dp.push_back (max (Dp[i-2] + nums[i], dp[i-1])); }        returnDp.back (); }};

Solution Two:

classSolution { Public:    /** * @param nums:an array of non-negative integers. * Return:the maximum amount of money can rob tonight*/    intHouseRobber2 (vector<int>&nums) {        if(Nums.size () <=1)returnNums.empty ()?0: nums[0]; returnMax (Rob (Nums,0, Nums.size ()-1), Rob (Nums,1, Nums.size ())); }    intRob (vector<int> &nums,intLeftintRight ) {        intA =0, B =0;  for(inti = left; I < right; ++i) {intm = A, n =b; A= n +Nums[i]; b=Max (M, n); }        returnMax (A, b); }};

[Lintcode] House Robber II Dagujieshe two

Related Article

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.