561. Array Partition I
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:
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].
解题思路:
为得到最大的和,所以要保证第2大的数字和第1大的数字进行组合,第4大的数字和第3大的数字进行组合,以此类推。可以看出,我们将数组进行排序后,取2n+1
项进行相加,即可得结果。解答:
class Solution { public: int arrayPairSum(vector<int>& nums) { int len = nums.size(); sort(nums.begin(), nums.end()); int result = 0; for(int i = 0; i < len; ++i) { result += nums[i]; ++i; } return result; } };