561. Array Partition I

原创 2018年04月17日 10:22:26

原题链接

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].

思路:排序求和奇数位,这样每位的损失最小。

AC代码:

class Solution {
public:
    int arrayPairSum(vector<int>& nums) {
        sort(nums.begin(),nums.end());
        int sum = 0;
        for(int i=0;i<nums.size();i+=2)
        {
            sum+=nums[i];
        }
        return sum;
    }
};
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/rockywallet/article/details/79970940

【讲师卡】Array老师:高新启蒙讲师卡的介绍

讲师介绍 【讲师卡介绍】 权益: 年卡的高性价比,时效期内均免费听课和提问。第一时间提供解疑服务,第一时间提供更新服 【服务承诺】 承诺: 第一时间优先提供解疑服务;第一时间优先提供更新服务;第一时间优先提供优惠服务
  • 2017年10月09日 17:34

561. Array Partition I的C++解法

尽量把小的数字分到一组就可以了。用快排,然后把0,2,4,6...个数相加。一开始写了个插排超时了。 class Solution { public: void quickSort(vector& ...
  • musechipin
  • musechipin
  • 2017-04-25 16:37:53
  • 461

LeetCode 561 Array Partition I(数组划分)

翻译原文Given an array of 2n integers, your task is to group these integers into n pairs of integer, say...
  • NoMasp
  • NoMasp
  • 2017-05-05 21:07:49
  • 2868

[leetcode: Python]561. Array Partition I

Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1...
  • NXHYD
  • NXHYD
  • 2017-05-19 16:20:44
  • 1277

python--leetcode561. Array Partition I

Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1...
  • wenqiwenqi123
  • wenqiwenqi123
  • 2017-09-22 17:12:29
  • 467

561. Array Partition I(C语言)

这道题什么鬼 不应该输出5,[2,3],一组,[1,4]一组么 Given an array of 2n integers, your task is to group these integers ...
  • yanqueen2011
  • yanqueen2011
  • 2017-05-10 11:03:09
  • 651

[LeetCode]561. Array Partition I (数组分区 1)

561. Array Partition IGiven an array of 2n integers, your task is to group these integers into n pai...
  • whl_program
  • whl_program
  • 2017-04-25 00:21:46
  • 4318

561. Array Partition I(Java)

Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1...
  • Crystal_ting
  • Crystal_ting
  • 2017-08-11 16:57:34
  • 181

LeetCode 561. Array Partition I

题目: Given an array of 2n integers, your task is to group these integers into n pairs of integer, sa...
  • sinat_36053757
  • sinat_36053757
  • 2017-04-24 09:36:20
  • 1918

leetcode561: Array Partition I

要求: Given an array of 2n integers, your task is to group these integers into n pairs of integer, ...
  • sinat_35425429
  • sinat_35425429
  • 2017-07-03 20:12:34
  • 76
收藏助手
不良信息举报
您举报文章:561. Array Partition I
举报原因:
原因补充:

(最多只允许输入30个字)