Leetcode#561. Array Partition I(数组拆分 I)
题目描述
给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从1 到 n 的 min(ai, bi) 总和最大。
示例 1:
输入: [1,4,3,2]
输出: 4
解释: n 等于 2, 最大总和为 4 = min(1, 2) + min(3, 4).
提示:
- n 是正整数,范围在 [1, 10000].
- 数组中的元素范围在 [-10000, 10000].
思路
分组之后min(ai, bi)的和最大,同组两个数相差越小越好,所以需要先对数组进行排序后,再取偶数位的和即可。
代码实现
package Array;
import java.util.Arrays;
/**
* 561. Array Partition I(数组拆分 I)
* 给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从1 到 n 的 min(ai, bi) 总和最大。
*/
public class Solution561 {
public static void main(String[] args) {
Solution561 solution561 = new Solution561();
int[] nums = {1, 4, 3, 2};
System.out.println(solution561.arrayPairSum(nums));
}
public int arrayPairSum(int[] nums) {
int sum = 0;
Arrays.sort(nums);
for (int i = 0; i < nums.length; i += 2) {
sum += nums[i];
}
return sum;
}
}
Leetcode#561. Array Partition I(数组拆分 I)的更多相关文章
- 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 (C++)
题目: Given an array of 2n integers, your task is to group these integers into npairs of integer, say ...
- [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 ...
- Leetcode561.Array Partition I数组拆分1
给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从1 到 n 的 min(ai, bi) 总和最大. 示例 ...
- 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】数组-6(561)-Array Partition I(比较抽象的题目)
题目描述:两句话发人深思啊.... Given an array of 2n integers, your task is to group these integers into n pairs o ...
随机推荐
- WPF界面+halcon生成的C#文件
1.新建WPF应用程序完成后,解决方案资源管理器——引用处右键,添加引用如下两个dll 工具箱,空白处右键,选择项(只添加halcondotnet.dll,位置同上).这样halcon平台的控件就添加 ...
- (等比数列)P1423 小玉在游泳 洛谷
题目描述 小玉开心的在游泳,可是她很快难过的发现,自己的力气不够,游泳好累哦.已知小玉第一步能游2米,可是随着越来越累,力气越来越小,她接下来的每一步都只能游出上一步距离的98%.现在小玉想知道,如果 ...
- Go-day03
概要: 1.strings与strconv的使用 2.Go中的时间和日期类型 3.流程控制 4.函数详解 strings与strconv用法 1.strings.HasPrefix(s string, ...
- python自动化开发-[第二十二天]-bbs多级评论、点赞、上传文件
今日概要: 1.related_name和related_query_name的区别 2.through_fields的用途 3.django的事务提交 4.点赞的动画效果 5.多级评论的原理 6.上 ...
- mysql 安装部署
如下方法 安装完成后 没有my.ini 配置文件,请结合下篇(mysql安装绑定my.ini)文章一起看. 安装包的安装方式:参考:https://www.cnblogs.com/ayyl/p/597 ...
- python mysql索引 优化神器explain 慢查询
##############总结########## 数据库中专门帮助用户快速找到数据的一种数据结构 类似于字典的目录的索引 索引的作用:约束和加速查找 工作原理: b+树形结构 最上层是树根,中间是 ...
- Git(使用码云)
使用GitHub时,国内的用户经常遇到的问题是访问速度太慢,有时候还会出现无法连接的情况(原因你懂的). 如果我们希望体验Git飞一般的速度,可以使用国内的Git托管服务——码云(gitee.com) ...
- ubuntu linux下建立stm32开发环境: 程序烧录 openocd+openjtag
原文出处: http://blog.csdn.net/embbnux/article/details/17619621 之前建立stm32开发环境,程序也已经编译好生成main.bin,接下来就是要把 ...
- HDU 1041(01展开 大数)
题意是将 1 展开成 01 ,将 0 展开成 10 ,问这样展开 n 次后序列中有多少对 0. 手写发现:0,1,1,3,5,11 ... 即 a[ i ] = a[ i -1 ] + a[ i - ...
- Oracle了解(一)
通常所说的Oracle数据库服务器由一个数据库和至少一个数据库实例组成. 数据库实例是由系统后台进程和分配的内存区域构成 实例你是提供服务的进程,数据库是存放的数据. 数据库是存储数据的文件 数据库实 ...