House Robber II
medium
dynamic-programming
arrays
You are a robber planning to loot houses along a street, but here the houses are arranged in a circle, so the first and last houses are adjacent. Each house holds nums[i] dollars, and robbing two directly adjacent houses on the same night triggers the alarm.
Return the maximum amount of money you can rob without alerting the police.
Input / output
- Input:
nums: int[] - Output:
int(maximum loot)
Examples
nums = [2,3,2]returns3because houses0and2are adjacent, so you can only take the single house holding3.nums = [1,2,3,1]returns4by robbing houses0and2(1 + 3).nums = [0]returns0.
Constraints
1 <= nums.length <= 1000 <= nums[i] <= 1000
Follow-up The only wrinkle versus the linear House Robber is the wrap-around adjacency. Can you reuse the linear solution as a black box instead of writing a new circular recurrence?
Examples
Example 1
Input: nums = [2,3,2]
Output: 3
Example 2
Input: nums = [1,2,3,1]
Output: 4
Example 3
Input: nums = [0]
Output: 0