house-robber-ii.sh — zsh
dynamic-programmingarrays

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

  1. nums = [2,3,2] returns 3 because houses 0 and 2 are adjacent, so you can only take the single house holding 3.
  2. nums = [1,2,3,1] returns 4 by robbing houses 0 and 2 (1 + 3).
  3. nums = [0] returns 0.

Constraints

  • 1 <= nums.length <= 100
  • 0 <= 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