House Robber[leetcode]题解 c++
2015-06-21 21:24
537 查看
House Robber
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.
Credits:
Special thanks to @ifanchu for adding this problem and creating all test cases. Also thanks to @ts for
adding additional test cases.
题目大意:在一串正整数串当中取一组整数串,且整数串中的数两两之间不相邻,求得许多串方案当中和最大的串,输出和。
一开始简单以为只是一个判断奇数偶数然后对于一些特殊情况特判一下的情况,后来才发现想当然了,改用动态规划的办法但是也就是递推了一下。
对于数组长度小等于3的特殊处理,数组长度大于3的采用的状态转移方程为:
dp[i] = max(dp[i-2],dp[i-3])+nums[i];
思路:就是以局部最大推导整体最大,需要判断的只是与第i-2的数相加,还是与第i-3的数相加,而第i-4自然不用考虑。
实现c++代码:
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<map>
#include<vector>
#include<iostream>
using namespace std;
class Solution {
public:
int rob(vector<int>& nums) {
if(nums.size() == 0)
{
return 0;
}
if(nums.size() == 1)
{
return nums[0];
}
if(nums.size() == 2)
{
return max(nums[0],nums[1]);
}
if(nums.size() == 3)
{
return max(nums[0]+nums[2],nums[1]);
}
int dp[nums.size()];
dp[0] = nums[0];
dp[1] = nums[1];
dp[2] = nums[0]+nums[2];
for(int i = 3;i<nums.size();i++)
{
dp[i] = max(dp[i-2],dp[i-3]) + nums[i];
}
//注意数组边界问题
return max(dp[nums.size()-3],max(dp[nums.size()-1],dp[nums.size()-2]));
}
};
int main()
{
vector<int> v;
for(int i = 0;i<10;i++)
{
v.push_back(i);
}
Solution s;
cout << s.rob(v)<<endl;
return 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.
Credits:
Special thanks to @ifanchu for adding this problem and creating all test cases. Also thanks to @ts for
adding additional test cases.
题目大意:在一串正整数串当中取一组整数串,且整数串中的数两两之间不相邻,求得许多串方案当中和最大的串,输出和。
一开始简单以为只是一个判断奇数偶数然后对于一些特殊情况特判一下的情况,后来才发现想当然了,改用动态规划的办法但是也就是递推了一下。
对于数组长度小等于3的特殊处理,数组长度大于3的采用的状态转移方程为:
dp[i] = max(dp[i-2],dp[i-3])+nums[i];
思路:就是以局部最大推导整体最大,需要判断的只是与第i-2的数相加,还是与第i-3的数相加,而第i-4自然不用考虑。
实现c++代码:
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<map>
#include<vector>
#include<iostream>
using namespace std;
class Solution {
public:
int rob(vector<int>& nums) {
if(nums.size() == 0)
{
return 0;
}
if(nums.size() == 1)
{
return nums[0];
}
if(nums.size() == 2)
{
return max(nums[0],nums[1]);
}
if(nums.size() == 3)
{
return max(nums[0]+nums[2],nums[1]);
}
int dp[nums.size()];
dp[0] = nums[0];
dp[1] = nums[1];
dp[2] = nums[0]+nums[2];
for(int i = 3;i<nums.size();i++)
{
dp[i] = max(dp[i-2],dp[i-3]) + nums[i];
}
//注意数组边界问题
return max(dp[nums.size()-3],max(dp[nums.size()-1],dp[nums.size()-2]));
}
};
int main()
{
vector<int> v;
for(int i = 0;i<10;i++)
{
v.push_back(i);
}
Solution s;
cout << s.rob(v)<<endl;
return 0;
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++编写简单的打靶游戏
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- C++中拷贝构造函数的应用详解
- C++中引用(&)的用法与应用实例分析
- C++使用CriticalSection实现线程同步实例
- C++智能指针实例详解
- 解析C++ 浮点数的格式化输出