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 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.
Credits:
Special thanks to @ifanchu for adding this problem and creating all test cases. Also thanks to @ts for adding additional test cases.
Dynamic planning, setting maxv[i] represents to the first house position, the maximum benefit.
Recursive relationship maxv[i] = max (Maxv[i-2]+num[i], maxv[i-1])
classSolution { Public: intRob (vector<int> &num) { intn =num.size (); if(n = =0) return 0; Else if(n = =1) returnnum[0]; Else{vector<int> Maxv (N,0); maxv[0] = num[0]; maxv[1] = max (num[0], num[1]); for(inti =2; I < n; i + +) Maxv[i]= Max (maxv[i-2]+num[i], maxv[i-1]); returnmaxv[n-1]; } }};
"Leetcode" 198. House robber