首页 > 其他 > 详细

561. Array Partition I

时间:2020-06-29 14:54:03      阅读:61      评论:0      收藏:0      [点我收藏+]

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.

最优解应该是排序后,按顺序两两组成pairs,然后取所有pairs的第一个数的和。

class Solution(object):
    def arrayPairSum(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        ans = 0
        nums = sorted(nums)
        for i in range(0, len(nums), 2):
            ans += nums[i]
        return ans

 

561. Array Partition I

原文:https://www.cnblogs.com/whatyouthink/p/13207735.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!