[easy] 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, 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:
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].

代码是:

class Solution(object):
    def arrayPairSum(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        nums.sort()
        sum,i = 0,0
        while i < len(nums):
            sum += nums[i]
            i += 2
        
        return sum
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,349评论 0 33
  • 文/史逍遥 10.三英战吕布 反董第一枪打响,长沙太守孙坚打头阵,率队攻打洛阳汜水关。 但是,在任何年代和任何时候...
    史逍遥阅读 9,151评论 0 0
  • 晨, 宁静而安详, 洗去昨夜繁华和嘈杂。 晨, 希望在发芽, 一天之计在于晨, 奋斗开始吧! 晨,奔跑, 晨,加油!
    无限极夏兰770621阅读 1,410评论 0 0
  • 3.28 我最近看的书多了,给我带来的是一种知识信息的爆炸。感觉到了头脑里的想法不断的冒出来,一个接着一个,给我造...
    昨日的记忆阅读 2,600评论 0 0
  • 关于“徐歌阳不雅视频事件”晨楠少彬微博误伤葛荟婕女士一事,晨楠少彬回应微博账号被盗,晨楠少彬发微博向葛荟婕女士,微...
    简书娱乐频道阅读 2,828评论 0 2