Leetcode 561. Array Partition I

Given an array of2nintegers, your task is to group these integers intonpairs 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.

Note:

nis 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].

给出一个长度为 2n 的整数数组,你的任务是将这些整数分成n组,每组两个一对,并求得所有分组中较小的数的总和(这个总和的值要尽可能的大)

思路:将整个数组升序排列,从下标为 0 处开始,每隔两个 取一个,并求和

return sum(sorted(nums)[::2])  #sum求和 sorted升序排列 [::2] 步长为2取

你可能感兴趣的:(Leetcode 561. Array Partition I)