Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
48 lines (46 sloc) 913 Bytes
/*
* @lc app=leetcode.cn id=561 lang=cpp
*
* [561] 数组拆分 I
*
* https://leetcode-cn.com/problems/array-partition-i/description/
*
* algorithms
* Easy (63.12%)
* Total Accepted: 11.3K
* Total Submissions: 17.6K
* Testcase Example: '[1,4,3,2]'
*
* 给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从1 到
* n 的 min(ai, bi) 总和最大。
*
* 示例 1:
*
*
* 输入: [1,4,3,2]
*
* 输出: 4
* 解释: n 等于 2, 最大总和为 4 = min(1, 2) + min(3, 4).
*
*
* 提示:
*
*
* n 是正整数,范围在 [1, 10000].
* 数组中的元素范围在 [-10000, 10000].
*
*
*/
//排序。贪心。
class Solution {
public:
int arrayPairSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
int ret = 0;
for (int i=0;i<nums.size();i+=2)
{
ret += nums[i];
}
return ret;
}
};
You can’t perform that action at this time.