213 House Robber II

Source: Internet
Author: User

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


Solution:

The difference between the title and the first version is that the house here is round and the adjacent two houses cannot be robbed at the same time,

Refer to the first version of the method, you need to note:

A) when the first house is robbed, the last house cannot be robbed. Range [0,n-2]

b) When the first house is not robbed, the last house can be robbed, [1,n-1]


In both cases, the Rob program is executed separately, with the maximum value of two cases being the final result


int Robcore (vector<int> &table,vector<int> &nums,int Start,int end) {

if (start>end)

return 0;

if (table[start]!=-1)

return Table[start];

int Robed=nums[start]+robcore (table,nums,start+2,end);

int Nonrobed=robcore (table,nums,start+1,end);

Table[start]=max (robed,nonrobed);

return Table[start];

}

int Rob (vector<int>& nums) {

int size=nums.size ();

if (size==0)

return 0;

if (size==1)

return nums[0];

vector<int> table (size,-1);

int robed= Nums[0]+robcore (table,nums,2,size-2);

vector<int> table2 (size,-1);

int Nonrobed=robcore (TABLE2,NUMS,1,SIZE-1);

Return Max (robed,nonrobed);

}


Solution Two

As with the first version, it is also possible to use iterative methods for dynamic planning

int Robcore (vector<int> &nums,int start,int end) {

int size=end-start+1;

if (start>end)//must be judged

return 0;

if (size==1)

return Nums[start];

int A=nums[start];

int B=max (nums[start],nums[start+1]);

for (int i=start+2;i<=end;++i) {

int temp=b;

B=max (A+NUMS[I],B);

A=temp;

}

return b;

}

int Rob (vector<int>& nums) {

int size=nums.size ();

if (size==0)

return 0;

if (size==1)

return nums[0];

int robed= Nums[0]+robcore (nums,2,size-2);//Note that this is the default guarantee End>=start

int Nonrobed=robcore (NUMS,1,SIZE-1);

Return Max (robed,nonrobed);

}


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.