1. Skip state
Best total when the previous house is not taken.
let skip = 0Typorithm · medium
Step 1 of 8 · intro card
Dynamic Programming
House Robber compares taking the current house with skipping it while respecting the no-adjacent rule.
Adjacent houses cannot both be chosen.
The state split tracks whether the previous house was taken.
Each house updates take and skip choices.
Choice: take current or skip current.
Taking current uses the previous skip state.
Skipping current keeps the best previous result.
Time O(n), space O(1).
function rob(nums) {
let skip = 0;
let take = 0;
for (const money of nums) {
const bestWithCurrent = skip + money;
skip = Math.max(skip, take);
take = bestWithCurrent;
}
return Math.max(skip, take);
}Connect each part of the code to the concept before you type.
Best total when the previous house is not taken.
let skip = 0Best total when the previous house is taken.
let take = 0Taking current requires skipping the previous house.
const bestWithCurrent = skip + moneyFinal answer may end by taking or skipping the last house.
return Math.max(skip, take)Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.
Algorithm lab · interactive
InputHouses [2,7,9,3,1,4]
Follow temporary values, local state and supporting structures at this step.
↗ The 7-cell working table above is itself auxiliary memory. Input values are shown separately.
Input · not auxiliary memory
Input6 entries
Around this step
Small, reproducible examples · ← → step · Space play/pause · Home/End first/last state