leetcode 198. House Robber
2016-04-09 14:52
399 查看
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.
这个题目是动态规划问题。设至第 i 家时的最大金额为maxsum[i],第 i 家的金额为 nums[i] , 则递推式可以写为 maxsum[i] = max( maxsum[i-2]+nums[i] , maxsum[i-1] ).
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.
这个题目是动态规划问题。设至第 i 家时的最大金额为maxsum[i],第 i 家的金额为 nums[i] , 则递推式可以写为 maxsum[i] = max( maxsum[i-2]+nums[i] , maxsum[i-1] ).
class Solution { public: int rob(vector<int>& nums) { int n=nums.size(); if(n==0) return 0; else if(n==1) return nums[0]; else{ vector<int> maxsum(n,0); maxsum[0]=nums[0]; maxsum[1]=max(nums[0],nums[1]); for(int i=2;i<n;i++){ maxsum[i]=max(maxsum[i-2]+nums[i],maxsum[i-1]); } return maxsum[n-1]; } } };
相关文章推荐
- kafka分布式搭建
- Git 常用指令
- 【Python笔记】图片处理库PIL的源代码安装步骤
- MsysGit下GUI乱码问题解决
- 深入理解javascript原型和闭包系列 深入理解javascript原型和闭包(16)——补this
- Python 练习实例4
- Android Studio 1.5 配置AndroidAnnotations
- js 截取或者替换字符串中的数字
- 大型网站系统架构的演化【转】
- 对结对作业的总结与体会
- 设计模式之观察者模式浅析
- 网易笔试题 路灯
- 关于在maven项目下,从一个jsp页面通过重定向跳转到另一个jsp页面时出现404的错误的解决方法
- 深入理解javascript原型和闭包系列 深入理解javascript原型和闭包(15)——闭包
- CCF-201509-3 模板生成系统
- 关于Java是强类型语言的相关解释
- 文本格式化
- Qt中让子窗体不继承副窗体的方法
- View.INVISIBLE和View.GONE的区别
- python-MySQL学习笔记-第一章链接到并创建数据库