DSADP1D

House Robber Visualizer

What is House Robber

Houses sit in a line. Taking house i forbids house i−1. The best total through i is the better of “skip i” and “take i plus the best through i−2”.

  • dp[i] = max(dp[i-1], dp[i-2] + nums[i])
  • Two variables store those two previous answers.
  • Not greedy: a small house can unlock a much larger later one.

Brute force approach

From house i either rob it and jump to i+2, or skip to i+1. Overlapping suffixes explode to O(2^n).

From house i the brute-force choice is rob it and jump to i+2, or skip it and go to i+1. The same index is solved on many branches. Memoization keeps one answer per index.

Try a small input

Use at most 3 houses so the tree fits the canvas. Example 2 7 9 robs the first house and the last for 11.

Answer: 0 Brute calls: 0 Memo calls: 0 Calls saved: 0
Recursive brute force
def rob(i):
if i >= n: return 0
return max(nums[i]+rob(i+2), rob(i+1))
Step 0

Memoized approach

take = max(take, skip + nums[i]), then slide the window. One linear pass, O(1) extra memory.

Each state is solved once. A dashed node is a cache hit: the call returns immediately and that branch stops.

Memoized recursion
def rob(i, memo):
if i in memo: return memo[i]
if i >= n: return 0
memo[i] = max(nums[i]+rob(i+2), rob(i+1))
return memo[i]
Step 0

How this visualizer works

Array 2, 7, 9, 3, 1. The trace picks 2+9+1 = 12, skipping the adjacent 7 and 3. Step through the DP table. The live code on the right is the DP recurrence — brute force is shown in Implementations below so you can see why it TLE.

Interactive DP trace

dp_table.state

Input
DP table
Cell being filled
Source / window
Already computed

current_action

Load an input, then press Play or Step.

live_trace.py — DP fill

1def rob(nums):
2skip, take = 0, 0
3for x in nums:
4skip, take = take, max(take, skip + x)
5return take

stdout — decision log

// each table write is recorded here
Speed Ready

Pseudocode & Complexity

dp_recurrence.txt

1def rob(nums):
2skip, take = 0, 0
3for x in nums:
4skip, take = take, max(take, skip + x)
5return take

complexity.md

OperationTime
Each houseO(1)
All housesO(n)
SpaceO(1)

Brute force explores the same subproblems many times and will TLE on the hidden tests. The DP codes below reuse each answer once.

Implementations

dp.py

1def rob_brute(nums, i=0):
2if i >= len(nums):
3return 0
4take = nums[i] + rob_brute(nums, i + 2)
5skip = rob_brute(nums, i + 1)
6return max(take, skip)
1def rob_dp(nums):
2skip = 0
3take = 0
4for x in nums:
5nxt = max(take, skip + x)
6skip = take
7take = nxt
8return take
1int rob_brute(int *nums, int n, int i) {
2int take;
3int skip;
4if (i >= n) {
5return 0;
6}
7take = nums[i] + rob_brute(nums, n, i + 2);
8skip = rob_brute(nums, n, i + 1);
9return take > skip ? take : skip;
10}
1int rob_dp(int *nums, int n) {
2int skip = 0;
3int take = 0;
4int i;
5int nxt;
6for (i = 0; i < n; i++) {
7nxt = take > skip + nums[i] ? take : skip + nums[i];
8skip = take;
9take = nxt;
10}
11return take;
12}