House Robber
2015-09-27 21:02
190 查看
【题目描述】
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.
【思路】
动态规划题目,掌握的不够好。
maxval[i]=max(maxval[i-2]+nums[i],maxval[i-1])
【代码】
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.
【思路】
动态规划题目,掌握的不够好。
maxval[i]=max(maxval[i-2]+nums[i],maxval[i-1])
【代码】
class Solution { public: int rob(vector<int>& nums) { int n=nums.size(); vector<int> maxval(n,0); if(n==0) return 0; if(n==1) return nums[0]; maxval[0]=nums[0]; maxval[1]=max(nums[0],nums[1]); for(int i=2;i<n;i++){ maxval[i]=max(maxval[i-2]+nums[i],maxval[i-1]); } return maxval[n-1]; } };
相关文章推荐
- 例题6-16 单词 并查集判断连通+有向图欧拉路规律
- 那些年想不起来的Git命令
- RC4加密算法
- UIView
- java SE复习笔记41
- 烦躁
- Devexpress汉化修改 已经汉化过后生成的*.resources.dll文件 z
- iOS6之后 NSAttributedString 福利
- 组合数(dfs)
- Java工具类(二) 模版导出工具类
- 软工视频总结(前期)
- Basic Calculator
- OCP-V13-008
- 两种方法实现stm32的8个串口通信
- jsonp的工作原理
- ASP.NET WebAPI 05 参数绑定
- HTML使用canvas载入图片
- 三个水杯 21 (bfs 转化) 好题
- 编译lamp
- 关于测试方法以及测试用例设计的一些总结