Given an array of 2n integers, your task is to group these integers into n pairs of integer, say \((a_1, b_1), (a_2, b_2), ..., (a_n, b_n)\) which makes sum of \(min(a_i, b_i)\) 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:

  1. n is a positive integer, which is in the range of [1, 10000].
  2. All the integers in the array will be in the range of [-10000, 10000].

自家代码:

先排序, 索引为偶数元素求和.

副产品为: vector的排序方式 sort(A.begin(), A.end()).

\(O(nlogn)\) time, \(O(1)\) extra space.

int arrayPairSum(vector<int>& A) {
sort(A.begin(), A.end());
int sum = 0;
for (int i = 0; i < A.size(); i += 2) {
sum += A[i];
}
return sum;
}

561. Array Partition I的更多相关文章

  1. Leetcode#561. Array Partition I(数组拆分 I)

    题目描述 给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从1 到 n 的 min(ai, bi) 总和最 ...

  2. 561. Array Partition I【easy】

    561. Array Partition I[easy] Given an array of 2n integers, your task is to group these integers int ...

  3. 561. Array Partition I - LeetCode

    Question 561. Array Partition I Solution 题目大意是,给的数组大小是2n,把数组分成n组,每组2个元素,每个组取最小值,这样就能得到n个值,怎样分组才能使这n个 ...

  4. 【LeetCode】数组-6(561)-Array Partition I(比较抽象的题目)

    题目描述:两句话发人深思啊.... Given an array of 2n integers, your task is to group these integers into n pairs o ...

  5. LeetCode 561. Array Partition I (数组分隔之一)

    Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...

  6. leetcode 561.Array Partition I-easy

    Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...

  7. 【easy】561. Array Partition I

    Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...

  8. LeetCode 561 Array Partition I 解题报告

    题目要求 Given an array of 2n integers, your task is to group these integers into n pairs of integer, sa ...

  9. [LeetCode] 561. Array Partition I_Easy tag: Sort

    Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...

随机推荐

  1. 十个你需要在 PHP 7 中避免的坑

    1. 不要使用 mysql_ 类函数 终于,你不用再看到建议不要使用 mysql_ 函数的提示了.因为 PHP 7 从核心上完全移除了它们,这意味着请你移步至更好的 mysqli_ 类函数,或者更灵活 ...

  2. leetcode算法:Next Greater Element I

    You are given two arrays (without duplicates) nums1 and nums2 where nums1's elements are subset of n ...

  3. JavaScript中的 原型 property 构造函数 和实例对象之间的关系

    1 为什么要使用原型? /* * javascript当中 原型 prototype 对象 * * */ //首先引入 prototype的意义,为什么要使用这个对象 //先来写一个构造函数的面向对象 ...

  4. wpf的tab移动焦点只能在容器内部使用

    设置 KeyboardNavigation.TabNavigation="Cycle" 即可

  5. Python 破解带密码保护的Zip文件

    今天发生了个有趣的事情,有个朋友发了一个带密码保护的Zip文件给我,却不给我密码,我就琢磨这怎么可以'猜'到密码呢? 经过一系列的尝试,最终使用python把这个密码给'猜'出来了.要想写出破解密码的 ...

  6. Unity3D input.GetAxis

    input.GetAxis用法:(GetAxis("Mouse X"),GetAxis("Mouse Y"),GetAxis("Mouse Scrol ...

  7. Python 装饰器示例

    #!/usr/bin/env python # _*_ coding: UTF-8 _*_ # Author:taoke def applePrice(func): def otherfunc(): ...

  8. [NOI 2009]变换序列

    Description 题库链接 对于 \(N\) 个整数 \(0, 1, \cdots, N-1\) ,一个变换序列 \(T\) 可以将 \(i\) 变成 \(T_i\) ,其中 \(T_i \in ...

  9. [测试题]数组(array)

    Description Input Output Sample Input1 3 2 75 4 2 Sample Output1 999999732 Sample Explanation1 Sampl ...

  10. POJ1743 Musical Theme(二分+后缀数组)

    题目大概是给n个数组成的串,求是否有多个“相似”且不重叠的子串的长度大于等于5,两个子串相似当且仅当长度相等且每一位的数字差都相等. 这题是传说中楼教主男人八题之一,虽然已经是用后缀数组解决不可重叠最 ...