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].
分析:将给出的整型数组分成两两一组后,返回每组最小值之和,要求和最大。
思路:分组时,让每组的差的平方和最小即可。最简单的方式,直接排序,依次分组。
JAVA CODE
class Solution {
public int arrayPairSum(int[] nums) {
Arrays.sort(nums);
int sum = 0;
for(int i = 0; i < nums.length; i += 2){
sum += Math.min(nums[i],nums[i+1]);
}
return sum;
}
}
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 ...
- 【LEETCODE】39、第561题 Array Partition I
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 561. Array Partition I - LeetCode
Question 561. Array Partition I Solution 题目大意是,给的数组大小是2n,把数组分成n组,每组2个元素,每个组取最小值,这样就能得到n个值,怎样分组才能使这n个 ...
- [Swift]LeetCode561. 数组拆分 I | Array Partition I
Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...
- 【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 ...
- [LeetCode] Array Partition I 数组分割之一
Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...
随机推荐
- 关于Java的发展前景
各位看官觉得Java还能火几年?未来的发展方向是什么?
- cobbler实现自动安装
author:JevonWei 版权声明:原创作品 cobbler 配置目录 配置文件目录 /etc/cobbler /etc/cobbler/settings : cobbler 主配置文件 /et ...
- c# 读取excel数据的两种方法(转)
转载自:http://developer.51cto.com/art/201302/380622.htm, 方法一:OleDb: 用这种方法读取Excel速度还是非常的快的,但这种方式读取数据的时候不 ...
- .net core的在初始化数据的拦截处理
本人初接触 .net core 如有不对的地方,请大家随时指正,共同学习. 首先说明,此案例是基于.net core1.0版本的,对于2.0好多的功能已经升级,例如:一些常用的dll已经在框架中存在, ...
- 团队作业10——Beta版本事后诸葛亮
事后诸葛亮分析 1.总结的提纲内容: a. 项目管理之事后诸葛亮会议. 一.设想和目标 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们的软件要解决的是教师需要 ...
- spring+mybatis的简单配置示例
简单代码结构: //Book.java package com.hts.entity; public class Book { private String id; private String bo ...
- 201521123026《Java程序设计》第8周学习总结
1. 本章学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 2. 书面作业 Q1.1.List中指定元素的删除(题目4-1) 1.1 实验总结 答: 1.通过equals ...
- Linux如何设置dns
首先打开dns设置文档 空的dns文档如图所示 键入图片中的文本保存即可设置了自己的dns 保存后推出即可.
- 201521123011《Java程序设计》第10周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常与多线程相关内容. 2. 书面作业 本次PTA作业题集异常.多线程 1.finally 题目4-2 1.1 截图你的提交结果(出 ...
- mshadow的原理--MXNet
mshadow的原理--MXNet 这文章主要解释了表达式模板的工作原理(也是mshadow的主要原理),文章的前半部分是翻译自exp-template/README.md.我们会解释它为什么会影响编 ...