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.
class Solution {
public:
int rob(vector<int> &num) {
const int n = num.size();
if (n == 0) return 0;
vector<int> rob(n);
vector<int> no_rob(n);
rob[0] = num[0], no_rob[0] = 0;
for (int i = 1; i < n; ++i) {
rob[i] = no_rob[i - 1] + num[i];
no_rob[i] = max(rob[i - 1], no_rob[i - 1]);
}
return max(rob[n - 1], no_rob[n - 1]);
}
};
专业抢劫者的算法策略
358

被折叠的 条评论
为什么被折叠?



