[LeetCode] 561. Array Partition I

DeepSeek-OCR 本地部署使用教程(附完整环境配置与推理示例) DeepSeek-OCR是由DeepSeek AI推出的开源OCR模型,支持高精度文档识别与结构化输出(如Markdown转换)。本文提供了详细的本地部署指南,包括系统要求(推荐Linux+A100 GPU)、环境配置(Python 3.12.9+PyTorch 2.6.0+CUDA 11.8)、模型下载及两种推理方式:高性能的vLLM(适合批量处理)和灵活的Transformers(适合调试)。支持多种分辨率模式和提示词模板,适用于合同识别、PDF转换等场景。常见问题包括显存优化和性能调优建议。 阅读详情

561. Array Partition I (数组分区 I)



1. 题目翻译

给定一个大小为2n的整数数组,你需要将它分成n组,每组两个数字(a1, b1), (a2, b2), …, (an, bn)。分组的依据是,取每组最小的数共n个,使他们的和尽可能大。

例子:

输入: [1,4,3,2]
输出: 4
解释: n为2, 最大和为4 = min(1, 2) + min(3, 4).

备注:

  1. n是正整数,范围为 [1, 10000].
  2. 所有数组中的整数的范围为 [-10000, 10000].

2. 解题方法

1,2,3,4 在这4个数中,4是不可能得到的,因为没有别的数比它更大。想要得到3必须将3与4分在一组。剩下的1和2只能取1。这道题其实就是取最大数与次大数,然后将这个数从原数组去掉,继续去最大数与次大数。最后将所有次大数加在一起。所以可以将原数组排序,然后取奇数位的所有值的和。

3. 代码

//Runtime: 75ms
class Solution {
public:
    int arrayPairSum(vector<int>& nums) {
        sort(nums.begin(),nums.end());
        int res = 0;
        for(int i = 0;i<nums.size();i+=2)
            res += nums[i];
        return res;
    }
};

如果不想使用自带的sort方法,可以使用下面自己写的快速排序的代码。

//Runtime: 78ms
class Solution {
private:
    int partition(vector<int>& nums, int left, int right){
        int i =left+1,j=i;
        for(i;j<=right;j++){
            if(nums[j]<=nums[left]){
                int temp = nums[j];
                nums[j] = nums[i];
                nums[i] = temp;
                i++;
            }
        }

        int temp = nums[i-1];
        nums[i-1] = nums[left];
        nums[left] = temp;
        return i-1;
    }

    void quicksort(vector<int>& nums, int left, int right){
        if(left<right){
            int index = partition(nums,left,right);
            quicksort(nums,left,index-1);
            quicksort(nums,index+1,right);
        }
    }
public:
    int arrayPairSum(vector<int>& nums) {
        quicksort(nums,0,nums.size()-1);
        int res = 0;
        for(int i = 0;i<nums.size();i+=2)
            res += nums[i];
        return res;
    }
};
【ROS2】RViz2加载URDF模型文件 1)运行RViz2rviz22)添加组件:RobotModel3)选择通过文件添加4)选择URDF文件,此时会报错,需要修改Fixed Frame为map即可 阅读详情

相关推荐

centos7搭建LNMP环境-编译安装&yum安装-超详细

文章目录1. 首先了解编译安装和yum安装的区别。2. yum安装准备工作---切换阿里云yum源3. 编译安装lnmp4.yum安装lnmp5. lnmp一键安装6. 一些报错7. 笔记8. 参考文章 1. 首先了解编译安装和yum安装的区别。 windows下安装centos虚拟机,之前写过。传送门~~ 编译安装: 可以按需安装,想安在哪个目录下就安在哪个目录下。 可以设定参数 可安装自己想要的版本 yum安装: 安装的方便快捷 不需要考虑包的依赖 可以直接使用service nginx st

最美不过下雨天 9361

Leetcode-561. Array Partition I (C Language)

The problem: 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...

Shirleylinyuer的专栏 329

Unity开发HoloLens应用:从打包到部署的完整指南

本文为Unity开发者提供了将应用部署到HoloLens设备的完整实战指南。详细讲解了从环境准备、Unity项目配置、生成Visual Studio工程,到最终构建APPX安装包并通过设备门户或Visual Studio进行部署的全流程,帮助开发者高效完成混合现实应用的打包与安装。

red88的博客 1026

Leetcode 561. Array Partition I

Leetcode 561. Array Partition I

zero的博客 635

保姆级教程:用CST 2023一步步复现一个1-8GHz Vivaldi天线(含参数优化与结果分析)

本文提供了一份详细的CST 2023教程,指导读者如何从零开始设计并优化1-8GHz Vivaldi天线。内容涵盖工程设置、参数化建模、仿真加速技巧以及性能优化策略,特别适合需要实现宽带天线设计的工程师和研究人员。通过实战案例,帮助用户掌握Vivaldi天线的核心设计要点和CST高级功能应用。

weixin_30800987的博客 628

三菱FX5U PLC与CCLINK模块通信配置全流程(附接线图与参数详解)

本文详细解析了三菱FX5U PLC与CCLINK模块的通信配置全流程,包括硬件接线规范、参数设置及软件编程方法。通过实战指南帮助工程师快速掌握FX5U与CCLINK模块的高效通信技术,提升工业自动化控制系统的稳定性和可靠性。

weixin_27014595的博客 457

leetcode 561. 数组拆分 I(Array Partition I)

目录 题目描述: 示例 1: 算法: 题目描述: 给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从1 到 n 的 min(ai, bi) 总和最大。 示例 1: 输入: [1,4,3,2]...

weixin_30951389的博客 171

VL53L0X测距不准?手把手教你做RefSPAD校准与环境补偿(附Arduino/STM32代码)

本文详细解析了VL53L0X激光测距传感器精度不足的原因,并提供了RefSPAD校准与环境补偿的完整解决方案。通过Arduino和STM32代码示例,手把手教你实现毫米级测距稳定性,适用于机器人避障、工业检测等场景。

weixin_28744423的博客 264

[leetcode]-561. Array Partition I(C语言)

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

shen_zhu的博客 332

Open-AutoGLM 实战:手把手教你用 AI 操作手机「喂饭教程」

Open-AutoGLM 是一个由智谱AI开源的手机操作智能体框架,由手机端智能助手框架Phone Agent和9B大小的模型AutoGLM-Phone-9B组成,借助ADB自动执行点击、滑动、输入等操作——用户只需用自然语言描述任务,系统便能自动规划并完成整个操作流程。

blues_C的博客 1285

图数据库Neo4j面试内容整理-Neo4j的性能

在执行复杂关系查询时,Neo4j 通过图结构的高效存储和查询机制,能够比传统的关系型数据库提供更快的性能,尤其是在涉及深层关系的查询时。通过合理的配置、优化查询和使用合适的硬件,Neo4j 可以处理大规模的数据集和高并发的查询请求,满足企业级应用的需求。Neo4j 在处理图数据时,通过优化图的存储、查询和遍历等方面,提供了高效的性能,特别适合用于需要处理复杂关系和多层次连接的应用场景,如社交网络、推荐系统、知识图谱等。:Neo4j 会根据查询的结构和数据的分布自动选择最优的查询计划。

不务正业的猿 591

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

29、几何形态测量学在蝴蝶翅膀研究中的应用

本博客介绍了几何形态测量学在花斑木蛱蝶(Pararge aegeria)翅膀研究中的应用,探讨了环境因素对蝴蝶翅膀大小和形状的影响。通过选择合适的地标并采用Procrustes分析方法,研究揭示了不同景观类型下翅膀形状的差异及其潜在的适应性意义。文章还讨论了发育可塑性在飞行形态变异中的作用,并比较了几何形态测量学与其他传统方法的优势。最后提出了未来研究方向,包括多因素综合分析、发育机制探索及长期生态监测,为理解蝴蝶的适应性进化提供了重要线索。

ff678的博客 118

[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

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

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的博客 285

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

[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

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

[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的博客 6628

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

厉兵秣码 378

【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

郝春雨的博客 549

LeetCode 561. Array Partition I

LeetCode 561. Array Partition I

Jh_SYSU的博客 355

[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的博客 355

【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的博客 253

[Leetcode] 561. Array Partition I

link: https://leetcode.com/problems/array-partition-i/ Solution: Sort以后取偶数位数字之和 Sort以后取相邻会使得min(a, b)之和最小的原因是: 假设a > b,则对于最后的结果,取(a, b)造成的损失是a-b 要使得损失最小,则a和b要离得最近,即相邻 class Solution { public in...

Renoler的博客 166

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] 728. Self Dividing Numbers
下一篇: [LeetCode] 821. Shortest Distance to a Character
LLLZHLIU
博客等级 码龄10年 19粉丝 43原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值