How to realize Array splitting in leetcode
In this issue, the editor will bring you about how to split arrays in leetcode. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
I. the content of the topic
Given an integer array nums of length 2n, your task is to divide these numbers into n pairs, such as (A1, b1), (a2, b2),..., (an, bn), so that the sum of min (ai, bi) from 1 to n is maximum.
Returns the maximum sum.
Example 1:
Input: nums = [1, 4, 3, 2]
Output: 4
Explanation: all possible divisions (ignoring the order of elements) are:
1. (1,4), (2,3)-> min (1,4) + min (2,3) = 1 + 2 = 3
2. (1,3), (2,4)-> min (1,3) + min (2,4) = 1 + 2 = 3
3. (1,2), (3,4)-> min (1,2) + min (3,4) = 1 + 3 = 4
So the maximum sum is 4.
Example 2:
Input: nums = [6, 2, 6, 5, 1, 2]
Output: 9
Explanation: the optimal method is (2, 1), (2, 5), (6, 6). Min (2,1) + min (2,5) + min (6,6) = 1 + 2 + 6 = 9
Tip:
one