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. SpringCloud的应用发布(四)顺序启动各个应用

    一.部署应用 二.启动应用(注意顺序) 三.观察效果 1.查看进程和日志 ps -ef | grep java tail -f AppYml.txt 2.验证功能

  2. 发布到NPMJS

    最近在做微服务的前后端设计,打算将客户端中的一个模块独立出来发布到npmjs上,因此,有机会了解了一下npm的发布过程. 参考了很多网上的文章,长篇累牍(但在这里还是真心感谢他们的分享),最终总结成一 ...

  3. cmd编译运行java

    新建.java结尾的文件 内容 public class hello{ public static void main(String[] args){ System.out.println(" ...

  4. Properties文件读写问题

    项目需要在Properties配置文件中设置一些配置属性,其中包含一些中文属性.经过一上午的奋斗终于圆满解决问题. 读取Properties文件所有属性 Map<String, String&g ...

  5. 复习HTML+CSS(4)

    n  HTML颜色表示 网页中的颜色有三种表示方法 颜色单词:blue.green.red.yellow 10进制表示:rgb(255,0,0).rgb(0,255,0).rgb(0,0,255) 1 ...

  6. Windows安装SVN服务器和客户端

    我的操作系统版本是windows10 64位.接下来我会先介绍SVN服务器的安装,然后再介绍安装SVN客户端,并进行测试. 下载 首先我们需要到官网上去下载svn服务器程序. [svn官网地址] (h ...

  7. codewars.DNA题目几种解法分析(字符串替换)

    题干: 意思就是字符串替换,"A"与"C"配对,"T"与"G"配对,DNA不为空. 解法一:我的解法,用for循环遍历字 ...

  8. Swing图层的应用——实现tooltip显示

    没有错是世纪前的swing. 在使用Swing的时候有个问题一直没有解决,就是Swing自带的tooltip不会跟随鼠标进行移动,而且移动到边界就会遮挡的问题.JCompoent有个createToo ...

  9. 关于require.js的模块化开发

      先是自己打了一些demo,然后回过头来看阮大神的博客,感觉很多莫名其妙的问题,瞬间解决了:很舒服,放上链接,希望对其他人也有帮助:     先是在html的末尾引入了require.js . da ...

  10. LOB对象在数据泵导出、导入后查询对象数量发现丢失

    问题描述:问题:源库的某个Schema使用数据泵Expdp元数据整体导出,在目标库导入且成功后,逻辑验证用户对象,发现缺失.分析查询后,缺失的对象,都是LOB类型(并不是所有的LOB都无法导入,是大部 ...