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 for adding this problem and creating all test cases. Also thanks to for adding additional test cases.
to see which companies asked this question
一道动态规划的题目,想了很久,没想出来。== 最后看陆草纯的代码的。http://www.cnblogs.com/ganganloveu/p/4417485.html
最开始我很简单的想,因为都是非负数嘛,所以就是越多加越好,所以就试了奇数号的加起来求和,偶数号的加起来求和。= = 然而这样行不通的,例如{1,3,1,3,100},{2,1,1,2}
后来看了tag是动态规划的题目,= = 动态规划真是难啊。就是想不明白。。。看了要好好学学动态规划了。。
1 class Solution { 2 public: 3 int rob(vector & nums) { 4 int s=nums.size(); 5 if(s==0) return 0; 6 if(s==1) return nums[0]; 7 vector maxn(s,0); 8 maxn[0]=nums[0]; 9 maxn[1]=max(nums[0],nums[1]);10 for(int i=2;i