当前位置:网站首页>力扣每日一题-第27天-561.数组拆分Ⅰ
力扣每日一题-第27天-561.数组拆分Ⅰ
2022-06-25 17:33:00 【重邮研究森】
2022.6.23今天你刷题了吗?
题目:
给定长度为 2n 的整数数组 nums ,你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从 1 到 n 的 min(ai, bi) 总和最大。
返回该 最大总和 。
分析:
给你一个偶数数组,我们需要把这些数按两两一组分配,然后取每组分配的最小值,再把这些最小值求和使得求和值最大。
思路在于,为了让最后的结果最大,那么分配的时候必须取得的最小值就最大,所以我们可以把最大和次打分配,这样就可以保留一个最大数,同理,最小和次小分配,这样就可以保留一个较小数。因此只需要排序后,隔一个数进行求和。
解析:
class Solution {
public:
int arrayPairSum(vector<int>& nums) {
int sum = 0;
sort(nums.begin(), nums.end());
for (auto i = 0; i < nums.size(); i += 2)
{
sum += nums[i];
}
return sum;
}
};边栏推荐
- Sentinel哨兵机制
- 杰理之adc_get_voltage 函数获取电压值不准【篇】
- Intelligent dialog 01 redis installation
- CVPR小目标检测:上下文和注意力机制提升小目标检测(附论文下载)
- 有关QueryInterface函数
- 杰理之如何给外界输出一个时钟源使用【篇】
- win10安装cuda的操作步骤(不断完美中)
- WARNING: Unsupported upgrade request.
- Jerry's system clock setting is reset or invalid [chapter]
- Website arrangement of super all metal PBR multi-channel mapping materials
猜你喜欢

【UVM实战 ===> Episode_1 】~ MCDF设计更新、AMBA标准接口、UVM验证环境更新

HMS Core机器学习服务实现同声传译,支持中英文互译和多种音色语音播报
![Jerry's system clock setting is reset or invalid [chapter]](/img/c6/ee6b287af7d309f98abda8e11d674c.png)
Jerry's system clock setting is reset or invalid [chapter]

使用DiskGenius拓展系统盘C盘的容量

Utilisation de diskgenius pour augmenter la capacité du disque système C

大学生暑假换机热,ROG 明星产品幻 16 翻转版 / 幻 13 / 幻 X 预约

微博评论的计算架构
![Precautions for the use of Jerry's wake-up mouth [chapter]](/img/01/3bfba9a486eb7fa3c0a888bb3ea2d2.png)
Precautions for the use of Jerry's wake-up mouth [chapter]

Website arrangement of super all metal PBR multi-channel mapping materials

Distinguishing seven kinds of facial expressions by deep separable convolution neural network
随机推荐
Learn Tai Chi Maker - mqtt (III) connect to mqtt server
视频制作素材网站整理
Langage d'assemblage (5) Registre (accès à la mémoire)
相同wifi下,笔记本连接台式机上的虚拟机
Why do we need ankeri's active power filter in frequency converter occasions?
HMS core machine learning service realizes simultaneous interpretation, supports Chinese-English translation and multiple voice broadcast
CVPR small target detection: context and attention mechanisms improve small target detection (attached with paper Download)
Acy100 oil fume concentration online monitor for kitchen oil fume emission in catering industry
container of()函数简介
Is Guotai Junan Securities reliable? Is it legal? Is it safe to open a stock account?
揭秘GES超大规模图计算引擎HyG:图切分
证券公司排名前十手续费最低 办理开户安全吗
Which of the top ten securities companies has the lowest commission? Is it safe to open an account
喜报|海泰方圆通过CMMI-3资质认证,研发能力获国际认可
超全金屬PBR多通道貼圖素材網站整理
【Matlab】数值微积分与方程求解
Huawei cloud gaussdb (for redis) unveiling issue 19: gaussdb (for redis) comprehensive comparison with CODIS
Uncover ges super large scale graph computing engine hyg: Graph Segmentation
杰理之系统时钟设置出现复位或无效问题【篇】
[matlab] curve fitting