当前位置:网站首页>力扣每日一题-第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;
}
};边栏推荐
- 用户调度问题
- Precautions for using Jerry's timer [chapter]
- 证券公司排名前十手续费最低 办理开户安全吗
- 智能对话01-redis的安装
- Why do we need ankeri's active power filter in frequency converter occasions?
- golang sort slice int
- 汇编语言(5)寄存器(内存访问)
- Jericho's method of obtaining reset source and wakeup IO port [chapter]
- 相同wifi下,笔记本连接台式机上的虚拟机
- 数学建模——线性规划
猜你喜欢

Learn Tai Chi Maker - mqtt (III) connect to mqtt server

【编译原理】词法分析

Use diskgenius to expand the capacity of system disk C

win10安装cuda的操作步骤(不断完美中)
![[compilation principle] overview](/img/af/246750beb72842e83a19e42270c09e.png)
[compilation principle] overview

深入理解ELF文件

数学建模——整数规划

Mathematical modeling -- integer programming

How does LSF see whether the job reserved slot is reasonable?

杰理之增加加密文件播放功能【篇】
随机推荐
Getting started with kotlin (20) several common dialog boxes
Precautions for use of Jerry's SPI slave [chapter]
How does LSF see whether the job reserved slot is reasonable?
DDD concept is complex and difficult to understand. How to design code implementation model in practice?
杰理之如何给外界输出一个时钟源使用【篇】
超全金屬PBR多通道貼圖素材網站整理
Idea global search for Chinese characters [easy to understand]
Kotlin入门(20)几种常见的对话框
VSCode 自动生成头文件的#ifndef #define #endif
Introduction to the container of() function
MySQL mysql-8.0.19-winx64 installation and Navicat connection
CVPR small target detection: context and attention mechanisms improve small target detection (attached with paper Download)
SDN system method | 10 The future of SDN
Huawei cloud gaussdb (for redis) unveiling issue 19: gaussdb (for redis) comprehensive comparison with CODIS
Win10开启热点共享后断网怎么解决?
Jericho's method of obtaining reset source and wakeup IO port [chapter]
TLV解码
CVPR小目标检测:上下文和注意力机制提升小目标检测(附论文下载)
CGI connects to database through ODBC
Which of the top ten securities companies has the lowest commission? Is it safe to open an account