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 ...
随机推荐
- java锁——wait,notify,synchronized
背景:这篇博客用来总结java锁相关的知识点,平时还是要自己多加练习 wait 和 notify以及notifyAll (1).方法介绍1.wait.notify以及notifyAll都是Object ...
- 跟我一起用node-express搭建一个小项目(mongodb)[二]
我的小项目主要是会用到MongoDB. 呵呵,我也是现学现卖. 都说小公司十八般武艺样样稀疏,没有办法啊! 兵来兵挡,将来将挡!自己是个兵呢?还是一个将呢! 没有公司培养,就自己培养自己呗.差的远一点 ...
- IntelliJ IDEA Cannot resolve symbol ''
study from : https://www.cnblogs.com/linmengfei/p/7909196.html File->Invalidate Caches 点击File | I ...
- JSP页面用<a>标签访问 Action 出错
问题: JSP页面 <a href="/crud1/crud1/add.action" >添加</a> struts.xml 中: <package ...
- 使用pip cmd安装包
pip install matplotlib -i http://pypi.douban.com/simple --trusted-host pypi.douban.com,通过命令行安装的时候,指定 ...
- 【内核】Linux内核Initrd机制解析,内核更新步骤,grub配置说明
什么是Initrd initrd的英文含义是 boot loader initialized RAM disk,就是由boot loader初始化的内存盘.在 linux内核启动前, boot loa ...
- as报错
android studio报错报错一:Could not HEAD 'https://dl.google.com/dl/android/maven2/com/android/tools/build/ ...
- Tensorflow object detection API 搭建物体识别模型(一)
一.开发环境 1)python3.5 2)tensorflow1.12.0 3)Tensorflow object detection API :https://github.com/tensorfl ...
- 建立Heapster Influxdb Grafana集群性能监控平台
依赖于kubenets dns服务 图形化展示度量指标的实现需要集成k8s的另外一个Addons组件: Heapster .Heapster原生支持K8s(v1.0.6及以后版本)和 CoreOS , ...
- my live boadband
id_boadband tel: 02511931324 ¥1600 包2年,10MB/S =100Mb,2018.12.1 ~ 2020.12.1 end