Facebook PixelHouse Robber II — Coding Practice
House Robber IIMedium

House Robber II

Medium 9.1k42% acceptance
ArrayDynamic Programming

This is the same setup as House Robber, except the houses are arranged in a circle: the first and last houses are now adjacent to each other.

Given nums of money in each house, return the maximum amount you can rob without triggering the alarm — meaning you may not rob two adjacent houses, and you may not rob both the first and last house.

Example 1
Input: nums = [2,3,2]
Output: 3
Houses 0 and 2 are adjacent in the circle, so you cannot take both 2s; the best single choice is the middle house, 3.
Example 2
Input: nums = [1,2,3,1]
Output: 4
Rob houses 0 and 2 → 1 + 3 = 4. They are not adjacent even in a circle.
Example 3
Input: nums = [1,2,3]
Output: 3
Constraints
  • 1 ≤ nums.length ≤ 100
  • 0 ≤ nums[i] ≤ 1000
Asked atAmazonGoogleMicrosoft
JavaScript
Loading editor…
Case 1
[2,3,2]
expected: 3
Case 2
[1,2,3,1]
expected: 4
Case 3
[1,2,3]
expected: 3
Case 4
[5]
expected: 5
Case 5
[2,7,9,3,1]
expected: 11
Case 6
[1,1]
expected: 1
Case 7
[200,3,140,20,10]
expected: 340