[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, 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.
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].
思路:
先从小到大排序,然后取每一个pair的第一个值相加即可,至于为什么这么做。。还没想清楚。
int arrayPairSum(vector<int>& nums)
{
int result = ;
sort(nums.begin(),nums.end());
for(int i =;i<nums.size();i+=) result += nums[i];
return result;
}
[leetcode-561-Array Partition I]的更多相关文章
- 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 ...
- LeetCode 561. Array Partition I (C++)
题目: Given an array of 2n integers, your task is to group these integers into npairs of integer, say ...
- 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 ...
随机推荐
- java实现二叉树的构建以及3种遍历方法
转载自http://ocaicai.iteye.com/blog/1047397 大二下学期学习数据结构的时候用C介绍过二叉树,但是当时热衷于java就没有怎么鸟二叉树,但是对二叉树的构建及遍历一直耿 ...
- tomcat启动一闪而过问题的解决
1.今天使用startup.bat启动tomcat报错,现象是一闪而过,在logs文件夹中有没有日志文件, 可以在控制台输入startup.bat,如下: 但是也没有看出什么太有用的错误,然后可以 ...
- flume集群日志收集
一.Flume简介 Flume是一个分布式的.高可用的海量日志收集.聚合和传输日志收集系统,支持在日志系统中定制各类数据发送方(如:Kafka,HDFS等),便于收集数据.其核心为agent,agen ...
- 使用Java语言开发微信公众平台(七)——音乐消息的回复
在上一节课程中,我们学习了图片消息的回复功能.根据微信公众平台的消息类型显示,微信共支持文本.图片.语音.视频.音乐.图文等6种消息类型的回复: 其中,我们已经实现了文本.图文.图片等消息的回复处理, ...
- 深入理解Java虚拟机 自己编译JDK
获取JDK源码 先明确OpenJDK和Sun/OracleJDK之间,以及OpenJDK 6.OpenJDK 7.OpenJDK7u和OpenJDK 8等项目之间是什么关系,这有助于确定接下来编译要使 ...
- JAVA 编程规范
软件开发技术规范 PTHINK-DEVELOP-JAVA-091010 Java语言编程规范 2009-10-10发布 2009-10-11实施 ...
- cin 对象取值过程详解
突然又空,鉴于对cin对象的去值机制还不是很了解,就探究一番,并且记下来,方便以后复习. #include <iostream> int main(void) { using namesp ...
- linux中搭建java开发环境
JDK安装 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 下载对应jdk版本, ...
- SVN环境搭建步骤
方法:一.准备工作1. Subversion服务器程序先到官方网站上下载最新版本.2. TortoiseSVN客户端程序它是一个客户端程序,用来与Subvers服务端通讯.Subversion自带一个 ...
- Hadoop之WordCount详解
花了好长时间查找资料理解.学习.总结 这应该是一篇比较全面的MapReduce之WordCount文章了 耐心看下去 1,创建本地文件 在hadoop-2.6.0文件夹下创建一个文件夹data,在其中 ...