# Easy-题目26：198. House Robber

164人阅读 评论(0)

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.

dp[n]=max(dp[n-1],dp[n-2]+money[n])

int rob(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;
}
}

0ms，beats3.60%，众数0ms，96.40%
Cmershen的碎碎念：

0
0

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