Workspace House Robber Ii
0d
Back to questionsDSA Visualizers: 388 Coding Questions

House Robber II visualizer

Learn House Robber II with a step-by-step Dynamic Programming visualizer, JavaScript source code and a worked example. Practice one dimensional dp.

INTERACTIVE EXECUTION STUDIO
LEARN BY DOING

Predict. Execute. Understand.

Follow the source code, inspect live state changes, and predict the next operation before revealing it.

1 / 5Execution step
Reference example · JavaScript
Inspect reference input
[
  2,
  3,
  2
]

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = (a,s)=>{
function f(a){
let p=0,q=0;
for(let x of a){
[p,q]=[q,Math.max(q,p+x)];
s("Choose rob or skip",{
Houses:a,Previous:[p,q]
})
}
return q
}
return Math.max(f(a.slice(0,-1)),f(a.slice(1)))
};

Execution canvas

Active Changed
STEP 1

Choose rob or skip

Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.

Houses2 items
Previous2 items
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Dynamic Programming · One dimensional DP

Follow the computed execution on the reference input. Advance one step at a time, inspect the changing values and compare your prediction with the final output.

Example input

[
  2,
  3,
  2
]

Execution steps

  1. Choose rob or skip

Computed output

3
JavaScript reference implementation
const solve = (a,s)=>{
  function f(a){
    let p=0,q=0;
    for(let x of a){
      [p,q]=[q,Math.max(q,p+x)];
      s("Choose rob or skip",{
        Houses:a,Previous:[p,q]
      })
    }
    return q
  }
  return Math.max(f(a.slice(0,-1)),f(a.slice(1)))
};

Continue learning

Practice related DSA questions in the same topic.