# Middle-题目58：213. House Robber II

73人阅读 评论(0)

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的碎碎念：

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：63302次
• 积分：3275
• 等级：
• 排名：第10713名
• 原创：270篇
• 转载：53篇
• 译文：0篇
• 评论：8条
文章分类
阅读排行
最新评论