LeetCode 198 · Medium
House Robber Explained (LeetCode 198)
Updated Nov 28, 2026 · 3 min read
Walk the houses once. At each one, the best total is either the best so far (skip this house) or this house's money plus the best from two houses back (take it). Keep just those two numbers, prev and best, and the final best is the answer, in O(n) time and O(1) space.

Key points
- ●Trying every allowed pick grows like Fibonacci: 13 for 5 houses, about 267 million for 40.
- ●best[i] = max(best[i - 1], best[i - 2] + coins[i]).
- ●For 2, 7, 9, 3, 1 the running best is 2, 7, 11, 11, 12.
- ●Only two numbers are needed: O(n) time, O(1) space.

The riddle: Leo's shortcut loses 2 coins
Leo has five jars in a row with 2, 7, 9, 3 and 1 coins. He can take any jars he likes, but an alarm goes off if he takes two jars that sit next to each other.
His first idea is every other jar. Starting from the second jar gives 7 + 3 = 10. Starting from the first gives 2 + 9 + 1 = 12. So the shortcut depends on luck, and it can leave coins behind.
That's LeetCode 198, House Robber: given the money in each house along a street, find the most you can take without ever taking two neighbors.
The slow way: try every allowed pick
You could list every set of jars with no two side by side and add each one up. The count grows like the Fibonacci numbers: 13 picks for 5 jars, and about 267 million for 40. Step 2 of the visual sheet above shows why: every jar branches into skip or take, and the branches keep solving the same smaller rows again.
The idea: skip it or take it
Walk the row from left to right. At each jar there are only two choices:
- Skip it. Your best stays the best so far.
- Take it. You get its coins plus the best from two jars back, since the jar right before it is now off limits.
Keep the bigger one:
best[i] = max(best[i - 1], best[i - 2] + coins[i])
Trace it on Leo's jars
| Jar | Coins | Skip | Take | Best |
|---|---|---|---|---|
| 1 | 2 | 0 | 0 + 2 | 2 |
| 2 | 7 | 2 | 0 + 7 | 7 |
| 3 | 9 | 7 | 2 + 9 | 11 |
| 4 | 3 | 11 | 7 + 3 | 11 |
| 5 | 1 | 11 | 11 + 1 | 12 |
The answer is 12, from jars of 2, 9 and 1. Notice jar 4: taking it only reaches 10, so skipping wins and the best stays at 11.
A second example
Try a shorter row: 1, 2, 3, 1. The running best goes 1, then max(1, 2) = 2, then max(2, 1 + 3) = 4, then max(4, 2 + 1) = 4. The answer is 4, from the first and third houses.
Notice the second house. Its 2 beats the first house's 1, so for a moment the best is 2. One step later, taking the third house means going back to the first house's total, and 1 + 3 beats 2. The rule never commits to a choice early, so it can change its mind like that.
The code in Python
You never look further back than two jars, so the table shrinks to two variables: prev (best up to two jars back) and best (best up to the last jar).
def rob(jars):
prev, best = 0, 0
for coins in jars:
prev, best = best, max(best, prev + coins)
return best
The tuple assignment matters. Both new values are computed from the old prev and best before either changes. Run it on 2, 7, 9, 3, 1 and best moves 2, 7, 11, 11, 12.
Complexity
- Time: O(n), one pass.
- Space: O(1), two numbers.
From exponential to a single walk down the row.
The trap to watch for
Greedy shortcuts. Grabbing every other house, or always grabbing the biggest jar left, both feel right and both fail on some inputs. The skip-or-take rule wins because it keeps the best answer for every prefix, so no choice is ever locked in too early. Also check the edge cases: a single house returns its own value, and houses with 0 coins work without any special code.
Frequently asked questions
Why does House Robber look two houses back?
Because taking the current house means its neighbor is off limits. The best total you can add to it is the best you had before that neighbor, which is two houses back.
Why doesn't taking every other house work?
It can miss the best mix. With 2, 7, 9, 3, 1, every other house from the second gives 7 + 3 = 10, while 2 + 9 + 1 gives 12. The skip-or-take rule checks both choices at every house.
What is the time complexity of House Robber?
O(n) time, one pass over the houses, and O(1) space, because you only keep prev and best instead of a full table.
Is House Robber the same as Climbing Stairs?
They share a shape. Both answers at step i depend only on steps i - 1 and i - 2, so both shrink to two running variables. Climbing Stairs adds the two, House Robber takes the max of skip and take.