leetcode/0561.Array-Partition/README.md
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.
Example 1:
Input: [1,4,3,2]
Output: 4
Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4).
Note:
给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从1 到 n 的 min(ai, bi) 总和最大。
package leetcode
func arrayPairSum(nums []int) int {
array := [20001]int{}
for i := 0; i < len(nums); i++ {
array[nums[i]+10000]++
}
flag, sum := true, 0
for i := 0; i < len(array); i++ {
for array[i] > 0 {
if flag {
sum = sum + i - 10000
}
flag = !flag
array[i]--
}
}
return sum
}