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.
Examples
Example 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.
Example 2
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.
Example 3
Input: nums = [5, 5]
Output: -1
Explanation: Two elements cannot form a group of at least 3.
Constraints
1≤nums.length≤105
1≤nums[i]≤109
Loading editor...
Log in to run your code. Your draft is saved in this browser.Log inSign up