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:

  1. n is a positive integer, which is in the range of [1, 10000].
  2. 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的更多相关文章

  1. Leetcode#561. Array Partition I(数组拆分 I)

    题目描述 给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从1 到 n 的 min(ai, bi) 总和最 ...

  2. 561. Array Partition I【easy】

    561. Array Partition I[easy] Given an array of 2n integers, your task is to group these integers int ...

  3. 【LEETCODE】39、第561题 Array Partition I

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  4. 561. Array Partition I - LeetCode

    Question 561. Array Partition I Solution 题目大意是,给的数组大小是2n,把数组分成n组,每组2个元素,每个组取最小值,这样就能得到n个值,怎样分组才能使这n个 ...

  5. [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 ...

  6. 【LeetCode】数组-6(561)-Array Partition I(比较抽象的题目)

    题目描述:两句话发人深思啊.... Given an array of 2n integers, your task is to group these integers into n pairs o ...

  7. 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 ...

  8. 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 ...

  9. [LeetCode] Array Partition I 数组分割之一

    Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...

随机推荐

  1. switch实现一个两数的运算

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. C# 单例模式(Singleton Pattern)(转SuagrMatl)

    单例模式的定义: 确保一个类只有一个实例,并提供一个全局访问点. 首先实例大家应该都明白就是类生成对象的过程简单的就是String s=new String(),则s就是个实例. Q:如何只生成一个实 ...

  3. PHP常量定义define与const

    一.const PHP5.3以前,const只能在类内部声明变量,5.3+允许在外部声明变量,但还不能使用常量计算! const ONE = 1; const WORD = 'hello world' ...

  4. element ui datePicker 设置当前日期之前的日期不可选

    pickerOptions0: { disabledDate(time) { return time.getTime() < Date.now() - 8.64e7 } },

  5. ArrayBuffer和TypedArray,以及Blob的使用

    前端使用TypedArray编辑二进制 ES6提供了, ArrayBuffer和TypedArray, 让前端也可以直接操作编辑二进制数据, 网页中的类型为file的input标签, 也可以通过Fil ...

  6. NHibernate教程(12)--延迟加载

    本节内容 引入 延迟加载 实例分析 1.一对多关系实例 2.多对多关系实例 结语 引入 通过前面文章的分析,我们知道了如何使用NHibernate,比如CRUD操作.事务.一对多.多对多映射等问题,这 ...

  7. Seesion工作原理

    session的工作原理一.术语session 在我的经验里,session这个词被滥用的程度大概仅次于transaction,更加有趣的是transaction与session在某些语境下的含义是相 ...

  8. 团队作业8----第二次项目冲刺(Beta阶段) 第二天

    BETA阶段冲刺第二天 1.开了个小会议 2.每个人的工作 (1) 昨天已完成的工作: 昨天把学生的登录功能完善了并且优化了下界面 (2) 今天计划完成的工作: (3) 工作中遇到的困难: 1.团队还 ...

  9. 201521123048《Java程序设计》第6周学习总结

    1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图,对面向对象思想进行一个总结. 2. 书面作业 1.clone方法 1.1 Object对 ...

  10. 201521123030 《Java程序设计》 第9周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 本次PTA作业题集异常 常用异常 1.题目5-1 1.1 截图你的提交结果(出现学号) 1.2 自己 ...