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.
Simply put, the title means, a series of numbers, not to go to the adjacent value, then how to take the maximum value. DP, expression: ret[i] = max (ret[i-1], ret[i-2] + nums[i]);
At first think of another possibility is ret[i-1] may be ret[i-2], but here does not affect, because or ret[i-2] + num[i] Larger, the code is as follows:
1 classSolution {2 Public:3 intRob (vector<int>&nums) {4 intSZ =nums.size ();5vector<int> Maxret =nums;6 if(SZ = =0)return 0;7 if(SZ = =1)returnnums[0];8 if(SZ = =2)returnMax (nums[0], nums[1]);9 inti;Tenmaxret[0] = nums[0]; Onemaxret[1] = max (nums[0], nums[1]); A for(i =2; I < sz; ++i) { -Maxret[i] = max (Maxret[i-2] + nums[i], maxret[i-1]); - } the returnMaxret[i-1]; - } -};
Leetcode oj:house robber (residential thief)