文章全部来自公众号:爱写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. My time is limited

    Your time is limited, so don't waste it living someone else's life. Don't be trapped by dogma - whic ...

  2. C# .NET 使用 NPOI 读取 .xlsx 格式 Excel

    string filePath = @"C:\Users\yangqinglin\Desktop\test.xlsx"; IWorkbook wk = null; string e ...

  3. WPF控件模板(6)

    什么是ControlTemplate? ControlTemplate(控件模板)不仅是用于来定义控件的外观.样式, 还可通过控件模板的触发器(ControlTemplate.Triggers)修改控 ...

  4. annyconnect掉线之后重新链接

    sudo service vpnagentd restart /opt/cisco/anyconnect/bin/vpnui 重启服务+重新登录 deepin的优点之一是它的程序不会安装到各个角落里, ...

  5. SpringMVC 之 上传文件

    一.需求: 利用SpringMVC实现上传文件的功能 二.思路: 1.我们可以在SpringMVC中,通过配置一个MultipartResolver来上传文件. 2.通过MultipartFile f ...

  6. 章节十五、2-PageObjectModel

    一.在实现自动化过程中,会有很多重复的代码,我们在维护代码时会很困难,如果想解决这个问题,我们就需要使用PageObjectModel(页面对象模型)的方式来进行自动化代码的书写. 二.案例演示 以该 ...

  7. 3.1 Spark概述

    一.Spark简介 1.Spark的特点 特点1:运行速度快(内存计算,循环数据流.有向无环图设计机制) 把所有针对数据集的操作转换成一张有向无环图,整个执行引擎调度都是基于这个有向无环图,对这个有向 ...

  8. vue-cli 3 和 vue-cli 2的区别

    分享文章: 浅谈vue-cli 3 和 vue-cli 2的区别 https://blog.csdn.net/weixin_42080056/article/details/81631661 vue- ...

  9. 几个python编程例子

    作业 有如下值集合[11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中,结果为{'k1': ...

  10. mysql select limit 大数据量查询 性能终极提升方法

    还是广告位 我们的使用mysql的时候总是想当然的使用 select × from tables where a>0 order by id desc limit 500000,200 当我们真 ...