题目要求

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对整数对,使得每个整数对的最小值的和最大,并返回这个最大值。可以将数组从小到大进行排序,然后两两组合,这样可以得到最大值。

python代码​

class Solution:

def arrayPairSum(self, nums):

"""

:type nums: List[int]

:rtype: int

"""

nums.sort()

newnums = nums[::2]

sum = 0

for i in newnums:

sum += i

return sum

LeetCode 561 Array Partition I 解题报告的更多相关文章

  1. 【LeetCode】561. Array Partition I 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...

  2. 【LeetCode】86. Partition List 解题报告(Python)

    [LeetCode]86. Partition List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...

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

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

  4. 【LeetCode】763. Partition Labels 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 日期 题目地址:https://leetcode.com/pr ...

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

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

  7. [LeetCode] 561. Array Partition I_Easy tag: Sort

    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 (C++)

    题目: Given an array of 2n integers, your task is to group these integers into npairs of integer, say ...

  9. 561. Array Partition I - LeetCode

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

随机推荐

  1. 常见Python爬虫工具总结

    常见Python爬虫工具总结 前言 以前写爬虫都是用requests包,虽然很好用,不过还是要封装一些header啊什么的,也没有用过无头浏览器,今天偶然接触了一下. 原因是在处理一个错误的时候,用到 ...

  2. Google file system

    读完了Google file system论文的中文版,记录一下总结,懒得打字,直接上草图:

  3. Java知多少(27)继承的概念与实现

    继承是类与类之间的关系,是一个很简单很直观的概念,与现实世界中的继承(例如儿子继承父亲财产)类似. 继承可以理解为一个类从另一个类获取方法和属性的过程.如果类B继承于类A,那么B就拥有A的方法和属性. ...

  4. Java知多少(28)super关键字

    super 关键字与 this 类似,this 用来表示当前类的实例,super 用来表示父类. super 可以用在子类中,通过点号(.)来获取父类的成员变量和方法.super 也可以用在子类的子类 ...

  5. 深度解析Java 8:JDK1.8 AbstractQueuedSynchronizer的实现分析

    深度解析Java 8:JDK1.8 AbstractQueuedSynchronizer的实现分析(上) 深度解析Java 8:AbstractQueuedSynchronizer的实现分析(下) A ...

  6. 【转帖】解决远程连接MariaDB(mysql)很慢的方法

    在CentOS7上安装完成MariaDB之后,发现无论命令行还是程序中连接MariaDB的时候都很慢,大约要一二十秒,于是网上搜索了一番,发现下面的文章内容: 在进行 ping和route后发现网络通 ...

  7. Orchard CMS -Migration文件更新后数据库不更新的问题 new properties not updating after migrationData migration is not working?

    Orchard CMS - new properties not updating after migrationData migration is not working? If your mapp ...

  8. [Object Tracking] Contour Detection through OpenCV

    利用OpenCV检测图像中的长方形画布或纸张并提取图像内容 - 阅读笔记 相对来说,如下链接是此文的高阶方案版本,做对比是极好的. [Object Tracking] Contour Detectio ...

  9. NLog类库的使用探索——认识配置+实习小感悟

    1 写在前面 1.1 为什么学了软件 1.高考失败,分数不高,不能随心所欲 2.农村孩子,学点技术,将来有口饭吃 3.有科技含量,想玩电脑(那个时候是这么想的,那个时候觉得学计算机就是玩电脑) 1.2 ...

  10. Proc文件系统接口调试

    在tpd_i2c_probe I2C的探测函数中创建proc接口 //----------------------------------------------------------------- ...