Skip to main content
← Problem catalogue Understand the pattern. Try it yourself. Review the solution. Practice this pattern →
Medium Dynamic Programming High frequency Solution Available Practice Mode

House Robber II LeetCode Solution

Tags: #Array #Dynamic Programming

Approach Summary

Houses form a circle so first and last are adjacent. Run House Robber on [0..n-2] and [1..n-1], return the max.

Practice Mode 3 Progressive Approaches

Solve it yourself before revealing the code

DSA interviews test reasoning from brute force to optimal. Think through the constraints first.

Algorithmic Pattern

Master the Dynamic Programming Pattern

View Pattern Guide →

Study the core invariant, step-by-step decision framework, and common interview traps for the Dynamic Programming pattern.

Framework: Dynamic Programming • Interview Prep: Tested at FAANG • Explore all Dynamic Programming problems & invariants →

How to Recognize This Pattern

  • Circular arrangement means first and last conflict
  • Run linear House Robber twice on overlapping ranges

Complexity Analysis

Time Complexity

O(n)

Space Complexity

O(1)

Tags

Array Dynamic Programming

Frequently Asked Questions

What is the optimal time and space complexity for House Robber II?
The optimal solution for House Robber II achieves O(n) runtime complexity and requires O(1) auxiliary space. Time complexity is O(n) because we run two linear passes of House Robber I over slices of size n - 1. Space complexity is O(1) because each pass uses only two scalar variables.
How do I identify that House Robber II requires the Dynamic Programming pattern?
In technical interviews, look for key problem signals: Circular arrangement means first and last conflict; Run linear House Robber twice on overlapping ranges. Recognizing these invariants allows you to bypass naive brute-force exhaustive search in favor of Dynamic Programming strategy.
What critical edge cases must be handled in House Robber II?
Essential edge cases to verify include: Single house (e.g. nums = [5]; returns nums[0] immediately); Two houses (returns max(nums[0], nums[1])); Three houses (returns max(nums[0], nums[1], nums[2])); All houses have 0 value. Always walk through these scenarios with your interviewer before coding.
Which tech companies frequently ask House Robber II?
House Robber II is a Medium-difficulty question tagged with high recurrence. It is actively asked in technical screening and onsite interview loops at Amazon, Google, Airbnb.
Buy me a coffee
Daily Practice
0/0