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 ...
随机推荐
- [Atcoder Grand Contest 003] Tutorial
Link: AGC003 传送门 A: 判断如果一个方向有,其相反方向有没有即可 #include <bits/stdc++.h> using namespace std; ]; map& ...
- 【spfa】bzoj3921 Mimori与树海
考虑“删除后图仍连通”,即其不是无向图的桥(bridge),可以用Tarjan算法预处理,这里不赘述. [算法一] 枚举删除的是哪条边,然后枚举起点,暴搜,统计答案. 可以通过0.1号测试点. 预计得 ...
- 1.1(学习笔记)Servlet简介及一个简单的实例
一.Servlet简介 Servlet是使用Java语言编写的服务器端程序,可以生产动态的Web界面. 主要运行在服务器端,Servlet可以方便的处理客户端传来的HTTP请求,并返回一个响应. 二. ...
- Problem A: 零起点学算法16——鸡兔同笼
#include<stdio.h> int main() { int n,m,a,b; while(scanf("%d %d",&n,&m)!=EOF) ...
- C#正则表达式开源工具
先交代一下背景,最近工作中经常用到正则表达式,而正则表达式这个东西我个人觉得很鸡肋,不用吧,有些功能实现起来会很麻烦.用吧,又不是说工作中经常用到,只是有时候有些需要求用到而已.但是正则表达式只要一段 ...
- Ubuntu 16.04通过源码安装QUEM虚拟机
下载编译安装: wget http://download.qemu-project.org/qemu-2.9.0.tar.xz tar xvJf qemu-2.9.0.tar.xz cd qemu-2 ...
- 继承了母板页的onload的事件
这段时间实在是太忙了, 现借国庆放假之际,把一些问题写一下, 不久前,有位网友问我继承了母版页的页面要加载body的onload事件如何加载,我以前刚开始用母板的时候也碰到过这种问题, 直接是用JAV ...
- [Lync]lync同步通讯簿
概述 在客户现场部署lync的时候,突然发现新安装的lync客户端,搜索联系人功能无法使用,而将lync客户端安装后,隔一段时间后,又可以查询了,发现可能是数据没有同步的原因. 解决方案 客户端地址簿 ...
- Tomcat部署时war和war exploded的区别
转自徐刘根的Tomcat部署时war和war exploded区别以及平时踩得坑 一.war和war exploded的区别 在使用IDEA开发项目的时候,部署Tomcat的时候通常会出现下边的情况: ...
- [转]C++中关于new和delete的使用
转载的地址 近一直在啃 C++ Primer 中文版第4版,发现 C++中new和delete应用遍布全书,现对其使用作简单总结.在C++中,可以使用new和delete动态创建和释放数组或者单个对象 ...