Skip to content
StackDecode
Dynamic programmingAnimated

House Robber

Max loot without taking two adjacent houses.

Time

O(n)

Extra space

O(1)

Use it for

No-adjacent selection.

How it works

  1. You cannot rob two adjacent houses. Maximise the total.
  2. Let best(i) be the most you can take from houses 0..i.
  3. At house i either skip it (best(i−1)) or rob it (best(i−2) + money[i]): take the larger.
  4. Only the last two values are needed. O(n).

Watch it run

Loading animation…