Pretty easy python solution with explaination


  • 0
    Y

    For example, input [1,4,3,2], to make the the largest sum of min(ai, bi), the target pairs should be (1,2) and (3,4). Let's see the hidden pattern inside. If each pair (ai, bi) contains the neighbors in the sorted array, the sum of each min can be the largest. And then the solution is written straight forward with the idea,just add the odd index of sorted array to the sum and report.

        def arrayPairSum(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            nums.sort()
            s,i=0,0
            while i<len(nums):
                s+=nums[i]
                i+=2
            return s
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.