[Array] 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 = 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].
思路:将整个数组进行排序,然后每个数对的最小值就是左边的值,在数组中的显示就是隔一个数字,因此,排序后,以2为步进求和。
代码:
int arraypairsum(vector<int>& nums)
{
sort(nums.begin(), nums.end());
int sum = ;
for(int i = ; i < nums.size(); i = i + )
sum = sum + nums[i];
return sum;
}
[Array] 561. Array Partition I的更多相关文章
- Leetcode#561. Array Partition I(数组拆分 I)
题目描述 给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从1 到 n 的 min(ai, bi) 总和最 ...
- 561. Array Partition I【easy】
561. Array Partition I[easy] Given an array of 2n integers, your task is to group these integers int ...
- 561. Array Partition I - LeetCode
Question 561. Array Partition I Solution 题目大意是,给的数组大小是2n,把数组分成n组,每组2个元素,每个组取最小值,这样就能得到n个值,怎样分组才能使这n个 ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- js Array.from & Array.of All In One
js Array.from & Array.of All In One 数组生成器 Array.from The Array.from() static method creates a ne ...
- Array.fill & array padding
Array.fill & array padding arr.fill(value[, start[, end]]) https://developer.mozilla.org/en-US/d ...
- 【LeetCode】数组-6(561)-Array Partition I(比较抽象的题目)
题目描述:两句话发人深思啊.... Given an array of 2n integers, your task is to group these integers into n pairs o ...
- 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 ...
随机推荐
- java_JDK8中新增的时间API
java.time 包含值对象的基础包 java.time.chrono 提供对不同的日历系统的访问 java.time.format 格式化和解析时间的日期 java.time.temporal 包 ...
- 【学术篇】luogu2184贪婪大陆
题目在这里哦, 戳一下就可以了~ 题目大意: 支持两种操作,区间添加一种新元素,查询区间颜色种数.. 题目标签是线段树啊,我也本来想写一个线段树,后来写不出来……(我太弱了orz) 然后就草率地看了看 ...
- C++: inheritance
公有继承(public).私有继承(private).保护继承(protected)是常用的三种继承方式. 1. 公有继承(public) 公有继承的特点是基类的公有成员和保护成员作为派生类的成员时, ...
- [转]使用TortoiseGit处理代码冲突
场景一 user0 有新提交 user1 没有pull -> 写新代码 -> pull -> 提示有冲突 解决办法一 -> stash save(把自己的代码隐藏存起来) ...
- 廖雪峰Java13网络编程-1Socket编程-3TCP多线程编程
TCP多线程编程 一个ServerSocket可以和多个客户端同时建立连接,所以一个Server可以同时与多个客户端建立好的Socket进行双向通信. 因此服务器端,当我们打开一个Socket以后,通 ...
- 菜鸟nginx源码剖析数据结构篇(八) 缓冲区链表ngx_chain_t[转]
菜鸟nginx源码剖析数据结构篇(八) 缓冲区链表 ngx_chain_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.c ...
- Error:Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
ylbtech-Error:Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerF ...
- SPSS分析技术:多元方差分析
SPSS分析技术:多元方差分析 下面要介绍多元方差分析的内容,多元方差分析是研究多个自变量与多个因变量相互关系的一种统计理论方法,又称多变量分析.多元方差分析实质上是单因变量方差分析(包括单因素和多因 ...
- 数据交互 axios 的使用
Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中. ##Axios npm version build status code coverage npm ...
- MyBatis与JPA的区别
参考博客: https://www.cnblogs.com/llywy/p/10103136.html https://www.jianshu.com/p/32ce87c163d6 MyBatis分为 ...