#198

House Robber

Medium 1D DP (take / skip)

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Houses in a row hold money. You can't rob two adjacent houses. Return the most money you can rob.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: rob houses without robbing two neighbours.

Why this works: at each house, either rob it (plus the best two houses back) or skip it (the best one house back).

Example: [2, 7, 9, 3, 1]

best: 2, 7, 11, 11, 12 → 12 (2 + 9 + 1)

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Robbing all even or all odd houses — the best choice can skip two in a row.

Solve on LeetCode ↗
Complexity
  • Time: O(n).
  • Space: O(1).
Topic: Dynamic Programming
🎬 Episodes 86–96: video coming soon