House robber
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 bro Keninto 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.
Robbers enter the house theft, and can not enter two adjacent room theft, for maximum benefit.
That is, in an array to select a number of numbers, the selected number can not be adjacent, to find their maximum and.
Dynamic planning.
Max[i] is the maximum profit for the house to the first room.
Max[i] = max (Max[i-2] + nums[i], max[i-1])
Class Solution {public: int Rob (vector<int>& nums) { int n = nums.size (); Vector<int>max (n,0); if (n = = 0) return 0; if (n = = 1) return nums[0]; Max[0] = nums[0]; MAX[1] = max (max[0],nums[1]); for (int i = 2;i < n;i++) { Max[i] = MAX (Max[i-2] + nums[i],max[i-1]); } return max[n-1];} ;
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Leetcode-198-house robber