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 biggestSm
is the answer of this problem. Given1
,Sm = a1 + a2 + ... + an
. - Denote
Sa = a1 + b1 + a2 + b2 + ... + an + bn
.Sa
is 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
, givenSa
is constant, we need to makeSd
as small as possible. - So this problem becomes finding pairs in an array that makes sum of
di
(distance betweenai
andbi
) 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 ...
随机推荐
- hadoop运行常见错误
1)“no job jar file set”原因 又是被折腾了一下午呀~~,“no job jar file set”就是找不到作业jar包的意思,然后就是提示找不到自定义的MyMapper类,一般 ...
- nginx 实现 ajax 跨域请求
原文:http://www.nginx.cn/4314.html AJAX从一个域请求另一个域会有跨域的问题.那么如何在nginx上实现ajax跨域请求呢?要在nginx上启用跨域请求,需要添加a ...
- wpf一些例子
相关知识点:WPF - Adorner WPF Diagram Designer http://www.codeproject.com/Articles/484616/MVVM-Diagram-Des ...
- [Android 新特性] 谷歌发布Android Studio开发工具1.0正式版(组图) 2014-12-09 09:35:40
Android Studio是谷歌于13年I/O大会推出的Android开发环境,基于IntelliJ IDEA. 类似 Eclipse ADT,Android Studio 提供了集成的Androi ...
- django admin中文输入编码错误
修改models里面的str方法,改为unicode class Category(models.Model): name = models.CharField(max_length=20, verb ...
- javascript快速入门20--Cookie
Cookie 基础知识 我们已经知道,在 document 对象中有一个 cookie 属性.但是 Cookie 又是什么?“某些 Web 站点在您的硬盘上用很小的文本文件存储了一些信息,这些文件就称 ...
- http://www.cnblogs.com/langtianya/archive/2013/02/01/2889682.html
http://www.cnblogs.com/langtianya/archive/2013/02/01/2889682.html
- 【玩转cocos2d-x之三十九】Cocos2d-x 3.0截屏功能集成
3.0的截屏和2.x的截屏基本上同样.都是利用RenderTexture来处理,在渲染之前调用call函数,然后调用Cocos的场景visit函数对其进行渲染,渲染结束后调用end函数就可以.仅仅是3 ...
- 怎样优化cocos2d/x程序的内存使用和程序大小
再次感谢原创者:Steffen Itterheim.原创博客原文地址: http://www.learn-cocos2d.com/2012/11/optimize-memory-usage-bundl ...
- java之static关键字
介绍: 1.在类中,用static声明的成员变量为静态成员变量,它为该类的公用变量,在第一次使用时被初始化,对于该类的所有对象来说,static成员变量只有一份. 2.用static声明的方法为静态方 ...