题目:

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. jenkins+pytest+ allure运行多个py文件测试用例

    jenkins的pytest运行多个py文件,导出allure报告方法,只需改下job的配置中的构建即可(pytest会运行指定文件下的所有test开头的py文件),如下:              ...

  2. 一个简单好用的http服务器

    http-server 是一个简单的零配置命令行HTTP服务器, 基于 nodeJs. 如果你不想重复的写 nodeJs 的 web-server.js, 则可以使用这个. 安装 (全局安装加 -g) ...

  3. java spring boot项目部署-上

    1.编写sh脚本,便于服务器上管理工程: #!/bin/bash source /etc/profile PROG_NAME=$ ACTION=$ usage() { echo "Usage ...

  4. 为Python加入默认模块搜索路径

    为Python加入默认模块搜索路径 方法一:函数加入 1) import sys 2) 查看sys.path 3) 加入sys.path.append("c:\\") 方法二:改动 ...

  5. 小白第一次使用Git随笔

    想研究Git很久了,一直没有找到很好的博客或论坛,近几天工作项目任务没有那么重,就想着找几篇文章把这玩意儿给解决掉,本博客是记录读廖雪峰老师所写的<Git教程>的随笔,以便巩固学习,若想学 ...

  6. PHPCMS v9 手机版如何设置独立域名

    一.在PHPcms V9管理后台设置手机门户(目前phpcms v9 版本为V9.6.3) 1.1.开启手机网站.位置:模块 >手机门户 > 添加手机站点,具体设置可参照截图: 填写站点名 ...

  7. string首字母大写

    定义函数将字符串首字母大写: 例1:Study hard, improve every day. def toJadenCase(string): return string.title()print ...

  8. JOOQ快速上手(基于springboot 和 postgresql)

    是什么 全称Java Object Oriented Querying,基于java开发出来的工具包,主要用于访问关系型数据库. 为什么用 Hibernate对SQL的操作太抽象 JDBC使用太过繁琐 ...

  9. Go压缩文件

    Go压缩文件 首先是恭喜IG获得S8全球总决赛冠军,IG牛逼.但咱是一介草民,狂欢后,还是得老老实实的开始敲代码.最近做了一个给底层固件压缩加密的工具,是使用C#做的,已经提交出去可以正常使用的.既然 ...

  10. Centos 6.4 安装mysql-5.6.14-linux-glibc2.5-i686.tar.gz

    创建用户和组 创建链接 授权own和grp给mysql-5.5.8-linux2.6-i686文件夹,就是下面的BASE_DIR 执行的mysql_install_db的时候后面带参数 ./scrip ...