// DP
class Solution {
public:
int rob(vector<int> &num) {
if (num.size() <= 1) return num.empty() ? 0 : num[0];
vector<int> dp = {num[0], max(num[0], num[1])};
for (int i = 2; i < num.size(); ++i) {
dp.push_back(max(num[i] + dp[i - 2], dp[i - 1]));
}
return dp.back();
}
};
相关文章
- 02-06House Robber III leetcode 动态规划
- 02-06Codeforces Round #359 (Div. 2) B. Little Robber Girl's Zoo 水题
- 02-06【leetcode】House Robber & House Robber II(middle)
- 02-06213. House Robber II
- 02-06LeetCode 198. 打家劫舍(House Robber)
- 02-06198.House Robber
- 02-06534. House Robber II
- 02-06Leetcode 198. House Robber
- 02-06213. House Robber II
- 02-06198. House Robber