题目:

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. HihoCoder - 1336 二维数状数组(单点更新 区间查询)

    You are given an N × N matrix. At the beginning every element is 0. Write a program supporting 2 ope ...

  2. CANOPEN开发问题

    我是一名研二的学生,现在教研室要开发canopen,我已经看了几个月了,在网上找了canopen的开源代码CANfestival,现在想做移植,有几个问题想要请教:1,开发主站,只买beckhoff的 ...

  3. 【git2】git+码云+webStrom

    在[git1]中介绍了Git的安装.webstrom配置Git和GitHub.GitHub项目上传下载的方法. 这篇将一下在[git1]步骤(一)基础上webstorm配置码云 实现项目的上传下载. ...

  4. MacOS(10.11.6)+Qt(5.5.1)+Xcode(8.2) C++开发环境配置

    VMware虚拟机安装MacOS(这里安装的是MacOS X 10.11.6), 百度很多, 不再详述. 安装Xcode(这里安装的是Xcode8.2) 下载(https://developer.ap ...

  5. python3通过纯真IP数据库查询IP归属地信息

    在网上看到的别人写的python2的代码,修改成了python3. 把纯真IP数据库文件qqwry.dat放到czip.py同一目录下. #! /usr/bin/env python # -*- co ...

  6. # 课下测试ch02

    课下测试ch02 1.假设下面位串是基于IEEE格式的5位浮点表示,一个符号位,2个阶码位,两个小数位.下面正确的是(AD) A . 3.5的表示是[01011] B . -1.0的表示[01111] ...

  7. WPF-学习笔记 动态修改控件Margin的值

    原文:WPF-学习笔记 动态修改控件Margin的值 举例说明:动态添加一个TextBox到Grid中,并设置它的Margin: TextBox text = new TextBox(); t_gri ...

  8. mfc 类对象指针

    类对象指针 一.类对象指针定义 Tdate d1; Tdate *p1=&d1; Tdate *p2=(Tdate *)malloc(sizeof(Tdate)); 二.类对象指针使用 int ...

  9. CF 1041 F. Ray in the tube

    F. Ray in the tube 链接 题意: 有两条平行于x轴的直线A,B,每条直线上的某些位置有传感器.你需要确定A,B轴上任意两个整点位置$x_a$,$x_b$,使得一条光线沿$x_a→x_ ...

  10. Gitlab+Jenkins学习之路(十一)之Jenkins自动触发构建和发布

    思路图: 一.下载gitlab plugin jenkins-->系统管理-->管理插件-->下载并安装gitlab plugin 二.配置gitlab认证 路径:Jenkins-- ...