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:

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

思路:将整个数组进行排序,然后每个数对的最小值就是左边的值,在数组中的显示就是隔一个数字,因此,排序后,以2为步进求和。

代码:

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

[Array] 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 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  5. js Array.from & Array.of All In One

    js Array.from & Array.of All In One 数组生成器 Array.from The Array.from() static method creates a ne ...

  6. Array.fill & array padding

    Array.fill & array padding arr.fill(value[, start[, end]]) https://developer.mozilla.org/en-US/d ...

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

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

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

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

随机推荐

  1. cocos2dx触摸响应

      Layer其实继承了触控的接口. 所以只需要重写一些函数即可.   在helloword类中重写:     virtual bool init();     /** Callback functi ...

  2. LVS/Nginx/HAProxy负载均衡器的对比分析

    转自:http://www.blogjava.net/ivanwan/archive/2013/12/25/408014.html LVS的特点是: 抗负载能力强.是工作在网络4层之上仅作分发之用,没 ...

  3. 输出内容 document.write() 可用于直接向 HTML 输出流写内容。简单的说就是直接在网页中输出内容

    输出内容(document.write) document.write() 可用于直接向 HTML 输出流写内容.简单的说就是直接在网页中输出内容. 第一种:输出内容用""括起,直 ...

  4. 算法系列:Shell 排序

    Copyright © 1900-2016, NORYES, All Rights Reserved. http://www.cnblogs.com/noryes/ 欢迎转载,请保留此版权声明. -- ...

  5. Loadrunner安装与破解【转】

    Loadrunner安装与破解 一.下载 我的LoadRunner 11下载地址是: http://pan.baidu.com/s/1qYFy2DI 二.安装 1.启动安装程序 运行setup.exe ...

  6. Data Dependency

    https://en.wikipedia.org/wiki/Data_dependency (There’s some misleading expression on the flow/data d ...

  7. pytorch基础(1)

    基本数据类型和tensor import torch import numpy as np #array 和 tensor的转换 array = np.array([,]) tensorArray = ...

  8. 国内有哪些质量高的JAVA社区?

    国内有哪些质量高的JAVA社区? 转自:http://www.zhihu.com/question/29836842#answer-13737722 并发编程网 - ifeve.com 强烈推荐 Im ...

  9. Everything-启用http服务器(公网IP)会导致共享文件被搜索引擎搜索

    1. 漏洞利用成功的前提 公网ip 启用http服务器 2.产生原因 启用http服务器功能点:让用户在本地或局域网上的其他电脑使用浏览器进行搜索,并支持文件下载.若拥有公网IP的用户启用此功能,就是 ...

  10. sqlmap:wins系统+python3上安装

    python2和python3互不兼容,SqlMap是基于python2的,所以SqlMap不支持python3,这里使用virtualenvwrapper切换python版本: 一.sqlmap的安 ...