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 ...
随机推荐
- 【动态规划】bzoj1633 [Usaco2007 Feb]The Cow Lexicon 牛的词典
f[i]=min{f[i+1]+1,f[i+len[j]+cant]+cant}(for i=L-1 downto 0)(1<=j<=w) #include<cstdio> # ...
- jdk8新特性
JDK8新特性(JDK8的新特性) * 接口中可以定义有方法体的方法,如果是非静态,必须用default修饰 * 如果是静态的就不用了 class Test { public void run() { ...
- 图像视图-ImageView
(一) 知识点: (1)imageView.setImageAlpha(Alpha):设置图片透明度 (2)在布局imageView中设置图片位置:android:scaleType="ce ...
- catalina_home与catalina_base
CATALINA_HOME是Tomcat的安装目 录,CATALINA_BASE是Tomcat的工作目录. 如果我们想要运行Tomcat的多个实例,但是不想安装多个Tomcat软件副本.那么我们可以配 ...
- ntp流量放大攻击分析
最近,听说挂在网络上的设备进行时间同步成功率低,YS需要架设自己的NTP服务器,这玩意第一时间能让人想到NTP流量放大攻击,这也是一种比较古老的攻击方式,检测了一下发现所使用的OS默认已经进行了加固, ...
- 配置.net程序集搜索路径
默认情况下,.net程序对外部程序集dll的搜索路径是exe文件所在的目录,虽然这种方式没有什么太多不好的地方,但是当我们引用外部程序集较多的时候显得非常杂乱.一种比较常用的解决方式是通过配置在app ...
- 数据库问题5-SYS.SYSPROCESSES使用和查找死锁
http://blog.sina.com.cn/s/blog_62c4727d0100jc5z.html (一)理論部份 sys.sysprocesses (Transact-SQL) http:// ...
- Word如何设置单元格垂直居中
那两个上面是水平居中,下面是垂直居中.
- DBA_SEGMENTS - 查看数据库对象所分配的物理存储空间
SELECT SEGMENT_NAME,SEGMENT_TYPE,<code>TABLESPACE_NAME</code>,EXTENTS,BLOCKS,BYTES/1024/ ...
- Xamarin.Forms 调用 腾讯地图SDK
Xamarin.Forms研究了好一段时间了,最近一直在学习中,想尝试一下调用其他的SDK,就如腾讯地图SDK(申请容易). 完成此次项目得感谢以下链接: http://www.cnblogs.com ...