House Robber II

Implement houseRobberII

This is the same street of houses as before, except now the street bends into a circle: the very first house and the very last house sit right next to each other, so robbing both on the same night still trips the alarm. Given each house's cash, find the maximum total that can be robbed without ever robbing two adjacent houses — including that one wraparound pair. The circular constraint only ever rules out one thing: robbing both the first and last house together. So instead of solving the whole circle at once, break it into two ordinary straight-line streets — one that stops just before the last house, and one that starts just after the first house — and solve each with the regular House Robber approach. Since any valid circular plan must leave out the first house, the last house, or both, one of these two straight-line streets is guaranteed to already contain the best possible plan, and the larger of the two results is the final answer.

Example 1:

Input: nums = [5,4,7,3]

Output: 12

Example 2:

Input: nums = [8,1,1,8]

Output: 9

Example 3:

Input: nums = [6,3,9,5,2,7]

Output: 17

+ 7 hidden test cases run on Submit.

Constraints:

  • 1 ≤ nums.length ≤ 100
  • 0 ≤ nums[i] ≤ 400
  • The houses are arranged in a circle: the first and last houses are adjacent to each other.

nums =

[5, 4, 7, 3]