Typorithm · medium

House Robber

▷ Interactive algorithm labhouse-robber-dpdynamic-programmingchoicespace-optimized
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn House Robber

0%

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.

Preparing algorithm lab…
01

Choice: take current or skip current.

02

Taking current uses the previous skip state.

03

Skipping current keeps the best previous result.

04

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);
}

Read the implementation

Connect each part of the code to the concept before you type.

1. Skip state

Best total when the previous house is not taken.

let skip = 0

2. Take state

Best total when the previous house is taken.

let take = 0

3. Take current

Taking current requires skipping the previous house.

const bestWithCurrent = skip + money

4. Best final

Final answer may end by taking or skipping the last house.

return Math.max(skip, take)

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

House Robber: Visual Walkthrough & Practice · Typorithm | ApplyDjinn