1. Topics
Given an array of length 2n , your task is to divide these numbers into n pairs, for example (A1, B1), (A2, B2), ..., (an, bn), so that the sum of min (AI, bi) from 1 to n is the largest.
Example 1:
Input: [1,4,3,2] Output: 4 Explanation: N equals 2, the maximum sum is 4 = min (1, 2) + min (3, 4).
Tips:
-
n is a positive integer range in [1, 10000].
-
The range of elements in the array is [-10000, 10000].
2. Solutions
Class solution (Object): Def arraypairsum (self, Nums): "" ": Type Nums:list[int]: rtype:int "" "ListNum = List (nums) listnum.sort () sum = 0 for i in range (0, Len (listNum), 2): Sum + = Listnum[i] return sum
The principle is that, to sort first, such a small element and the smallest combination except it will not sacrifice the big. This is the biggest. This is done using the sort method in the Python list.
http://www.codeblogbt.com/archives/39689
Array split I array-partition Leetcode python