Loading...
You are given an integer array nums arranged in a circle, so the first and the last elements are adjacent to each other, in addition to every pair of consecutive elements.
Choose a subset of the elements so that no two chosen elements are adjacent. Return the maximum possible sum of the chosen elements.
The subset may be empty, and the sum of an empty subset is 0. Since every value satisfies nums[i] ≥0, the answer is never negative. When nums has only one element, there is no separate first and last element, so that element can always be chosen.
Input: nums = [4,1,1,4]
Output: 5
Explanation: The first and last elements (values 4 and 4) are adjacent because the array is a circle, so they cannot both be chosen. Choosing indices 0 and 2 (values 4 and 1) gives a sum of 5.
Input: nums = [2,7,9,3,1]
Output: 11
Explanation: Choosing indices 0 and 2 (values 2 and 9) gives a sum of 11. Indices 0 and 4 cannot both be chosen, because they are the first and last elements of the circle.
nums.length ≤105nums[i] ≤400Click "Run" to test with sample cases or "Submit" to run all tests.