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

My method:

Step 1: sort the list L

Step 2: sum(L[0]+L[2]+L[4]+...+L[2n-2])

Code:

class Solution:
def arrayPairSum(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums=sorted(nums)
return sum(nums)-sum(nums[::-2])

  

[LeetCode&Python] Problem 561. 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 - LeetCode

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

  3. 561. Array Partition I【easy】

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

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

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

  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 解题报告

    题目要求 Given an array of 2n integers, your task is to group these integers into n pairs of integer, sa ...

  7. [LeetCode&Python] Problem 108. Convert Sorted Array to Binary Search Tree

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...

  8. [LeetCode&Python] Problem 905: Sort Array By Parity

    Given an array A of non-negative integers, return an array consisting of all the even elements of A, ...

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

随机推荐

  1. node - web 服务器 、server 服务器

    node 作为服务端 - 基本使用 1. web 服务器 web.html <!DOCTYPE html> <html> <head> <meta chars ...

  2. iOS进阶_Socket(Socket简介&代码演练)

    网络通讯三要素 TCP & UDP 三次握手 断开连接的四次握手 Socket套接字 了解了上面的概念,我们开始演练一下Socket #import "ViewController. ...

  3. python - 面向对象编程(初级篇)

    写了这么多python 代码,也常用的类和对象,这里准备系统的对python的面向对象编程做以下介绍. 面向对象编程(Object Oriented Programming,OOP,面向对象程序设计) ...

  4. tomcat和java环境

    mac tomcat http://blog.csdn.net/huyisu/article/details/38372663 mac jdk 1.8 http://wlb.wlb.blog.163. ...

  5. unittest参数化

    我们在写case的时候,如果用例的操作是一样的,就是参数不同,比如说要测一个登陆的接口,要测正常登陆的.黑名单用户登陆的.账号密码错误的等等,在unittest里面就要写多个case来测试. 这样的情 ...

  6. 76. Minimum Window Substring *HARD*

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  7. ECC算法整理纪要

    初始ECC算法 1.用户A 密钥生成 (1):用随机数发生器产生随机数k∈[1,n-1]: (2):计算椭圆曲线点PA=[k]G,为公钥,k为用户A私钥: 2. 用户B加密算法及流程 设需要发送的消息 ...

  8. 非常简单的vue里面引入jquery

    如何在vue里面引入jq了,只需四部就完成 第一步 cnpm install jquery 第二步 打开build文件夹 , 打开webpack.base.conf.js文件找到下面module.ex ...

  9. Android中aar和jar文件的认识

    在Android开发中,我们总是会引入其他第三方的库或者资源等,有时候是添加一个jar文件,有时候添加一个aar文件,那么这两种类型的文件有什么区别吗?详情请看下文. 一.描述. 1.   *.jar ...

  10. SHA1加密算法 java

    //下面四个import放在类名前面 包名后面 //import java.io.UnsupportedEncodingException; //import java.security.Messag ...