House Robber
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.
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.
class Solution {
public:
int rob(vector &num) {
if (num.size() < 1) return 0;
vector result;
result.push_back(num[0]);
for (int i = 1; i < num.size(); ++i) {
int curr_max = 0;
for (int j = 0; j < i - 1; ++j) {
curr_max = max(curr_max, num[i] + result[j]);
}
curr_max = max(curr_max, result[i - 1]);
curr_max = max(curr_max, num[i]);
result.push_back(curr_max);
}
return result[result.size() - 1];
}
};
Comments
Post a Comment