当前位置:网站首页>力扣每日一题-第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;
}
};边栏推荐
- Can I open an account? Is it safe to open an account
- golang sort slice int
- How high does UART baud rate require for clock accuracy?
- Distinguishing seven kinds of facial expressions by deep separable convolution neural network
- Win10开启热点共享后断网怎么解决?
- [efficiency] another note artifact is open source!
- golang list to string
- 华为云GaussDB(for Redis)揭秘第19期:GaussDB(for Redis)全面对比Codis
- 数据挖掘之时间序列分析[通俗易懂]
- UART波特率对时钟精度的要求有多高?
猜你喜欢

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

Use diskgenius to expand the capacity of system disk C

Automatic submission for the next education day

WARNING: Unsupported upgrade request.

【Matlab】曲线拟合

什么是算子?

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

SDN system method | 10 The future of SDN

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

使用DiskGenius拓展系統盤C盤的容量
随机推荐
数学建模——非线性规划
Jerry's ADC_ get_ Incorrect voltage value obtained by voltage function [chapter]
[matlab] data interpolation
mysql mysql-8.0.19-winx64 安装与navicat连接
数学建模——线性规划
【Matlab】数值微积分与方程求解
How does LSF see whether the job reserved slot is reasonable?
Which of the top ten securities companies has the lowest commission? Is it safe to open an account
Accumulation of some common knowledge points
配电室环境的分布式远程管理
Learning Tai Chi makers - mqtt (I) what is mqtt
ES6 knowledge points
Golang sort slice int
Time series analysis of data mining [easy to understand]
Mobx learning path - powerful "state management tool"
揭秘GES超大规模图计算引擎HyG:图切分
C语言中%含义
深入理解ELF文件
The second round of Yunnan Cyberspace Security competition in May 2021
汇编语言(6)使用JCC指令构造分支与循环