LeetCode 561. Array Partition I (C++)
题目:
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:
- n is a positive integer, which is in the range of [1, 10000].
- 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++)的更多相关文章
- Leetcode#561. Array Partition I(数组拆分 I)
题目描述 给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从1 到 n 的 min(ai, bi) 总和最 ...
- 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 ...
- 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 ...
- LeetCode 561 Array Partition I 解题报告
题目要求 Given an array of 2n integers, your task is to group these integers into n pairs of integer, sa ...
- [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 ...
- 561. Array Partition I - LeetCode
Question 561. Array Partition I Solution 题目大意是,给的数组大小是2n,把数组分成n组,每组2个元素,每个组取最小值,这样就能得到n个值,怎样分组才能使这n个 ...
- 561. Array Partition I【easy】
561. Array Partition I[easy] Given an array of 2n integers, your task is to group these integers int ...
- 【LeetCode】561. Array Partition I 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...
- 【LeetCode】数组-6(561)-Array Partition I(比较抽象的题目)
题目描述:两句话发人深思啊.... Given an array of 2n integers, your task is to group these integers into n pairs o ...
随机推荐
- ddt 数据处理调用excel数据建模
1.数据模型: 2.数据处理 最终返回数据:[(),()] 格式 ddt调用: import ddtimport unittest @ddt.ddtclass Test(unittest.TestCa ...
- python logging模块按天滚动简单程序
简单日志按天滚动,加入apsheduler,用crontab模式按小时运行测试: import logging from logging.handlers import TimedRotatingFi ...
- 【FileZilla FTP Client】文件与服务器操作客户端
跨平台的FTP,FTPS和SFTP客户端 可以断点续传进行上传.下载(需要服务器支持). 自定义命令. 可进行站点管理.
- head 标签里有什么?
head 标签里有什么? 每一个 HTML 文档中,都有一个不可或缺的标签:<head> ,它作为一个容器,主要包含了用于描述 HTML 文档自身信息(元数据)的标签,这些标签一般不会在页 ...
- python 3.x 实现简单用户登录
import os import sys import getpass login_username = 'admin' login_password = ' u = 0 while u < 3 ...
- 6.Exceptions-异常(Dart中文文档)
异常是用于标识程序发生未知异常.如果异常没有被捕获,If the exception isn't caught, the isolate that raised the exception is su ...
- JQuery第三天——CSS操作与JQuery事件
JQuery的CSS_DOM与样式操作 样式: 获取 class 和设置 class : class 是元素的一个属性, 所以获取 class 和设置 class 都可以使用 attr() 方法来完成 ...
- Oracle单节点_Grid_Infrastructure_DB_安装过程图解(二/三)
接上文 Oracle单节点_Grid_Infrastructure_DB_安装过程图解(一/三) 接下来,进行Grid Infrastructure 部分的安装:
- Noip前的大抱佛脚----赛前任务
赛前任务 tags:任务清单 前言 现在xzy太弱了,而且他最近越来越弱了,天天被爆踩,天天被爆踩 题单不会在作业部落发布,所以可(yi)能(ding)会不及时更新 省选前的练习莫名其妙地成为了Noi ...
- Spring学习(十七)----- Spring自动代理创建者
1. BeanNameAutoProxyCreator示例 在此之前,必须手动创建一个代理bean(ProxyFactryBean). <beans xmlns="http://www ...