#213

House Robber II

Medium 1D DP on two ranges

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

?The problem

Same as House Robber, but the houses form a circle, so the first and last are adjacent.

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

◎Understanding

Really asking: same, but the houses are in a circle.

Why this works: the first and last can't both be robbed, so solve without the last, solve without the first, and take the better.

Example: [2, 3, 2]

without last [2, 3] → 3without first [3, 2] → 3 → answer 3

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Forgetting n = 1, where both ranges are empty and the answer is nums[0].

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