https://leetcode.com/problems/house-robber/
- 1D DP
At each house, choose between robbing it plus the best up to two houses back or skipping it.
O(n)
O(1)
class Solution {
public int rob(int[] nums) {
int prev2 = 0, prev1 = 0;
for (int num : nums) {
int take = prev2 + num;
int skip = prev1;
int current = Math.max(take, skip);
prev2 = prev1;
prev1 = current;
}
return prev1;
}
}