dsa · easy
Array Partition
nums has even length 2n. Pair the 2n values into n pairs (a1, b1), …, (an, bn). The score of a pairing is min(a1, b1) + … + min(an, bn). Return the maximum score over all pairings.
Example
nums = [6, 2, 6, 5, 1, 2] (so n = 3).
One pairing is (6, 2), (6, 5), (1, 2) with mins 2 + 5 + 1 = 8. A better pairing is (1, 2), (2, 5), (6, 6) with mins 1 + 2 + 6 = 9. No pairing scores above 9, so the answer is 9.
nums = [1, 1] is a single pair whose min is 1.
## Arguments - nums — even-length array of 2n integers to pair
Constraints
2 <= nums.length <= 4*10^4 nums.length is even -10^4 <= nums[i] <= 10^4 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: [6,2,6,5,1,2] Expected: 9
Example 2
Input: [1,1] Expected: 1
Example 3
Input: [1,4,3,2] Expected: 4