House Robber II

IF
AlgoAxiomStaff Engineers
JSTS
Medium20 mins

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle, meaning the first house is the neighbor of the last one.

Given an integer array nums representing the amount of money at each house, return the maximum amount of money you can rob tonight without robbing two adjacent houses.

Examples

Example 1:

Input: nums = [2,3,2]

Output: 3

Explanation: You cannot rob house 0 (money = 2) and house 2 (money = 2), because they are adjacent in the circle. The best is to rob house 1 (money = 3).

Example 2:

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

Output: 4

Explanation: Rob house 0 (money = 1) and house 2 (money = 3). Total = 1 + 3 = 4.

Example 3:

Input: nums = [1,2,3]

Output: 3

Explanation: Rob house 2 (money = 3). You cannot rob house 0 and house 2 together since they are adjacent in the circle.

Constraints

  • 1 <= nums.length <= 100
  • 0 <= nums[i] <= 1000
Source: Dynamic Programming pattern — AlgoAxiom
JavaScript
Test Case 1
root = [1, 2, 3]
Test Case 2
root = [1, 2, 3, 4, 5]
Idle