Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible.
Example 1:
Input: [1,4,3,2] Output: 4 Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4).
Note:
- n is a positive integer, which is in the range of [1, 10000].
- All the integers in the array will be in the range of [-10000, 10000].
思路:在为数据分队时,尽可能的使每队的数据大小接近时,累计每队中的较小者可得出竟可能大的和。
所以,先进行排序处理,使整组数据有序,之后从第0个元素开始,每次只需要固定偏移2个位置来累计就能得到答案,就不上代码了
本文介绍了一种算法问题,即如何将2n个整数分成n对,使得每对中的最小值之和最大。通过排序并选取每对中的较小值实现这一目标。
457

被折叠的 条评论
为什么被折叠?



