LeetCode || 198. House Robber
2016-02-24 20:41
260 查看
198. 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.
题目分析:典型的动态规划的题目。耗时0ms。
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.
题目分析:典型的动态规划的题目。耗时0ms。
class Solution { public: int rob(vector<int>& nums) { if(nums.size() <=0) return 0; if(nums.size() == 1) return nums[0]; int maxV = nums[0]; vector<int> arr; arr.push_back(nums[0]); arr.push_back(nums[1]); for(int i = 2;i<nums.size();i++) { arr.push_back(maxV + nums[i]); //update max if(arr[i-1] > maxV) maxV = arr[i-1]; } return max(arr[arr.size()-1], arr[arr.size()-2]); } };
相关文章推荐
- Struts2之文件下载
- Codeforces 628E Zbazi in Zeydabad 树状数组
- Mathtype中批量修改公式的字体和大小
- 关于MKNetworking自己维护
- mysql数据类型
- java 文件输入输出流 文件夹的复制
- LCA在线算法ST算法
- 3、C++新的关键字
- hdu 2546 饭卡
- Android ROM开发(一)——Windows下Cygwin和Android_Kitchen厨房的安装
- Android ROM开发(一)——Windows下Cygwin和Android_Kitchen厨房的安装
- Git学习笔记(3)——撤销修改与文件删除
- 1030. 完美数列(25)
- hdu4028 The time of a day (map+dp)
- 计算文本高度
- 整数排序 华为入职前培训练习题
- [iOS]AppDelegate方法简单说明
- python学习之10 随机数random
- 【JSON异常系列】new JSONObject对象时卡死原因
- Java高级软件工程师面试考纲总结