[LeetCode] 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].
这道题让我们分割数组,两两一对,让每对中较小的数的和最大。这题难度不大,用贪婪算法就可以了。由于我们要最大化每对中的较小值之和,那么肯定是每对中两个数字大小越接近越好,因为如果差距过大,而我们只取较小的数字,那么大数字就浪费掉了。明白了这一点,我们只需要给数组排个序,然后按顺序的每两个就是一对,我们取出每对中的第一个数即为较小值累加起来即可,参见代码如下:
class Solution {
public:
int arrayPairSum(vector<int>& nums) {
int res = , n = nums.size();
sort(nums.begin(), nums.end());
for (int i = ; i < n; i += ) {
res += nums[i];
}
return res;
}
};
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Array Partition I 数组分割之一的更多相关文章
- Leetcode561.Array Partition I数组拆分1
给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从1 到 n 的 min(ai, bi) 总和最大. 示例 ...
- [LeetCode] Split Array With Same Average 分割数组成相同平均值的小数组
In a given integer array A, we must move every element of A to either list B or list C. (B and C ini ...
- [LeetCode] Split Array into Consecutive Subsequences 将数组分割成连续子序列
You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...
- [LeetCode] 659. Split Array into Consecutive Subsequences 将数组分割成连续子序列
You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...
- Leetcode#561. Array Partition I(数组拆分 I)
题目描述 给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从1 到 n 的 min(ai, bi) 总和最 ...
- LeetCode初级算法--设计问题01:Shuffle an Array (打乱数组)
LeetCode初级算法--设计问题01:Shuffle an Array (打乱数组) 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:h ...
- 【LEETCODE】39、第561题 Array Partition I
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 【leetcode】561. Array Partition I
原题: Given an array of 2n integers, your task is to group these integers into n pairs of integer, say ...
- LeetCode 数组分割
LeetCode 数组分割 LeetCode 数组怎么分割可以得到左右最大值的差值的最大 https://www.nowcoder.com/study/live/489/1/1 左右最值最大差 htt ...
随机推荐
- 访问限制:由于对必需的库 C:/Program Files/Java/jre6/lib/rt.jar 具有一定限制,因此无法访问类型。。
在项目上单击右键选择 属性 Java编译器 错误或警告 选择启用特定于项目的设置 建议不要使用和限制使用的API将 禁止的引用(访问规则) 设置为 警告 然后应用即可解决
- Android权限Uri.parse的几种用法(转载)
1,调web浏览器 Uri myBlogUri = Uri.parse("http://xxxxx.com"); returnIt = new Intent(Intent.ACTI ...
- QQ数据库管理
1,数据库关系图 ##用例1:查询数据 #01.查询QQ号码为54789625的所有好友信息,包括QQ号码,昵称,年龄 select RelationQQID as QQ号码,NickName as ...
- [日常] Codeforces Round #440 Div.2 大力翻车实况
上次打了一发ABC然后大力翻车...上午考试又停电+Unrated令人非常滑稽...下午终于到了CF比赛... 赛前大力安利了一发然后拉了老白/ $ljm$ / $wcx$ 一起打, 然后搞了个 TI ...
- JavaScript 哈希表(散列表)应用
查找的效率与比较次数密切相关.基于比较的程序,运算效率是比较低的.比如平时可以通过indexOf查找一个数据.但这是一个基于比较的一个实现.如果是淘宝那样有上亿个商品,那么用indeOf 来查数据就会 ...
- 『BUG』Android Studio 64位 始终提示 JVM 启动不了,JDK配置失败,error code -1
前几天 安装了 Android Studio 2.2. 本来一切都好,但是当我修改了 配置文件 studio64.exe.vmoptions 想修改 最大内存(保证运行流畅)后,AS 就再也运行不了了 ...
- Leetcode 2——Range Sum Query - Mutable(树状数组实现)
Problem: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), ...
- 06_Python的数据类型3元组,集合和字典_Python编程之路
上一节跟大家讲了Python的列表,当然不是完整的讲完,后续我们还会提到,这一节我们还是来讲Python的数据类型 首先要讲到的就是元组 元组其实拥有列表的一些特性,可以存储不同类型的值,但在某些方面 ...
- vmware 12 安装 mac os 10.12正式版
1.首先下载安装vmware 12 pro ,将VT打开(虚拟功能,以前安装过虚拟机点的同学可忽略). 2.下载mac ox 10.12正式版镜像文件(cdr后缀). 3.下载Unlocker208( ...
- ELK学习总结(2-6)elk的mapping
1.什么是映射 映射:创建索引的时候,预先定义字段的类型及相关属性 作用:这样会让索引建立的更加细致和完善,如:是否存储.使用何种分析器.重要级别 分类:静态映射和动态映射 2.字段类型:string ...