LeetCode 561:数组拆分 I Array Partition I
文章全部来自公众号:爱写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:
- 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].
提示:
- n 是正整数,范围在 [1, 10000].
- 数组中的元素范围在 [-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的更多相关文章
- Java实现 LeetCode 561 数组拆分 I(通过排序算法改写PS:难搞)
561. 数组拆分 I 给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), -, (an, bn) ,使得从1 到 n 的 min(ai, bi ...
- Leetcode 561.数组拆分I
数组拆分 I 给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从1 到 n 的 min(ai, bi) 总 ...
- [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 561. 数组拆分 I
为了理解这种方法,让我们从不同的角度来看待问题.我们需要形成数组元素的配对,使得这种配对中最小的总和最大.因此,我们可以查看选择配对中最小值的操作,比如 (a,b)(a,b) 可能会产生的最大损失 ...
- 力扣(LeetCode)561. 数组拆分 I
给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从1 到 n 的 min(ai, bi) 总和最大. 示例 ...
- LeetCode之“数组”:Rotate Array
题目链接 题目要求: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, ...
- 力扣561. 数组拆分 I-C语言实现-简单题
题目 传送门 给定长度为 2n 的整数数组 nums ,你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从 1 到 n 的 min(a ...
- 561.数组拆分I
题目:给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从1 到 n 的 min(ai, bi) 总和最大. ...
- 561. 数组拆分 I
题目 python class Solution: def arrayPairSum(self, nums): """ :type nums: List[int] :rt ...
随机推荐
- 2019-11-29-WPF-元素裁剪-Clip-属性
原文:2019-11-29-WPF-元素裁剪-Clip-属性 title author date CreateTime categories WPF 元素裁剪 Clip 属性 lindexi 2019 ...
- QGIS中查看PostGIS空间数据库中的影像
在QGIS中的Browser中是无法显示PostGIS空间数据库中的影像 要找到影像显示打开"Database" –> "DB Manager" 右击-- ...
- maplotlib画柱状图并添加标签
import json from collections import Counter import matplotlib.pyplot as plt import matplotlib as mpl ...
- python3字符串常用操作练习
练习一下字符串的常用操作 #-*- coding:utf-8 -*- #字符串的常用操作 str = "1111 Hell :wo:rld! " #删除头尾所有指定字符串,默认移除 ...
- java基本程序设计结构总结
学习一门语言:(1)掌握它的表现形式(2)这些语言什么应用. 1.1关键字 1.关键字是被赋予了特殊含义的单词. 2.关键字特点:关键字所有字母都小写. 3.类名的每一个单词开头必须大写. 1.2标识 ...
- 腾讯WeTest兼容服务再次升级,支持小程序兼容
WeTest 导读 小程序作为微信内能被便捷地获取和传播的工具,吸引着越来越多的开发者加入其中.无论是小游戏.零售.出行服务还是生活餐饮等,各行各业的小程序出现在用户的手机上,在给用户带来便利的同时, ...
- Linux Workqueue【转】
转自:http://kernel.meizu.com/linux-workqueue.html 21 August 2016 Workqueue 是内核里面很重要的一个机制,特别是内核驱动,一般的 ...
- AssetBundleMaster_ReadMe_CN
在开始使用之前, 建议先导入到一个空的工程里, 通过ReadMe的一步步引导使你对整个框架以及文件结构进行熟悉, 之后再考虑导入到现有工程中使用, 完整看完教程大概需要2个小时左右. 先看看文件夹结构 ...
- SpringMVC Junit Demo
package com.zchx.test; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.L ...
- NLP中的预训练语言模型(三)—— XL-Net和Transformer-XL
本篇带来XL-Net和它的基础结构Transformer-XL.在讲解XL-Net之前需要先了解Transformer-XL,Transformer-XL不属于预训练模型范畴,而是Transforme ...