[LeetCode] 561. Array Partition I

解密电商大促背后的智慧:LSTM与ARIMA模型的双剑合璧 举个例子,如果某东发现某款新推出的电子产品在促销前夕搜索量突然暴涨,虽然这个产品没有过去的销售记录,但LSTM模型可以结合用户对类似产品的行为模式、实时的市场反馈,迅速捕捉到这些变化,并预测这款产品在大促期间的需求会急剧上升。想象一下,如果预测不准确,备货不足,商品很快就卖光了,大量用户可能在促销期间买不到想要的商品,这不仅让用户失望,还会影响某东的品牌形象。这种情况就像我们看一部很长的电视剧,看得越久,前面的情节可能就越来越模糊,到最后有些关键的情节细节都忘了,影响了对整个剧情的理解。 阅读详情

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;
    }
};
语义分割(Semantic Segmentation)近年发展综述 目标:为图像中每个像素分配一个语义类别标签(如“道路”、“行人”、“天空”),不区分同类实例。关键挑战精确边界恢复(高空间分辨率)全局上下文理解(大感受野)计算效率与精度平衡小样本/弱监督泛化语义分割的十年演进,是一条从专用→通用、封闭→开放、慢速→实时、像素→语义2015:FCN/U-Net 奠基;2018:DeepLabv3+/PSPNet 实现高精度;2020:HRNet+OCR 登顶竞赛;2021:SegFormer 开启 Transformer 新纪元;2023+ 阅读详情

相关推荐

具身智能数据采集的工程化指南:从时间同步到质量校验

具身智能的模型训练离不开高质量的多模态轨迹数据,而数据采集并非简单的传感器记录。真实场景中,相机图像、机械臂关节状态与力觉信息必须在时间轴上精确对齐,标定的漂移和格式混乱会让看似正常的数据无法用于训练。时间同步与标定一致性,是决定数据质量的核心技术环节。在机器人抓取、操作等工程应用中,一套包含多传感器同步、轻量记录脚本、自动化质量校验与版本管理的数据采集流程,能大幅降低数据生产的失败率。本文从工程视角拆解具身数据采集中的典型问题,并给出可落地的流程设计与校验方法,帮助团队构建可持续迭代的高质量数据集。

weixin_30603633的博客 510

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

电磁炉线圈加热仿真全流程:用Ansys Maxwell分析涡流效应与热损耗

本文详细介绍了使用Ansys Maxwell进行电磁炉线圈加热仿真的全流程。通过构建2D轴对称涡流场模型,深入分析了高频下的涡流效应、趋肤效应与热损耗分布,为优化线圈设计、提升加热效率与均匀性提供了量化依据和实用指南。

sss66的博客 963

Leetcode 561. Array Partition I

Leetcode 561. Array Partition I

zero的博客 634

分布式推理服务(策略:数据并行、张量并行、流水线并行、专家并行)技术分析

摘要:分布式推理突破单GPU内存和算力限制,支持大模型部署。四种核心策略包括: 数据并行:复制模型拆分数据,提升吞吐但内存效率低; 流水线并行:模型分阶段执行,支持超大模型但存在计算气泡; 张量并行:拆分层内矩阵运算,延迟低但通信开销大; 专家并行:动态激活MoE模型专家,计算高效但路由复杂。实际应用中常组合策略(如3D并行),需权衡模型规模、硬件条件与性能目标。

技术引领业务创新 940

开箱即用的Platform IO开发平台(包括Arduino,ESP8266,ESP32,STM32)

开箱即用的 Platform IO 离线开发环境,包含(ESP32,ESP32S2,ESP8266,Arduino,STM32)

qq_19703307的博客 755

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的专栏 328

【电机驱动与运动控制】期末重点问题集合

问题1 梯形曲线和S形曲线各自的特点和区别,比较梯形和S形曲线位置、速度、加速度、加加速度的区别。 问题2 总惯量的计算包含哪几个部分? 哪几个部分需要进行折算? 在齿轮箱的输出侧(2号轴)总惯量需要折算到齿轮箱的输入侧(1号轴) 为什么需要折算? 齿轮箱比的存在使2号轴的转速和电机的转速不同 问题3 指出每个部分的名称。 每个部分的功能。 主磁极的励磁绕组:绝大部分直流电机都是由励磁绕组通以直流电建立主磁场; - 电枢铁心:在两个主磁极内表面之间,由硅钢片叠成的圆

【人工智障 | 计算鸡视觉 | 机器孩】 5703

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

笔试题-2023-紫光展锐-数字芯片设计【纯净题目版】

笔试题-2023-紫光展锐-数字芯片设计【纯净题目版】

w0shishabi的博客 2923

TMC2240 芯片数据手册解读|第十三篇 StallGuard4负载测量(StallGuard4 Load Measurement)

摘要: TMC2240的StallGuard4技术专为StealthChop2静音模式设计,通过SG4_RESULT实时测量电机负载,实现堵转检测、无传感器回零和CoolStep节能。其核心原理基于负载角检测,通过SG4_THRS阈值调节灵敏度,并支持四通道独立测量(SG4_IND_0~3)优化相位平衡。独特功能包括无抖动模式切换补偿(sg_angle_offset),解决StealthChop2与SpreadCycle切换时的电流突变问题。调优时需结合SG4_THRS设定与DIAG验证,确保堵转精准触发。

BackCatK Chen的博客 302

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

Yocto开发讲解系列 - 总目录

Yocto开发讲解系列总纲Yocto开发专栏前言

fulinux的博客 3万+

[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

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

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

IEM模型,地表粗糙度模拟

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

[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

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

[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

【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

【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

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

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

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、付费专栏及课程。

余额充值