561. Array Partition I【easy】
561. Array Partition I【easy】
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:
- 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].
解法一:
class Solution {
public:
int arrayPairSum(vector<int>& nums) {
if (nums.empty()) {
return ;
}
sort(nums.begin(), nums.end());
int sum = ;
for (int i = ; i < nums.size(); i += ) {
sum += nums[i];
}
return sum;
}
};
为了不浪费元素,先排序,这样可以保证min加出来为max
比如[1, 9, 2, 4, 6, 8]
如果按顺序来的话,1和9就取1,2和4就取2,6和8就取6,显而易见并不是最大,原因就是9在和1比较的时候被浪费了,9一旦浪费就把8也给影响了,所以要先排序
@shawngao 引入了数学证明的方法,如下:
Let me try to prove the algorithm...
- Assume in each pair
i,bi >= ai. - Denote
Sm = min(a1, b1) + min(a2, b2) + ... + min(an, bn). The biggestSmis the answer of this problem. Given1,Sm = a1 + a2 + ... + an. - Denote
Sa = a1 + b1 + a2 + b2 + ... + an + bn.Sais constant for a given input. - Denote
di = |ai - bi|. Given1,di = bi - ai. DenoteSd = d1 + d2 + ... + dn. - So
Sa = a1 + a1 + d1 + a2 + a2 + d2 + ... + an + an + di = 2Sm + Sd=>Sm = (Sa - Sd) / 2. To get the maxSm, givenSais constant, we need to makeSdas small as possible. - So this problem becomes finding pairs in an array that makes sum of
di(distance betweenaiandbi) as small as possible. Apparently, sum of these distances of adjacent elements is the smallest. If that's not intuitive enough, see attached picture. Case 1 has the smallestSd.

561. Array Partition I【easy】的更多相关文章
- 167. Two Sum II - Input array is sorted【easy】
167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...
- 167. Two Sum II - Input array is sorted【Easy】【双指针-有序数组求两数之和为目标值的下标】
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- 1. Two Sum【easy】
1. Two Sum[easy] Given an array of integers, return indices of the two numbers such that they add up ...
- 26. Remove Duplicates from Sorted Array【easy】
26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...
- 88. Merge Sorted Array【easy】
88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...
- 448. Find All Numbers Disappeared in an Array【easy】
448. Find All Numbers Disappeared in an Array[easy] Given an array of integers where 1 ≤ a[i] ≤ n (n ...
- 189. Rotate Array【easy】
189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...
- 121. Best Time to Buy and Sell Stock【easy】
121. Best Time to Buy and Sell Stock[easy] Say you have an array for which the ith element is the pr ...
- 27. Remove Element【easy】
27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and ...
随机推荐
- []ARC098
咕咕咕 C:普及组难度的题 D:给定$a_{1\cdots n}$,求有多少$1\leq l\leq r\leq n$满足$x_l+\cdots+x_r=x_l\text^\cdots\text^x_ ...
- 【博弈论】【SG函数】hdu1848 Fibonacci again and again
某个状态的SG函数被定义为 除该状态能一步转移到的状态的SG值以外的最小非负整数. 有如下性质:从SG值为x的状态出发,可以转移到SG值为0,1,...,x-1的状态. 不论SG值增加与否,我们都可以 ...
- 【分块】bzoj1798 [Ahoi2009]Seq 维护序列seq
分块,打标记,维护两个标记:乘的 和 加的. 每次 区间乘的时候,对 乘标记 和 加标记 都 乘上那个值. 每次 区间加的时候 对 加标记 加上那个值. (ax+b)*v=axv+bv.开 long ...
- golang设计模式-成员变量赋值
常见golang的struct赋值有两种: 1)定义变量同时初始化 val := &Options{ UID:int(1), } 2)先定义变量,再赋值 val := new(Options) ...
- Linux下判断字符串长度
方法1:使用wc -L命令 wc -L可以获取到当前行的长度,因此对于单独行的字符串可以用这个简单的方法获取,另外wc -l则是获取当前字符串内容的行数. echo 'abc' |wc -L 注意:这 ...
- 连接zookeeper集群
连接到ZooKeeper集合 ZooKeeper类通过其构造函数提供connect功能.构造函数的签名如下 : ZooKeeper(String connectionString, int sessi ...
- win10 彻底删除mysql步骤
转载自:https://blog.csdn.net/sxingming/article/details/52601250 1. 停止MySQL服务 开始->所有应用->Windows管理工 ...
- ThreadPoolExecutor 的三种提交任务方式
学习内容: ExecutorService线程池的应用... 1.如何创建线程池... 2.调用线程池的方法,获取线程执行完毕后的结果... 3.关闭线程... 首先我们先了解一下到底什么是线程池 ...
- PHP addslashes() 函数
定义和用法 addslashes() 函数在指定的预定义字符前添加反斜杠. 这些预定义字符是: 单引号 (') 双引号 (") 反斜杠 (\) NULL 语法 addslashes(stri ...
- linux基础-第二十单元_计划任务crond服务
第二十单元 计划任务crond服务 什么是计划任务:后台运行,到了预定的时间就会自动执行的任务,前提是:事先手动将计划任务设定好.这就用到了crond服务 crond服务相关的软件包[root@MiW ...