LeetCode 213 · Medium

House Robber II Explained (LeetCode 213)

Updated Nov 28, 2026 · 3 min read

Because the houses form a circle, the first and last can't both be taken. So solve the ordinary straight-row House Robber twice, once without the last house and once without the first, and return the bigger answer. Each pass is O(n) time and O(1) space.

House Robber II reel cover

Key points

  • ●In a ring the first and last houses are neighbors, so they can't both be picked.
  • ●Every valid pick leaves out the first house, the last house, or both.
  • ●Solve nums[:-1] and nums[1:] with the House Robber 198 helper and take the max.
  • ●For 5, 1, 1, 5 a straight row gives 10, but the ring gives 6.
  • ●Two passes are still O(n) time and O(1) space.
Step-by-step visual sheet: House Robber II
The full visual sheet, made with Edithly.Open the interactive sheet

The riddle: Nia's tables sit in a ring

Nia serves 4 tables with tips of 5, 1, 1, 5. She can collect from any tables she likes, but never from two tables side by side.

If the tables stood in a straight row, she'd take the first and the last for 10. But these tables sit in a ring, so the first and last are neighbors. Taking both breaks the rule, and the best she can do drops to 6.

That is LeetCode 213, House Robber II: the same skip-or-take problem as House Robber 198, except the houses form a circle, so the first one and the last one touch.

The slow way: try every allowed pick

You could list every set of tables with no two neighbors and add up each one. For 4 tables in a ring there are 7 allowed picks, counting the empty one. That number grows exponentially with the number of tables, so it falls apart well before the 100-house limit.

The idea: break the ring

The ring adds exactly one extra conflict: the first and last tables. Any allowed pick leaves out at least one of them. So split the problem in two:

  • Without the last table: solve the straight row [5, 1, 1].
  • Without the first table: solve the straight row [1, 1, 5].

Together those two cases cover every valid choice on the ring. If the best pick skips the last table, the first row finds it. If it skips the first table, the second row finds it. If it skips both, either row finds it. Each one is a plain House Robber 198 row, and you already know how to solve that. Step 2 of the visual sheet above draws the ring being cut both ways.

Trace both rows

The row rule: at each table, either skip it (keep the best so far) or take it (its tip plus the best from two tables back). prev is the best two back, best is the best so far.

Row Table prev best
[5, 1, 1] 5 0 5
1 5 5
1 5 6
[1, 1, 5] 1 0 1
1 1 1
5 1 6

Both rows give 6, so the answer is max(6, 6) = 6.

The code in Python

def row(a):
    prev = best = 0
    for x in a:
        prev, best = best, max(best, prev + x)
    return best

def rob(nums):
    return nums[0] if len(nums) == 1 else max(row(nums[:-1]), row(nums[1:]))

row is the House Robber 198 solution. The tuple assignment updates prev and best from the old values at the same time, which is what you want. rob calls it on the two slices and keeps the bigger result.

Complexity

  • Time: O(n). Two passes over at most n - 1 houses each.
  • Space: O(1) in the helper, two variables. The slices copy the list; pass indexes instead if that matters.

The trap to watch for

The single-house case. With nums = [7], both nums[:-1] and nums[1:] are empty and row returns 0 for each. One house has no neighbor, so the right answer is 7. That's why the code checks len(nums) == 1 first. Two houses are fine without a special case: for [2, 3] the rows are [2] and [3], and the answer is 3.

The other trap is solving the row once and hoping. Running 198 on the full ring [5, 1, 1, 5] gives 10, which picks two tables that are neighbors in the circle.

Frequently asked questions

Why does House Robber II solve the problem twice?

Any allowed choice skips the first house or skips the last house, since the two are neighbors. Solving the row without the last house covers every choice that skips it, and solving without the first covers the rest. The bigger of the two is the answer.

What is the difference between House Robber and House Robber II?

House Robber (198) has the houses in a straight line. House Robber II (213) puts them in a circle, which adds one extra conflict: the first and last houses. You reuse the 198 solution as a helper and call it on two slices.

Why do you need a special case for one house?

With a single house, both slices nums[:-1] and nums[1:] are empty and each returns 0. But one house has no neighbors, so you can take it. Return nums[0] directly.

What is the time complexity of House Robber II?

O(n) time, because each of the two passes looks at each house once, and O(1) extra space, because the helper keeps only two running numbers. Slicing the list makes copies; pass start and end indexes instead if you want truly constant space.

Turn any problem or document into a visual sheet like this one

Paste a LeetCode problem, a PDF or a link into Edithly and get a step-by-step sheet you can study, share or quiz yourself on.

Make this sheet in Edithly, free