Middle-题目58：213. House Robber II

Note: This is an extension of House Robber.

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

Easy-题目26，但是现在房子围成了环形，仍然求最大利益。

int rob(int* nums, int numsSize) {
if(numsSize==0)
return 0;
else if(numsSize==1)
return nums[0];
else if(numsSize==2)
return nums[0]>nums[1]?nums[0]:nums[1];
else
return max(rob1(nums,numsSize-1),rob1(nums+1,numsSize-1));
}
int max(int a,int b) {
return a>b?a:b;
}
int rob1(int* nums, int numsSize) {
if(numsSize==0)
return 0;
if(numsSize==1)
return nums[0];
else if(numsSize==2)
return nums[0]>nums[1]?nums[0]:nums[1];
else
{
int i;
int dp1,dp2,dp;

dp1=nums[0];
dp2=nums[0]>nums[1]?nums[0]:nums[1];
for(i=2;i<numsSize;i++)
{
dp=dp2>dp1+nums[i]?dp2:dp1+nums[i];
dp1=dp2;
dp2=dp;
}
return dp;
}
}

1ms，众数100%
Cmershen的碎碎念：

• 本文已收录于以下专栏：

举报原因： 您举报文章：Middle-题目58：213. House Robber II 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)