题目:

Given an array of 2n integers, your task is to group these integers into npairs 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].

分析1:

给定一个大小为2n数组,其内元素两两一组,组内求min,总体求max值。

很明显,我们希望每一组内的两个元素尽可能的相近,这样在求min之后的和一定是最大的。所以可以进行排序,然后直接对0,2,4,...加和处理。

程序1:

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

分析2:

看到一种O(n)的解法。使用桶排序,因为note中给定了元素的范围是[-10000,10000],我们可以开辟一个20001大小的数组,将元素的值和数组的索引联系起来,-10000存在n[0]中,10000存在n[20000]中,在按照索引大小对数组进行遍历,通过设定flag的值来交替将数组中的值加到sum中,以起到求两数较小值的功能。不过这种方法牺牲了空间,属于用空间来换时间的做法。

程序2:

class Solution {
public:
int arrayPairSum(vector<int>& nums) {
vector<int> n(, );
for(int i:nums)
n[i+]++;
int flag = ;
int sum = ;
for(int i = ; i < ;){
if((n[i] > )&& flag == ){
sum += (i-);
n[i]--;
flag = ;
}
else if((n[i] > ) && flag == ){
n[i]--;
flag = ;
}
else
i++;
}
return sum;
}
};

LeetCode 561. Array Partition I (C++)的更多相关文章

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

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

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

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

  4. LeetCode 561 Array Partition I 解题报告

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

  5. [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 ...

  6. 561. Array Partition I - LeetCode

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

  7. 561. Array Partition I【easy】

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

  8. 【LeetCode】561. Array Partition I 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...

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

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

随机推荐

  1. HDU 2298(纯物理加解一元二次方程)

    Toxophily Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  2. BZOJ2194: 快速傅立叶之二(NTT,卷积)

    Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1776  Solved: 1055[Submit][Status][Discuss] Descript ...

  3. 打开 CRM 时,出现错误:"Invalid Action – The selected action was not valid"

    今天当所有用户在打开CRM时,都出现了一个错误提示 “Invalid Action – The selected action was not valid”. 打开服务器的 event viewer查 ...

  4. C语言学习记录_2019.02.05

    switch只能判断整数,而分段函数的判别是一个范围,我们无法用整数来表示范围 跟踪语句的方法: (1)debug调试 (2)printf( )语句跟踪 小套路:当循环次数很大时,可以先模拟较小次数的 ...

  5. 9 README,全套代码

    BBS+ BLOG系统(仿博客园) 一.概要 欢迎您使用该BBS+BLOG系统,希望在您使用的过程中体验到便捷和愉快的使用感受,并对我们的软件提出您发现的问题和建议,谢谢. 联系邮箱:liangshu ...

  6. 2460: [BeiJing2011]元素

    2460: [BeiJing2011]元素 链接 分析: 贪心的想:首先按权值排序,然后从大到小依次放,能放则放.然后用线性基维护是否合法. 代码: #include<cstdio> #i ...

  7. linux中mycat的配置,分片,以及主从复制

    1.1    安装环境 1.jdk:要求jdk必须是1.7及以上版本 2.Mysql:推荐mysql是5.5以上版本 1.2  安装步骤 Mycat有windows.linux多种版本.本教程为lin ...

  8. GPUImage每个类的作用

    28 #import "GPUImageBrightnessFilter.h"                //亮度 29 #import "GPUImageExpos ...

  9. HTTPS为什么又快又安全?

    一.基础:对称加密和非对称加密 对称加密 通信两端用一样的密钥加解密.如DES.AES. 优点:性能损耗低,速度快: 缺点:密钥存在泄露的可能. 非对称加密 通信两端各自持有对方的公钥及自己的私钥,通 ...

  10. Oracle安装到Maven本地仓库

    1.由于Maven的特性,并且之前的IDE环境已帮我们集成了Maven.而现在我们需要手动安装MVN本地仓库到电脑. 将mvn绿色安装包bin路径配置到系统环境变量Path中 验证命令: mvn –v ...