文章全部来自公众号:爱写bug

算法是一个程序的灵魂。

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.

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

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

提示:

  1. n 是正整数,范围在 [1, 10000].
  2. 数组中的元素范围在 [-10000, 10000].

解题思路:

​ 其实就是把 数组排序,然后按顺序 每两个数既是一对,每对的第一个数累加之和即为所求。就是考一下各类排序算法的性能。

先使用内置 sort() 函数理解一下思路:

Java:

import java.util.Arrays;
class Solution {
public int arrayPairSum(int[] nums) {
Arrays.sort(nums);
int sum=0;
for (int i=0;i<nums.length;i+=2){
sum+=nums[i];
}
return sum;
}
}

扩展:

维基百科上对排序算法介绍的非常详细,并且进行了归类比较,地址: https://zh.wikipedia.org/wiki/%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95

这里简单推荐两个:

  • 快速排序(quick sort)—期望时间,最坏情况;对于大的、随机数列表一般相信是最快的已知排序(C语言标准库的qsort()排序用的就是快速排序算法,利用递归和分而治之思想)
  • 桶排序(bucket sort)—;需要额外空间(典型的牺牲空间换时间)

冒泡排序、选择排序都是比较简单容易理解,复杂度是 n^2 ,所以不再赘述。



LeetCode 561:数组拆分 I Array Partition I的更多相关文章

  1. Java实现 LeetCode 561 数组拆分 I(通过排序算法改写PS:难搞)

    561. 数组拆分 I 给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), -, (an, bn) ,使得从1 到 n 的 min(ai, bi ...

  2. Leetcode 561.数组拆分I

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

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

  4. leetcode 561. 数组拆分 I

    为了理解这种方法,让我们从不同的角度来看待问题.我们需要形成数组元​​素的配对,使得这种配对中最小的总和最大.因此,我们可以查看选择配对中最小值的操作,比如 (a,b)(a,b) 可能会产生的最大损失 ...

  5. 力扣(LeetCode)561. 数组拆分 I

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

  6. LeetCode之“数组”:Rotate Array

    题目链接 题目要求: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, ...

  7. 力扣561. 数组拆分 I-C语言实现-简单题

    题目 传送门 给定长度为 2n 的整数数组 nums ,你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从 1 到 n 的 min(a ...

  8. 561.数组拆分I

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

  9. 561. 数组拆分 I

    题目 python class Solution: def arrayPairSum(self, nums): """ :type nums: List[int] :rt ...

随机推荐

  1. 2019-11-25-win10-uwp-通过命令行脚本开启旁加载

    原文:2019-11-25-win10-uwp-通过命令行脚本开启旁加载 title author date CreateTime categories win10 uwp 通过命令行脚本开启旁加载 ...

  2. Winform中设置ZedGraph的颜色填充使用Fill

    场景 Winforn中设置ZedGraph曲线图的属性.坐标轴属性.刻度属性: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...

  3. CefSharp F12打开DevTools查看console js和c#方法互相调用

    转载地址: https://www.cnblogs.com/lonelyxmas/p/11010018.html winform嵌入chrome浏览器,修改项目属性 生成 平台为x86 1.nuget ...

  4. yii2 HeadersAlreadySentException 报错

    An Error occurred while handling another error:exception 'yii\web\HeadersAlreadySentException' with ...

  5. C# 获取社会统一信用代码

    时间不多,废话少说: 网络请求代码如下: using System; using System.Collections.Generic; using System.Linq; using System ...

  6. 修改源代码时不需要重启tomcat服务器

    我们在写JSP + Servlet 的时修改了Java代码就要重新启动服务器.十分麻烦. 为了解决这个问题我们可以将服务器改成debug 模式.就是按调试状态这样修改Java代码就不用再重新启动服务器 ...

  7. Django RestFramework(DRF)类视图

    基础视图 1.基础函数视图(@api_view) DRF提供了一种函数基础视图来装饰Django的普通视图,我们可以使用request来接受请求和response响应.一个小例子: from rest ...

  8. Nginx+Docker部署模式下 asp.net core 获取真实的客户端ip

    目录 Nginx+Docker部署模式下 asp.net core 获取真实的客户端ip 场景 过程还原 结论 参考资料 Nginx+Docker部署模式下 asp.net core 获取真实的客户端 ...

  9. Artificial Intelligence in Finance

    https://sigmoidal.io/real-applications-of-ai-in-finance/ Artificial Intelligence is taking the finan ...

  10. Redis笔记1-Redis介绍及数据类型使用场景

    Redis介绍:C语言开发.单线程操作.高性能.键值对.可持久化的数据库.Redis采用redisObject结构来统一五种数据类型,redisObject是五种类型的父类,可以在函数间传递时隐藏具体 ...