2dbi

House Robber II

viaLeetCode

Problem Houses in a CIRCLE each hold money; adjacent houses can't both be robbed, and house 0 and house n−1 are adjacent. Maximize the take.

Input / Output

  • Input: int array nums. Output: max sum.

Constraints

  • n up to 100 classically; O(n)/O(1) expected.

Example

  • [2,3,2] → 3 (can't take both 2s — they're circular neighbors); [1,2,3,1] → 4.

Expected approach

  • The circular constraint only couples first and last: they can't BOTH be robbed. So run the linear House Robber twice — on nums[0..n−2] (first allowed, last excluded) and nums[1..n−1] — and take the max; single-house edge case handled explicitly. Linear subroutine: rolling take/skip DP, O(n)/O(1). The decomposition argument (case analysis on whether house 0 is robbed) is what's evaluated; House Robber III (tree) is the next escalation.
Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account