Python, Simple with Explanation

  • 10

    Consider the smallest element x. It should be paired with the next smallest element, because min(x, anything) = x, and having bigger elements only helps you have a larger score. Thus, we should pair adjacent elements together in the sorted array.

    def arrayPairSum(self, A):
        return sum(sorted(A)[::2])

  • 0

    I don't really understand the problem's meaning,it's describe (an,bn),min(ai,bi) for all i to n but also appears (a1,b2).

  • 0

    @dick159 What it actually means is that, given an array of say 10 integers(2*5 elements), you can derive 5 pairs of numbers, among each pair, pick the minimum and take the sum.
    So that looks like sum of min(each pair).

  • 0

    @awice said in Python, Simple with Explanation:

    return sum(sorted(A)[::2])
    If i give input as [1,54,9,97,45,49] expected result via Run Code is 100. But there is no pair in the input that makes Sum as 100.
    Expected result according to me is 55.
    Am I correct?

  • 0

    @awice Very good approach!

  • 0

    @awice It's the simplest and best explanation of this question!

Log in to reply

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