题目描述:

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:

  1. n is a positive integer, which is in the range of [1, 10000].
  2. All the integers in the array will be in the range of [-10000, 10000].

要完成的函数:

int arrayPairSum(vector<int>& nums)

说明:

1、给定一个vector,里面包含2n个元素,要把这些元素组成n个对子,比如[1,3,2,4],可以组成[1,2],[3,4]这样的对子。

求每个对子的最小值,然后把最小值加起来求和,要让和最大,应该怎样组对子?能够输出最大的和是多少?

2、我们思考一下,比如[1,3,2,4]这样的vector,4这个最大值能不能作为和的一部分,明显不能,无论4跟谁搭配,都不能输出。

那3这个第二大的数呢?3只能跟4在一起的时候,才能输出。

那2呢?假如2跟4搭配,3跟1搭配,那么的确可以输出2,但是3就输出不了了。为了一个2,失去一个3,明显不值得。

所以最理想的搭配是[3,4]输出3,[1,2]输出1,我们要尽可能让大的数体现它的价值。

3、思路很清晰,代码如下:

    int arrayPairSum(vector<int>& nums) {sort(nums.begin(),nums.end());//升序排列int s1=nums.size(),sum=0;for(int i=0;i<s1;i+=2)sum+=nums[i];return sum;}

代码简洁,实测76ms,beats 78.50% of cpp submissions。

转载于:https://www.cnblogs.com/chenjx85/p/8985158.html

leetcode-561-Array Partition I相关推荐

  1. [LeetCode]561. Array Partition I (数组分区 1)

    561. Array Partition I Given an array of 2n integers, your task is to group these integers into n pa ...

  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(数组划分)

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

  4. leetcode#561 Array Partition I

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

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

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

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

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

  7. 561.Array Partition I--Python

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

  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 ...

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

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

最新文章

  1. MindSpore整体架构介绍
  2. 贝叶斯思想概述:从贝叶斯定理到贝叶斯网络
  3. [MetalKit]34-Working-with-memory-in-Metal内存管理
  4. 阿里云帮助江苏省财政厅力推统一公共支付平台
  5. 无刷新上传Excel后利用JQuery AJAX 显示进度条的实现方式
  6. 2017第四届中国国际大数据大会成功召开
  7. 请问delphix下双缓冲是自动开启的吗?
  8. php serialize error at offset,unserialize(): Error at offset出现的原因分析以及解决方法
  9. 修改navigationbar右侧按钮离屏幕边缘位置
  10. 最近在做中文的全文检索(中文搜索引擎)
  11. Echarts官方文档!
  12. CBCGPToolBarImages和CImageList创建与使用
  13. kafka2.0-幂等发送(the idempotent producer)_09
  14. 图像处理中的用于消除高斯噪声的加法运算
  15. 测试脂肪的软件叫,智能健康监测app
  16. 毕业了,让我们唱起这十首歌!
  17. 【时间与空间】惯性系与地固系之间的转换
  18. 易经与计算机科学,人与计算机的思维模式区别,以及计算机的易经八卦运算
  19. 【并发编程三】C++进程通信——管道(pipe)
  20. ipad1电池激活(长时间不用,电量过低,无法开机和充电)

热门文章

  1. 【shell】shell脚本实战-awk使用案例
  2. oracle12c查看sid,[20170527]12c连接插件数据库使用sid
  3. DaVinci Resolve Studio 17 Mac 达芬奇剪辑调色软件
  4. VR全景看考场,线上“踩点”不迷路
  5. OpenCV——边缘检测原理
  6. 使用KVM服务创建虚拟机
  7. 【linux】循序渐进学运维-cp
  8. Python爬虫笔记——字符串的常用方法
  9. 机器学习概念总结笔记(一)——机器学习算法分类、最小二乘回归、岭回归、LASSO回归
  10. python爬取HTML中属性的值的实例(loading...)