题目要求

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. zookeeper 入门(一)

    1 下载安装 wget http://mirrors.hust.edu.cn/apache/zookeeper/stable/zookeeper-3.4.6.tar.gzcp zookeeper-3. ...

  2. ZigBee基础

    Zigbee技术 Zigbee由Zigbee联盟制定的无线网络协议,在IEEE 802.15.4标准的基础上设计,是一种自愈.安全和稳健的网状网协议,可扩展到更大范围内的数百个节点.主要用于距离短.功 ...

  3. not in 的优化

    //---------------------- 建表1 ---------------------- create table TESTTABLE( id1 VARCHAR2(12), name V ...

  4. Sword redis存取二进制数据

    #include "hiredis/hiredis.h" /* redis头文件 */ #include <stdio.h> #include <stdlib.h ...

  5. Java如何根据主机名(域名)获取IP地址?

    在Java编程中,如何根据主机名(域名)获取IP地址? 以下示例显示了如何通过net.InetAddress类的InetAddress.getByName()方法将主机名更改为指定的IP地址. pac ...

  6. IDEA VS 常用高效 黄金 快捷键

    [参考] VS 常用高效 快捷键 身为一个编程人员,掌握IDE的快键是提高开发效率最简单直接的方法,也是必备技能.和网上的大篇罗列不同,下面只讲精髓,根据实践不断调整.本人C#转Java,曾经试过Ec ...

  7. PM2 指令简介

    pm2 是一个带有负载均衡功能的Node应用的进程管理器.当你要把你的独立代码利用全部的服务器上的所有CPU,并保证进程永远都活着,0秒的重载, PM2是完美的,下面我们来看pm2常用的命令用法介绍吧 ...

  8. [IR] Dictionary Coding

    [数据压缩]LZ77算法原理及实现 [数据压缩]LZ78算法原理及实现 Lempel–Ziv–Welch 年发表的论文<A Universal Algorithm for Sequential ...

  9. websql的使用/phonegap操作数据库 sqlite

    对websql的常用操作进行了一个封装,项目是基于phonegap的,不过phonegap默认已经集成了数据库操作的插件,所以无需再配置什么,直接用就可以了: /** *数据库操作辅助类,定义对象.数 ...

  10. 【Scikit】实现Multi-label text classification代码模板

    Refer to: https://stackoverflow.com/a/10527953 code: # -*- coding: utf-8 -*- import numpy as np from ...