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”.
Brute force approach
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.
Use at most 3 houses so the tree fits the canvas. Example 2 7 9 robs the first house and the last for 11.
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.
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
Pseudocode & Complexity
| Operation | Time |
|---|---|
| Each house | O(1) |
| All houses | O(n) |
| Space | O(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