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 ...
随机推荐
- [Contest20180325]序列
Hogura有一个序列$a$,她希望你帮她维护下面的这些操作. $1\ l\ r\ x$对$l\leq i\leq r$的$a_i$执行$a_i=a_i+x$ $2\ l\ r\ x$对$l\leq ...
- Android介绍
Android系统的底层建立在Linux系统之上,该平台有操作系统,中间件,用户界面和应用软件4层组成,它采用一种被称为软件叠层(Software Stack)的方式进行构建. 1.应用程序层:And ...
- Unix高级环境编程,编译时的err_sys和err_quit错误
err_sys以及err_quit等函数不是C语言自带函数,是作者自己编写的函数.所以,想要运行书中的源代码,就必须自建一个头文件my_err.h把作者的代码拷贝进去,然后在程序中加载. #inclu ...
- Intellij IDEA System.out.println输出中文乱码问题
进行下列设置即可:
- Codeforces D. Giving Awards 412 题解
就是依照一定顺序输出排序. 比方a欠b的钱就不能先输出a然后输出b. 本题的技巧就是.要求的是不能先输出a然后输出b,可是能够先输出b然后输出a. 故此能够依照a欠b的钱的关系.建立图,然后DFS深度 ...
- J2EE开发工作中遇到的异常问题及解决方法总结
参考博文:http://blog.csdn.net/rchm8519/article/details/41624381
- Mounting the NFS share on a Windows server
今天遇到一个相当奇怪的问题,在windows 上mount LINUX NFS, powershell 脚本可以成功, 用图形界面也可以成功,但BATCH就是不行.提示53网络错误. 不过公司已经有人 ...
- spring利用后置处理器初始化bean属性
spring利用后置处理器初始化bean属性 参考:http://blog.csdn.net/heyutao007/article/details/50326793 @Configurable @En ...
- 怎么成为PHP专家
当阅读各种和PHP相关的博客.Quora问题.Google+社区.资讯和杂 志的时候,经常会注意到在技术上出现两极分化的现象.问题均围绕在“我如何连接到一个MySQL数据库?”,或者是“我如何使得邮件 ...
- WCF调试异常信息:ServiceHost 仅支持类服务类型
"/CommonHelpServices"应用程序中的server错误. ServiceHost 仅支持类服务类型. 说明: 运行当前 Web 请求期间,出现未经处理的异常. 请检 ...