House Robber II
Problem Statement
This is House Robber with one change. All the houses sit in a circle, so the first house is the neighbour of the last house. The alarm rule is the same: if you rob two neighbouring houses, the alarm goes off.
Return the most money you can take without robbing two neighbouring houses.
nums, a list of whole numbers. nums[i] is the money in house i. The first and last entries are neighbours.
The largest total you can rob, with no two neighbouring houses both robbed.
Houses 0 and 2 are neighbours on the circle, so you cannot rob both. Rob house 1 (3) alone.
Rob house 0 (1) and house 2 (3). Total 4.
Houses 0 and 2 are neighbours on the circle. The best single choice is house 2 (3).
There is only one house, so rob it.
- • 1 ≤ nums.length ≤ 100
- • 0 ≤ nums[i] ≤ 1000
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Walk the row with two flags: was the previous house taken, and was house 0 taken
Remember each answer for the same house and the same two flags
Run House Robber on houses 0..n−2 and on 1..n−1, then keep the bigger answer
Walk the houses from left to right. At each house, take it or skip it, under two rules. No two houses in a row may both be taken. And if house 0 was taken, the last house is off-limits, because it is next to house 0 on the circle. A flag remembers whether house 0 was taken.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like DP — 1D Linear translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.