↓Skip to main content
  1. LeetCode/

LeetCode 213: House Robber II

·2 mins· ·
LeetCode Medium Dynamic-Programming
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-04-25 Source: Day 11 learning note

Study Context
#

This article is rebuilt from the exact LeetCode section in the learning note. I kept the note’s repair points, comparison points, and common mistakes, while removing unrelated non-LeetCode material from the same day.

Learning Note Extract
#

Problem 2 - LC 213 House Robber II Timed Re-Solve
#

  • Status: Good enough.
  • Pattern: Circular array -> split into two linear robber problems.

Key Constraint
#

first house and last house are adjacent

So a valid answer must be one of:

exclude last house
exclude first house

Interview-Ready Explanation
#

Because the houses are arranged in a circle, the first and last houses are adjacent, so I cannot rob both. I split the problem into two linear House Robber I cases: rob nums[:-1] or rob nums[1:], then take the maximum of those two answers.

Organized Notes
#

The circular constraint is the whole problem. Once house 0 and house n - 1 are adjacent, a single linear robber pass can accidentally choose both. Splitting into nums[:-1] and nums[1:] removes that conflict: every valid optimal answer either excludes the last house or excludes the first house. The helper is exactly LC 198 with two rolling variables, and the n == 1 case must be handled before slicing.

Clean Solution
#

The note above captures the reasoning and the mistakes to avoid. The implementation below is the version I would submit.

from typing import List

class Solution:
    def rob(self, nums: List[int]) -> int:
        if len(nums) == 1:
            return nums[0]

        def rob_line(arr):
            prev2 = prev1 = 0
            for x in arr:
                prev2, prev1 = prev1, max(prev1, prev2 + x)
            return prev1

        return max(rob_line(nums[:-1]), rob_line(nums[1:]))

Complexity
#

Time O(n), Space O(1).

Mistakes To Watch
#

  • Running LC 198 directly on a circle.
  • Forgetting n=1.

Final Interview Explanation
#

I would first call out the circular edge between the first and last house. Because an optimal solution cannot include both, I solve two linear robber subproblems, excluding one end each time, and return the larger result. The single-house case must be handled separately.

Related

LeetCode 322: Coin Change
·2 mins
LeetCode Medium Dynamic-Programming Unbounded-Knapsack
LeetCode note for Coin Change, rebuilt from the original learning note
LeetCode 740: Delete and Earn
·2 mins
LeetCode Medium Dynamic-Programming
LeetCode note for Delete and Earn, rebuilt from the original learning note
LeetCode 3840: House Robber V
·2 mins
LeetCode Medium Dynamic-Programming Array
LeetCode 3840 note: houses conflict only when adjacent with the same color; take a house outright when the colors differ, and do the House Robber choice only when they match
LeetCode 2054: Two Best Non-Overlapping Events
·2 mins
LeetCode Medium Dynamic-Programming Binary-Search Sorting Heap
LeetCode 2054 note: with at most two events, pair each event with the most valuable event that ends before it starts; sort by end time and use a prefix max with binary search
LeetCode 1888: Minimum Number of Flips to Make the Binary String Alternating
·2 mins
LeetCode Medium Sliding-Window String Dynamic-Programming
LeetCode 1888 note: rotations become length-n windows over s + s; the window’s mismatches against 0101… are diff, and against 1010… they are n - diff
LeetCode 1493: Longest Subarray of 1's After Deleting One Element
·2 mins
LeetCode Medium Sliding-Window Dynamic-Programming Array
LeetCode 1493 note: find the longest window with at most one 0; since one element must be deleted, the answer is the window length minus 1