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
- You cannot rob two adjacent houses. Maximise the total.
- Let best(i) be the most you can take from houses 0..i.
- At house i either skip it (best(i−1)) or rob it (best(i−2) + money[i]): take the larger.
- Only the last two values are needed. O(n).
Watch it run
Loading animation…