Loading...
You are given an integer array nums.
Partition the entire array into contiguous groups such that every group contains at least 3 elements. The score of a group is its third-smallest value. For the group [8, 2, 5, 1, 10] the sorted order is [1, 2, 5, 8, 10], so its score is 5. Equal values count separately: the third-smallest of [4, 4, 4] is 4.
Return the maximum total score over all valid partitions. If the array cannot be partitioned into valid groups (fewer than 3 elements), return -1.
Input: nums = [8, 2, 5, 1, 10]
Output: 5
Explanation: With 5 elements the only valid partition is the whole array as one group, whose third-smallest value is 5.
Input: nums = [1, 2, 3, 4, 5, 6]
Output: 9
Explanation: [1, 2, 3] scores 3 and [4, 5, 6] scores 6, total 9. Keeping everything in one group would score only 3.
Input: nums = [5, 5]
Output: -1
Explanation: Two elements cannot form a group of at least 3.
nums.length ≤105nums[i] ≤109Click "Run" to test with sample cases or "Submit" to run all tests.