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

Vitis HLS 学习笔记--变量存储(指令:ARRAY_PARTITION) ARRAY_PARTITION 指令中非常重要,它用于优化数组的存储和访问。该指令可以将一个大数组分割成多个小数组,以提高并行处理能力和减少访问延迟。 阅读详情

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

题目大意:
给定一个长度为2n(偶数)的数组,分成n个小组,返回每组中较小值的和sum,使sum尽量大
Example 1:

Input: [1,4,3,2]
Output: 4
Explanation: n is 2, and the maximum sum of pairs is 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].

思路:

  • 先排序,将相邻两个数分为一组,每组较小数都在左边,求和即可

算法分析:
查看英文版请点击上方

  • 假设对于每一对i,bi >= ai。
  • 定义Sm = min(a1,b1)+ min(a2,b2)+ … + min(an,bn)。最大的Sm是这个问题的答案。由于bi >= ai,Sm = a1 + a2 + … + an。
  • 定义Sa = a1 + b1 + a2 + b2 + … + an + bn。对于给定的输入,Sa是常数。
  • 定义di = | ai - bi |。由于bi >= ai,di = bi-ai, bi = ai+di。
  • 定义Sd = d1 + d2 + … + dn。
  • 所以Sa = a1 + (a1 + d1) + a2 + (a2 + d2) + … + an + (an + di) = 2Sm + Sd , 所以Sm =(Sa-Sd)/ 2。为得到最大Sm,给定Sa为常数,需要使Sd尽可能小。
  • 所以这个问题就是在数组中找到使di(ai和bi之间的距离)的和尽可能小的对。显然,相邻元素的这些距离之和是最小的。

代码如下:

// https://leetcode.com/problems/array-partition-i/#/description原题
//https://discuss.leetcode.com/topic/87206/java-solution-sorting-and-rough-proof-of-algorithm
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
    int arrayPairSum(vector<int>& nums) {
        int res = 0;
        sort(nums.begin(), nums.end());
        for(int i=0; i<nums.size(); i+=2){
            res += nums[i];
        }
        return res;
    }
};
int main()
{
    Solution a;
    int num[4] = {1, 4, 3, 2};
    int numLength = sizeof(num) / sizeof(num[0]);
    vector<int> nums(num, num+numLength);
    cout << a.arrayPairSum(nums) << endl;
    return 0;
}
LeetCode-561 Array-Partition-I 文章目录0.简介1.题目描述2.解题思路3.参考代码4.复杂度分析参考内容 0.简介 记录LeetCode刷题过程,每篇分为四部分,题目描述、解题思路、参考代码、复杂度分析 如有疑问欢迎讨论,GitHub地址:https://github.com/LoneRanger0504/LeetCode 1.题目描述 给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (... 阅读详情

相关推荐

KEPServerEX实战:Modbus TCP与RTU双协议配置详解

本文详细解析了如何使用KEPServerEX软件同时配置与管理Modbus TCP和RTU混合网络。通过实战步骤,指导用户建立双协议通道、配置设备与标签,并分享核心的地址映射规则与通讯故障排查技巧,帮助工业自动化工程师高效集成多协议设备,简化数据采集架构。

weixin_29186249的博客 406

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 possible. Example 1: Input: [1,4,3,2] Output: 4 Explanation: n

weixin_43846020的博客 171

逆向实战:手把手教你分析Google DroidGuard虚拟机的加密与反调试(附Python脚本)

本文深入解析Google DroidGuard虚拟机的加密机制与反调试策略,提供从环境配置到逆向分析的完整实战指南。通过IDA Pro、Frida等工具链,详细剖析DroidGuard的信号拦截、内存校验等保护机制,并附Python脚本实现自动化分析,助力Android安全研究人员突破GMS核心组件的安全防护。

weixin_32428571的博客 321

【CF1114】【贪心(排序)】Yet Another Array Partitioning Task

Yet Another Array Partitioning Task 【题意】: 题目意思是:给你n个数,让你分开为k组,每一组必须不小于m,然后每一组挑选出m个数,每一组数求和得到的结果输出,并且输出对应的分界点。 【题解】: 自己的做法: 其实很容易观察就得出一个小结论,就是:无论你怎么分都好,必定会把排序后前m*k大的数给挑选出来。这个结论虽然说死了,但是你只能从这一个突破口出发...

Z_sea的博客 795

【寄存器模型】二、前门访问与后门访问

1.后门访问可以更好的完成前门访问能够完成的事情:后门访问不消耗仿真时间,且消耗的运行时间也远小于前门访问的运行时间。 2.后门访问可以完成前门访问不能完成的事:对于某些特定的场景,如网络通信系统中的一些只读计数器,当要验证计数器进位归零的场景时,前门访问无法完成写寄存器操作,只能通过消耗仿真时间来使计数器到达特定的计数值。而后门访问可以直接对只读寄存器赋一个初值......

carrotbanana的博客 9669

AD20丝印层导入图片logo保姆级教程(附黑白预览避坑技巧)

本文提供了一份详尽的AD20丝印层导入图片Logo的实战指南。核心在于将图片预处理为高质量的单色BMP位图,并独家分享了利用Windows画图工具进行“黑白预览”的关键技巧,以提前规避转换失真。教程涵盖了从脚本获取、图片处理、参数设置到生产规范的完整流程,帮助硬件工程师高效、准确地在PCB的Top Overlay层添加个性化标识。

weixin_29194255的博客 397

HLS ARRAY_PARTITION

type有三个可选值,分别为block、cyclic和complete。 为便于说明,这里我们以一个数组长度为12的一维数组A[12]为例。 如果type为block,factor为4,意味着将A[12]分割为4个小的数组,这样每个数组的长度为12/4=3,同时每个数组中的元素是按顺序依次从原始数组中获取。 如果type为cyclic,factor为4,这仍是将数组分割为4个长度为3的小数组,每个数组中的元素是交织地从原始数组中获取。 如果type是complete,此时参数factor不起作用,可理..

qq_35608277的博客 4291

NotebookLM平替实测:用Open Notebook+Ollama零成本搭建私有AI知识库

本文提供了一份详细的实战指南,教你如何利用Open Notebook与Ollama,在本地零成本搭建一个功能强大且完全私有的AI知识库。通过Docker容器化部署,实现数据绝对安全、模型自由选择与离线运行,是NotebookLM等云端工具的绝佳平替方案,尤其适合注重数据隐私的教育工作者、研究者及小团队。

weixin_29052717的博客 527

Vitis HLS中的Array Partition与Array Reshape详解

在高层次综合(HLS)设计中,数组是最常用的数据结构之一,但默认情况下,HLS会将数组映射到单个BRAM块,这会限制并行访问能力,成为性能瓶颈。为了克服这一限制,Vitis HLS提供了两种强大的数组优化指令:`ARRAY_PARTITION`和`ARRAY_RESHAPE`。这两种优化都能显著提高设计的并行度,但它们的工作原理、适用场景和资源影响各不相同。本文将深入剖析这两种优化技术的细节,帮助您在HLS设计中做出明智的选择。

kanhao100的博客 1534

79-Halcon-- visualize_object_model_3d 函数功能(交互式3D可视化)

该算子是连接3D数据与决策者的关键可视化桥梁,通过其独特的交互能力和高质量的渲染效果,将复杂的3D信息转化为直观的可视内容,大幅提升工程决策效率和质量。操作符,不仅能在窗口中渲染3D对象,还支持多种交互操作,适用于调试、展示和交互分析场景。本解析将全面探讨其参数功能及工业级应用方案。+几何类型: PointCloud/ TriMesh/CAD。+法线属性: [NX,NY,NZ]+附加数据: Attributes。是Halcon中功能强大的。+顶点属性: [X,Y,Z]+颜色属性: [R,G,B]

weixin_45590420的博客 1214

40、5G NR射频要求解析

本文详细解析了5G NR射频相关要求,包括参考灵敏度与动态范围的定义、接收机对干扰信号的抗扰性要求(如阻塞、邻道选择性等)、辐射射频要求在FR1和FR2频段的差异,以及多标准无线电基站(MSR)的发展及其对射频要求的影响。内容涵盖测试模型、干扰场景、具体指标和实现挑战,为理解和应用5G NR射频规范提供了全面指导。

qsc901234的博客 270

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

【Python爬虫教程】第3篇-解决使用reqeusts遇到的ProxyError异常

Python request代理错误提示“HTTPSConnectionPool, Cannot connect to proxy, no such file or directory”

分享技术,记录思考和感悟 1521

【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

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

LeetCode //C - 561. Array Partition

【代码】LeetCode //C - 561. Array Partition。

Made in Code 1756

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

[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

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

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

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

561. Array Partition I*

561. Array Partition I* https://leetcode.com/problems/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), ..., ...

珍妮的选择的博客 201

【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

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

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

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

Leetcode 561. 数组拆分 I 1 题目描述(Leetcode题目链接)   给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1,b1),(a2,b2),...,(an,bn)(a1, b1), (a2, b2), ..., (an, bn)(a1,b1),(a2,b2),...,(an,bn) ,使得从1 到 n 的 min(ai,bi)min(ai, bi)m...

qq_39378221的博客 207

[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 390

[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

题目链接:https://leetcode.com/problems/array-partition-i/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) w...

ZayneHuang的博客 182

【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的博客 525
上一篇: [LeetCode]409. Longest Palindrome(最长回文)
下一篇: [LeetCode]563. Binary Tree Tilt(二叉树的差值)
whl_program
博客等级 码龄11年 101粉丝 279原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址