题目要求

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开发-与其他语言的比较

    1.关于函数 1)不需要指定返回类型,不需要指定是否有返回值,每个函数都有返回值,没有的话,就返回None 2)参数也可以不指定类型,可以有默认参数,但是必须放到最后,调用的时候指定参数的值,和顺序无 ...

  2. Manager升职了

    公司去年从每年七月份公布officer升职改成了每年四月份公布. 早上收到大头发给全公司的邮件,赫然发现Manager升了一级到VP,虽然是金融公司,但我司的VP好像会比银行多一点点福利,比如额外假期 ...

  3. 【Linux】防火墙与CentOS中的iptables

    [iptables] 参考好文:http://www.zsythink.net/archives/1199.这个博客的作者写了深入浅出的iptables介绍,基本上我就是做个他的读书笔记. ■ 基本介 ...

  4. Java如何匹配列表中的电话号码?

    在Java编程中如何匹配列表中的电话号码? 以下示例显示如何使用phone.matches(phoneNumberPattern)方法将列表中的电话号码与指定模式相匹配. package com.yi ...

  5. spring data jpa 查询自定义字段,转换为自定义实体

    目标:查询数据库中的字段,然后转换成 JSON 格式的数据,返回前台. 环境:idea 2016.3.4, jdk 1.8, mysql 5.6, spring-boot 1.5.2 背景:首先建立 ...

  6. [Laravel] 08 - Auth & Data Migration

    登录注册框架 一.加载Auth模块 Step 1, 安装Auth模块 生成相关 laravel 框架内部的代码模块: $ php artisan make:auth 自动添加了路由代码 在larave ...

  7. Mac OSX安装启动 zookeeper

    安装 zookeeper支持brew安装 ➜ ~ brew info zookeeper zookeeper: stable (bottled), HEAD Centralized server fo ...

  8. 【代码审计】XYHCMS V3.5任意文件读取漏洞分析

      0x00 环境准备 XYHCMS官网:http://www.xyhcms.com/ 网站源码版本:XYHCMS V3.5(2017-12-04 更新) 程序源码下载:http://www.xyhc ...

  9. Fiddler 简介

    Fiddler 简介: (1) Fiddler 是一个抓包工具,主要用来对 HTTP 请求进行分析,浏览器按 F12 也可以进行抓包,但是比较轻量,不支持一些复杂的抓包:(2) WireShark 工 ...

  10. [Linux] 修改系统默认编码

    locale 命令 locale 命令用以设置程序运行的语言环境. locale 设置语言环境的命名规则为 Language_area.charset,例如 en_US.utf8 表示语言为英语,地区 ...