[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 ...
随机推荐
- QueryBuilder 前端构造SQL条件的插件使用方法
页面引入JS等: <script type="text/javascript" src="/qysds-jx/pages/gzrw/js/jquery.js&quo ...
- 小程序之Tab切换(二)
之前写的那个Tab切换是常规逻辑写的,接下来我会列出小程序api自带的写法,当然了 这个写法更加简单,实用.我们只需要配置app.json这个文件即可. 先看效果图: app.json代码:(有木有感 ...
- [日常] AtCoder Beginner Contest 075 翻车实录
别问我为啥要写一篇ABC的游记... 周日打算CF开黑于是就打算先打打ABC找回手速... 进场秒掉 $A$ 和 $B$ , 小暴力一脸偷税 然后开 $C$ ...woc求桥? 怎么办啊我好像突然忘了 ...
- 【R语言系列】R语言初识及安装
一.R是什么 R语言是由新西兰奥克兰大学的Ross Ihaka和Robert Gentleman两个人共同发明. 其词法和语法分别源自Schema和S语言. R定义:一个能够自由幼小的用于统计计算和绘 ...
- Alpha阶段报告-hywteam
一.Alpha版本测试报告 1. 在测试过程中总共发现了多少Bug?每个类别的Bug分别为多少个? BUG名 修复的BUG 不能重现的BUG 非BUG 没能力修复的BUG 下个版本修复 文件路径的表示 ...
- mui 页面无法下滑拖拽 主要体现在华为手机浏览器
项目做到中期遇到一个问题,华为手机有些页面显示不全且无法下滑. 因为之前一直用的Google浏览器的模拟模式进行开发和调试的,一直未发现这个问题. 刚开始 选用mui的下拉刷新上拉加载的方式来进行页面 ...
- java之多态详解
前言 什么叫多态?多态就是一种事物可以有多种表现形式 多态三要素 1.被动方必须有继承关系 2.子类一般都要重写父类方法 3.必须将主动方的功能函数的参数设置为 被动方父类的类型 举个例子司机开车 假 ...
- 微信开发之SVN提交代码与FTP同步到apache的根目录
SVN是协同开发的,版本控制器,就是几个人同时开发,可以提交代码到SVN服务器,这样就可以协同开发,一般是早上上班首先更新下代码,然后自己修改代码 工作一天之后,修改代码之后,下班之前,更新代码,然后 ...
- LAMP 搭建
p { margin-bottom: 0.25cm; line-height: 120% } LAMP 搭建 承 Ubuntu 17.10.1安装, 定制. 参考 电子工业出版社, Ubuntu完美应 ...
- C#配置文件config的使用
做程序的时候总会有一些参数,可能会调整,这时候一般情况下我都会写在配置文件里,这样方便一点. 配置文件的读取 <?xml version="1.0" encoding=&qu ...