[Leetcode] 561. Array Partition I

SAP BOM (物料清单) 详解 BOM(Bill of Materials,物料清单)是制造业和生产管理中的一个核心概念。它列出了生产某种产品所需的所有原材料、零部件和子组件的详细信息。在SAP系统中,BOM管理是为了帮助企业在生产过程中更高效地管理物料需求、库存以及生产成本。BOM不仅包括物料的数量和单位,还详细描述了每个物料的规格、标准和在产品中的位置。 阅读详情

Description:
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:

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:
n is a positive integer, which is in the range of [1, 10000].
All the integers in the array will be in the range of [-10000, 10000].

目标:让这n对整数中的最小值之和达最大。

解法一:(Accepted: beat 63%)
思路:
排序,从0开始,每隔一个数相加。
使n对整数最小值之和达最大即在有序状态下将偶数下标值相加。
(如果是求最小值之和达最小即前n个数相加)

class Solution {
public:
    int arrayPairSum(vector<int>& nums) {
     sort(nums.begin(),nums.end());//vector使用sort函数排序
     int l = nums.size();
     total=0;
     for(int i = 0; i <= l-2 ; i=i+2)
     {
         total+=nums[i];
     }
     return total;
    }
};
  • 利用sort函数排序,sort函数本质是快速排序
  • 实践证明for循环比while循环要快很多

解法二:哈希表(用空间换时间)O(n)O(n) (Accepted: beat 90%)
思路:
#####The key of this solution is to use the element itself as the index.
为了便于理解,假设所有整数范围都在[0,10000]而非[-10000,10000]。创造一个叫hash的数组(vector),它的大小为10000-0+1=10001,这个数组是用于记录nums数组中每个元素的出现次数。
这个方法的关键在于:把nums数组元素本身作为hash数组的下标。
例如,我们遍历nums数组,遇到2的时候,执行hash[2]++;;遇到7的时候,执行hash[7]++;,等等。这也是第一个循环的作用。
对于第二个循环,我们遍历哈希表hash。如果hash[0]==2,说明在nums数组中0有2个,也就是排序之后的nums从两个0开始,如果hash[1]=3,说明这两个0之后有3个1。
flag在这里的作用是记录当前数是否是nums排序后的偶数下标的数。因为前面解法一提到,使n对整数最小值之和达最大即在有序状态下将下标为偶数的值相加。而total是用来计算求和,最后返回的。
需要注意的是,当处理完哈希表某一位置时仍不为0时,如hash[3]=2变成了hash[3]=1,此时还应该继续处理这个位置,因此第二个循环中i不会加一。只有当hash[3]=0时,i才加一,处理下一个位置,也就是else i++;的时候。
而这个problem里,整数范围是[-10000,10000],因此hash的capacity应为10000-(-10000)+1=20001。为了避免下标为负数,在第一个循环中使用hash[nums[i]+10000]++;,而在第二个循环中,要把这加上的10000减回来,故有result+=i-10000;

 class Solution {
public:
    int arrayPairSum(vector<int>& nums) {
        vector<int> hash(20001,0);
        for(int i=0; i<nums.size();i++)
     {
         hash[nums[i]+10000]++;
     }
        int result=0, flag=0;
        for(int i = 0;i<20001;)
        {
            if((hash[i]>0)&&(flag==0))
            {
                result+=i-10000;
                hash[i]--;
                flag = 1;
            }
            else if((hash[i]>0)&&(flag==1))
            {
                hash[i]--;
                flag = 0;
            }
            else
                i++;
        }
        return result;
    }
};
  • 这个方法在已知原数组nums元素的范围以及这个范围不是很大的情况下(如nums数组的元素都是[INT_MIN,INT_MAX]范围内的整数)是有用的,否则空间复杂度会很高。
Happy coding!
YOLOv11 改进 - 注意力机制 | GC Block(GlobalContext Block)全局上下文块:三重变换捕获全局依赖,提升复杂场景鲁棒性 本文介绍了全局上下文块(GC Block)及其在YOLOv11中的集成应用。GC Block是GCNet的核心组件,结合了NLNet和SENet的优势,通过上下文建模、特征变换和特征融合三个模块,高效捕获特征图中的全局依赖关系,在提高模型性能的同时降低计算成本。我们将GC Block引入YOLOv11,在检测头部分的不同尺度特征图上应用该模块。实验表明,改进后的YOLOv11在目标检测任务中表现良好,展现了GC Block在实际应用中的价值。 阅读详情

相关推荐

Audio2Face进阶玩法:结合LiveLink在UE5中实现MetaHuman全身动画+语音驱动(保姆级配置指南)

本文提供了一份详尽的保姆级教程,指导如何将NVIDIA Audio2Face与Unreal Engine 5的LiveLink功能结合,实现驱动MetaHuman数字人面部表情的同时,无缝融合身体动画。文章详细讲解了从环境准备、插件部署、基础面部驱动测试,到动画蓝图高级配置与多数据流融合的全流程,并分享了性能优化与问题排查的实战经验,旨在帮助创作者打造表情与肢体动作协调统一的生动数字人。

q9w8e7r6t5的博客 196

Leetcode 561: Array Partition I

题目传送门: Leetcode 561. Array Partition I题目: 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,

丫丫狸笫的专栏 615

xsens IMU配置

1.安装驱动 sudo apt-get update sudo apt-get install ros-kinetic-xsens-driver 2.配置IMU 设置输出四元数,加速度、角速度、地磁 cd /opt/ros/kinetic/lib/xsens_driver python mtdevice.py --configure "oq,aa,wr,mf" 3.启动IMU roslaunch xsens_driver xsens_driver.launch 转(https://www.cnblo

qq_41585680的博客 2324

[LeetCode] 561.Array Partition I

[LeetCode] 561.Array Partition I 题目描述 解题思路 实验代码 题目描述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 m

user_name_is_so_long的博客 381

深度学习十大算法之Word2Vec

Word2Vec的提出,标志着词嵌入技术的一次重大进步。Word2Vec是一种高效的词嵌入模型,由Google的研究团队于2013年开发。它使用一种称为神经网络的机器学习方法,通过学习大量文本数据,生成能够表达词语间丰富语义关系的词向量。Word2Vec的核心优势在于它能够捕捉到词语之间的细微关系。例如,在Word2Vec模型中,可以通过计算向量之间的距离来估计词语之间的相似度。这不仅能帮助理解语言的复杂性,还可以应用于各种NLP任务,如机器翻译和搜索引擎优化。

第欧根尼的酒桶的博客 1864

Linux下科大讯飞离线语音识别SDK从下载到实战(附常见问题解决)

本文详细介绍了在Linux系统下,如何从科大讯飞开放平台获取离线语音识别SDK,完成环境准备、编译部署到实战开发的完整流程。文章重点解析了SDK目录结构、语法词典定制、示例代码修改,并针对开发中常见的“词典加载失败”、音频格式、交叉编译及性能优化等问题提供了深度解决方案,助力嵌入式与IoT开发者快速实现稳定可靠的离线命令识别功能。

honey的博客 1239

Leetcode题解-561. Array Partition I

Leetcode题解-561. Array Partition IGiven 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

hzw2945的博客 284

2026年9月20日 2026年CSP-S初赛真题整理

这是本次考试最难的部分,涉及Gray Code(格雷码)+ 状态压缩 + 数学推导难点解决方案关键代码问题转化将得分差转化为m−2xim - 2x[i]m−2xi高效枚举Gray Code相邻状态O(1)转移奇偶判断利用奇偶性统一条件。

weixin_51184047的博客 619

[LeetCode]561. Array Partition I

[LeetCode]561. Array Partition I题目描述思路实质上就是对数组排序之后,计算下标为0,2,4,6…的数的和代码#include <iostream> #include <vector> #include <algorithm>using namespace std;class Solution { public: int arrayPairSum(vector<

Lcharon的博客 974

KV STUDIO的安装与实践(一)

KV STUDIO是一种软件开发工具,用于编写、调试和管理基于Key-Value(键-值)存储的应用程序。KV STUDIO提供了一个集成开发环境(IDE),使开发人员可以方便地创建和管理键-值数据库,并编写逻辑和业务规则来操作和查询这些数据。它支持多种编程语言和平台,并提供了丰富的API和工具来简化开发过程。使用KV STUDIO,开发人员可以快速构建高效、可靠的键-值存储系统,并实现各种应用程序,如缓存、会话管理、配置存储等。本次使用plc进行案例讲解,结合小编的亲身经历,仅供参考!

weixin_47514459的博客 6346

YOLOv11 改进 - 特征融合 | 替换 C3k2 涨点:GELAN 融合 CSP+ELAN 提精度

本文介绍了可编程梯度信息(PGI)和广义高效层聚合网络(GELAN)与 YOLOv11 的结合,以解决深度网络数据传输时的信息丢失问题。PGI 能为目标任务提供完整输入信息,计算目标函数以更新网络权重。GELAN 作为轻量级网络架构,由 SPPELAN、ELAN1、RepNCSPELAN4 等模块组成。我们将 GELAN 代码集成进 YOLOv11, 实验脚本显示,结合 GELAN 的 YOLOv11 在 MS COCO 数据集目标检测任务中或有良好表现。

魔改工程师的博客 1242

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

561. Array Partition IGiven 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

whl_program的博客 6626

【AI就业干货12】机器学习八股文速通:从损失函数到 XGBoost,一篇带走全部面试点

本文总结了机器学习面试的核心知识点,涵盖监督与非监督学习、线性模型、正则化、决策树、KNN/K-means、SVM、集成学习、梯度下降、评估指标、特征工程、降维、概率模型等内容。重点包括:损失函数选择(MSE/交叉熵)、正则化作用(L1稀疏/L2平滑)、决策树划分准则、集成学习方法(Bagging降方差/Boosting降偏差)、XGBoost优化等。提供了30秒速记口诀和XGBoost代码示例,帮助快速掌握面试要点。全文采用公式+代码+口诀的形式,系统梳理了机器学习从理论到实践的常见考点。

m0_51738372的博客 731

【Leetcode】 561. 数组分割 I(Array Partition I)

Leetcode - 561 Array Partition I (Easy) 题目描述:数组中有 2n 个元素,将这 2n 个元素两两组成一对 (ai, bi),使得构成的 n 对 min(ai, bi) 的和最大。 Input: [1,4,3,2] Output: 4 Explanation: n is 2, and the maximum sum of pairs is 4 = min(1...

厉兵秣码 377

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

【LeetCode】561.Array Partition I解题报告

【LeetCode】561.Array Partition I解题报告tags: Array 题目地址:https://leetcode.com/problems/array-partition-i/#/description 题目描述:   Given an array of 2n integers, your task is to group these integers into n p

郝春雨的博客 547

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

LeetCode 561. Array Partition I

LeetCode 561. Array Partition I

Jh_SYSU的博客 355

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

[leetcode]561. Array Partition I

[leetcode]561. Array Partition I Analysis 中午吃啥?—— [明天要回家啦,嘻嘻~] 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...

weixin_32135877的博客 117

【Leetcode】561. Array Partition I

题目地址: https://leetcode.com/problems/array-partition-i/ 题目大意是,给定2n2n2n个整数,将它们两两分组分成nnn个组,并对每组较小的数求和,问如何分组使得,这样的和最小,求出数值。 题目并不难,直觉上只需要从小到大排序,然后选第0,2,4,...,2n−20,2,4,...,2n-20,2,4,...,2n−2个数求和就行了。代码如下: i...

数学、算法爱好者的博客 255

Leetcode算法学习日志-561 Array Partition I

Leetcode 561 Array Partition I 题目原文 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, b

Zarlove的博客 353

【LeetCode】561. Array Partition I

Python版本:Python3.6.2561. Array Partition IGiven 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,

sunflower_kris的博客 249

leetcode题集:561. Array Partition I

第二天:561.Array Partition I 一道水题,给定2n个数的数组,求n对二元组的最小值之和的最大值,分析发现,只需对数组进行一个从小到大的排序,取n=0,2,4,6....2n-2即是所求之和的最大值。完整代码如下: class Solution { public: int arrayPairSum(vector<int>& nums) { ...

wx_14678的博客 222

【LeetCode】 561. Array Partition I

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

韩旭051的博客 521

[LeetCode] 561. Array Partition I

561. Array Partition I (数组分区 I) 561. Array Partition I (数组分区 I) 1. 题目翻译 2. 解题方法 3. 代码 1. 题目翻译 给定一个大小为2n的整数数组,你需要将它分成n组,每组两个数字(a1, b1), (a2, b2), …, (an, bn)。分组的依据是,取每组最小的数共n个,使他们的和尽可能...

LLLZH 388

LeetCode | 561. Array Partition I

. 题目 Given an integer array nums of 2n integers, group these integers into n pairs (a1, b1), (a2, b2), …, (an, bn) such that the sum of min(ai, bi) for all i is maximized. Return the maximized sum. Example 1: Input: nums = [1,4,3,2] Output: 4 Explanation:

iLOVEJohnny的博客 724

LeetCode 561.Array Partition I

LeetCode 561.Array Partition IDescription: 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,

My Blog 273

leetcode 561 Array Partition I

题目来源:LeetCode 561 简单题目分析: 给你2n个数,让你配成n对,并求∑min(ai,bi)的最大值。 思路: 先把这2n个数排好序,然后取下标为偶数的项即可,下面给出证明: 1.定义ans=min(a1,b1)+min(a2,b2)+...+min(an,bn)(所要求的值) 2.不妨假设bi≥ai,则ans=a1+a2+a3+...+an 3.定义Sn=a1+b1+

Polanwind 248
上一篇: [Leetcode] 1. Two Sum
下一篇: [Leetcode] 771. Jewels and Stones
Peggy_Chang
博客等级 码龄11年 21粉丝 44原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值