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:
Note:
n is 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].
这题第一个想到的方法是排序然后把双数位的数加起来。因为要min,所以相邻两个取min能取到最大和。具体数学证明不会。因为这是two pointer题里的,所以一看到就排序了。T: O(NlogN), S O(1)
另外在solution里看到一种T: O(N), S: O(N)的解法,不明觉厉。因为总数就只有2万个数字,所以建一个hashmap来数数字。主要是控制每一个数取多少个。举个例子:例如给的nums是{1, 1, 1, 3},那么1会被取两次。arr长这个样子[...0, 0, 3, 0, 1, 0, 0, ...], (1有3个,3 有一个)
Last updated