193. House Robber II
There are nums.length houses arranged in a circle, and nums[i] is the amount of money stored in house i. Because the street loops around, the first and the last house are direct neighbors. You may collect from any set of houses as long as no two chosen houses are adjacent on the circle.
Return the maximum total amount you can collect.
This is the circular twist on the classic straight-line version — think about what choosing the first house implies for the last one.
Example 1:
Input: nums = [3, 1, 5, 2]
Output: 8
Explanation: Take houses 0 and 2 (3 + 5 = 8). They are not neighbors on the circle, and no other legal selection does better.
Example 2:
Input: nums = [4, 2, 3]
Output: 4
Explanation: On a circle of three houses, every pair of houses is adjacent, so at most one house can be robbed — the richest one holds 4.
Constraints:
- 1 ≤ nums.length ≤ 100
- 0 ≤ nums[i] ≤ 1000
Hints:
If you rob house 0, the last house is automatically off-limits — and if you skip house 0, the last house is free to take. That one case split removes the circle entirely.
Solve the straight-line robber problem twice: once on nums without its last element, once on nums without its first, and return the better of the two. A single house is the only special case.
The linear version needs just two rolling values: the best total ending at the previous house and the best total ending two houses back.
▶ Run checks these sample cases. Submit also runs hidden edge cases.
Input: nums = [3, 1, 5, 2]
Expected output: 8