function rob(nums) {const n = nums.length;
if (n === 0) return 0;
const dp = new Array(n).fill(0);◄
dp[0] = nums[0];
for (let i = 1; i < n; i++) {const rob = (i >= 2 ? dp[i-2] : 0) + nums[i];
const skip = dp[i-1];
dp[i] = Math.max(rob, skip);
}
return dp[n-1];
}
At every house you face one binary choice: rob it, or skip it. That choice is all the state you need.
If you ROB house i, you cannot have robbed i − 1, so your total is dp[i − 2] + nums[i]. If you SKIP it, your total is just the best through i − 1, i.e. dp[i − 1].
So dp[i] = max(dp[i − 2] + nums[i], dp[i − 1]) — each house only ever looks two steps back.
Seed it with dp[0] = nums[0], and the last dp value is the answer. (Since only two previous values matter, this can be squeezed to O(1) space with two rolling variables.)