House Robber II

MediumDynamic Programming I: 1D

Houses stand around a circular street, and nums[i] is the money inside house i. You may rob any set of houses as long as you never rob two houses that are next to each other. Because the street is a circle, the first house and the last house are neighbours.

Return the largest total you can rob. If there is only one house, you may rob it.

Constraints

  • 1 <= nums.length <= 100
  • 0 <= nums[i] <= 1000

Examples

Example 1

Input: nums = [2,3,2]
Output: 3

The 2s are neighbours in the circle, so you cannot take both. Robbing the middle house gives 3, better than a single 2.

Example 2

Input: nums = [1,2,3,1]
Output: 4

Rob houses 1 and 3 (values 1 and 3) for 4, or houses 2 and 4 (2 and 1) for 3. The first and last cannot both be used.

Example 3

Input: nums = [1,2,3]
Output: 3

Every house touches the other two in a ring of three, so only one can be robbed: take 3.

JavaScript you'll use: Arrays: create, read, update Numbers, Math and integer tricks

/**
 * @param {number[]} nums
 * @return {number}
 */
function robCircle(nums) {
  
}

Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.