561. Array Partition I

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.

题目大意:
给定一个长度为2n(偶数)的数组,分成n个小组,返回每组中较小值的和sum,使sum尽量大
Example 1:

Input: [1,4,3,2]
Output: 4
Explanation: n is 2, and the maximum sum of pairs is 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].

思路:

  • 先排序,将相邻两个数分为一组,每组较小数都在左边,求和即可

算法分析:
查看英文版请点击上方

  • 假设对于每一对i,bi >= ai。
  • 定义Sm = min(a1,b1)+ min(a2,b2)+ … + min(an,bn)。最大的Sm是这个问题的答案。由于bi >= ai,Sm = a1 + a2 + … + an。
  • 定义Sa = a1 + b1 + a2 + b2 + … + an + bn。对于给定的输入,Sa是常数。
  • 定义di = | ai - bi |。由于bi >= ai,di = bi-ai, bi = ai+di。
  • 定义Sd = d1 + d2 + … + dn。
  • 所以Sa = a1 + (a1 + d1) + a2 + (a2 + d2) + … + an + (an + di) = 2Sm + Sd , 所以Sm =(Sa-Sd)/ 2。为得到最大Sm,给定Sa为常数,需要使Sd尽可能小。
  • 所以这个问题就是在数组中找到使di(ai和bi之间的距离)的和尽可能小的对。显然,相邻元素的这些距离之和是最小的。

代码如下:

// https://leetcode.com/problems/array-partition-i/#/description原题
//https://discuss.leetcode.com/topic/87206/java-solution-sorting-and-rough-proof-of-algorithm
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:int arrayPairSum(vector<int>& nums) {int res = 0;sort(nums.begin(), nums.end());for(int i=0; i<nums.size(); i+=2){res += nums[i];}return res;}
};
int main()
{Solution a;int num[4] = {1, 4, 3, 2};int numLength = sizeof(num) / sizeof(num[0]);vector<int> nums(num, num+numLength);cout << a.arrayPairSum(nums) << endl;return 0;
}

[LeetCode]561. Array Partition I (数组分区 1)相关推荐

  1. LeetCode 561 Array Partition I(数组划分)

    翻译 原文 Given an array of 2n integers, your task is to group these integers into n pairs of integer, s ...

  2. LeetCode 561. Array Partition I

    题目: Given an array of 2n integers, your task is to group these integers into n pairs of integer, say ...

  3. leetcode#561 Array Partition I

    Description: Given an array of 2n integers, your task is to group these integers into n pairs of int ...

  4. Leetcode PHP题解--D14 561. Array Partition I

    561. Array Partition I 题目链接 561. Array Partition I 题目分析 本题给了一个数组,要求将数组分为n个只有2个元素的一对. 使得每对数字中最小的数加起来的 ...

  5. Leetcode刷题记录[java]——561 Array Partition I

    一.前言 二.题561 Array Partition I Given an array of 2n integers, your task is to group these integers in ...

  6. 561.Array Partition I--Python

    刚开始学习Python编程,欢迎交流学习! 561.Array Partition I Given an array of 2n integers, your task is to group the ...

  7. 561. 数组拆分 I(javascript)561. Array Partition I

    给定长度为 2n 的整数数组 nums ,你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), -, (an, bn) ,使得从 1 到 n 的 min(ai, bi) 总和 ...

  8. 【刷leetcode,拿Offer-009】561. Array Partition I(贪心,C++)

    题目链接 Given an array of 2n integers, your task is to group these integers into n pairs of integer, sa ...

  9. 561. Array Partition I

    原题 Given an array of 2n integers, your task is to group these integers into n pairs of integer, say ...

最新文章

  1. oracle查看数据库字符编码,oracle 查看、批改字符集编码
  2. 好插件·用户造【CSND超好用插件】·【机械键盘大放送】
  3. 在DataFrame中新建列赋值后全部为NaN(转)
  4. eclipse环境lsp4e --- org.eclipse.lsp4e
  5. python控制台清屏_Python Shell 怎样清屏?
  6. python菜单栏添加子菜单_python添加菜单图文讲解
  7. 兜兜转转,回到原点,Hello Mr.my yesterday
  8. Serializable接口序列化与反序列化
  9. SQL 插入 CLOB类型
  10. Google浏览器调试页面时设置分辨率
  11. 电压跟随器的作用及特点
  12. ThinkPad笔记本如何重装系统
  13. [idea遇到的坑] - Cannot load settings from file[xxxx/xxxx]
  14. php date 加月_php如何使时间增加一个月
  15. etcd之日志和快照管理
  16. KSO-.NETCore中实现跨域的代码以及几种跨域方式
  17. Python bool布尔类型(解析)
  18. linux下删除软连接
  19. 飞思卡尔S12系列(基于MC9S12XET256MAA和/MC9S12XEP100)锁存器资料总结和分享
  20. 仓储+调度,YOGO智能配送站能否改变外卖配送格局?

热门文章

  1. 【C语言作业7】二分法猜数游戏:先由计算机“想”一个1~100之间的数请人猜,用二分法进行猜数,计算机给出反馈,猜大额或是猜小了,直到人猜对为止。计算机记录猜的次数,以此反映猜的水平。
  2. crc16 ccitt的详细标准及其出处
  3. 麻省理工学院计算机好考吗,麻省理工大学世界排名是多少(麻省理工有多难考)...
  4. 下一个十年,区块链将如何厚“基”薄发?
  5. win7系统快捷键大集合 这个最有感觉了~~:~~ 【Win+Tab】:3D切换窗口
  6. lstm预测股票_股票相关性与lstm预测误差
  7. TypeError: xxx() missing 1 required positional argument: 'xxx'
  8. 计算机维修中拆机工具有哪些,秒变专业拆机维修达人,这款工具套装你值得拥有...
  9. IGF1重组人胰岛素样生长因子-1解决方案
  10. Packstack搭建Openstack R版本