Leetcode-house-robber
https://leetcode.com/problems/house-robber/
Q:
You is a professional robber planning to rob houses along a street. Each house have a certain amount of money stashed, the only constraint stopping all from robbing each of the them are that Adjac ENT houses have security system connected and it would automatically contact the police if the adjacent houses were broken Into on the same night.
Given a list of non-negative integers representing the amount of money in each house, determine the maximum amount of mone Y you can rob tonight without alerting the police.
1 classsolution{2 Public:3 intRob (vector<int>&nums) {4 intLen =nums.size ();5 if(len==0)return 0;6 if(len==1)returnnums[0];7vector<int> Value (len+1,0);8value[0] =0;9value[1] = nums[0];Ten for(intI=2; i<len+1; i++){ OneValue[i]=max (value[i-1], value[i-2]+nums[i-1]); A } - returnValue[len]; - } the};
Idea: The DP problem should be dynamically planned. Never written before ... At first the recursion was written and timed out.
Later I looked at someone else's code, all using arrays to store the data.
References:
Http://www.cnblogs.com/ganganloveu/p/4417485.html
Http://www.cnblogs.com/sdjl/articles/1274312.html
Another way to do this:
Http://www.meetqun.com/thread-8304-1-1.html
The wrong words were written before:
classsolution{ Public: intRob (vector<int>&nums) { if(nums.size () = =1)returnnums[0]; //int len = Num.size (); //vector<int>::iterator iter1 = Nums.begin (); //Vector<int>::iterator iter2 = Nums.end ()-1;vector<int> Subnums1 (Nums.begin (), Nums.end ()-1); Vector<int> Subnums2 (Nums.begin (), Nums.end ()-2); returnMax (Rob (SUBNUMS1), Rob (SUBNUMS2) + * (Nums.end ()-1)); }};
Leetcode-house robber