当前位置:网站首页>力扣每日一题-第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;
}
};边栏推荐
- What is public chain development? What are the public chain development projects?
- RuntimeError: Trying to backward through the graph a second time (or directly access saved variable
- 服务器四通道内存性能提升,四通道内存性能怎么样
- 有关均衡----简易版瓶颈模型
- A development of student management system based on PHP
- 大学生暑假换机热,ROG 明星产品幻 16 翻转版 / 幻 13 / 幻 X 预约
- 宝藏又小众的国画3d材质贴图素材网站分享
- Precautions for the use of Jerry's wake-up mouth [chapter]
- Website arrangement of super all metal PBR multi-channel mapping materials
- 杰理之增加加密文件播放功能【篇】
猜你喜欢

杰理之获取复位源和唤醒的 IO 口的方法【篇】

HMS core machine learning service realizes simultaneous interpretation, supports Chinese-English translation and multiple voice broadcast

Assembly language (5) register (memory access)

什么是算子?

Distinguishing seven kinds of facial expressions by deep separable convolution neural network

杰理之系统时钟设置出现复位或无效问题【篇】

智能对话01-redis的安装

汇编语言(5)寄存器(内存访问)

SDN system method | 10 The future of SDN

Use diskgenius to expand the capacity of system disk C
随机推荐
汇编语言(5)寄存器(内存访问)
JS disable browser default events
HMS Core机器学习服务实现同声传译,支持中英文互译和多种音色语音播报
[matlab] data interpolation
服务器四通道内存性能提升,四通道内存性能怎么样
数学建模——整数规划
证券公司排名前十手续费最低 办理开户安全吗
Utilisation de diskgenius pour augmenter la capacité du disque système C
汇编语言(6)使用JCC指令构造分支与循环
mvdr波束 matlab,mvdr波束形成matlab[通俗易懂]
杰理之获取复位源和唤醒的 IO 口的方法【篇】
卷积操作的本质特性+TextCNN文本分类
[UVM practice== > episode_2] ~ VIP, VIP development, VIP release
How about qiniu's Zhangle TenPay? Is it safe
[compilation principle] overview
Interrupt operation: abortcontroller learning notes
启牛的涨乐财付通如何?安全靠谱吗
jupyter的使用
杰理之定时器使用注意事项【篇】
Use diskgenius to expand the capacity of system disk C