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

【LeetCode】第561题——数组拆分I(难度:简单) 【LeetCode】第561题——数组拆分I(难度:简单)题目描述解题思路代码详解注意点 题目描述 给定长度为 2n 的整数数组 nums ,你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), …, (an, bn) ,使得从 1 到 n 的 min(ai, bi) 总和最大。 返回该 最大总和 。 示例 1: 输入:nums = [1,4,3,2] 输出:4 解释:所有可能的分法(忽略元素顺序)为: (1, 4), (2, 3) -> min(1, 4) + min 阅读详情

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), (... 阅读详情

相关推荐

手把手用ROS实现Ego-Planner动态避障:无人机撞树问题终结方案

本文详细介绍了如何在ROS和Gazebo仿真环境中,通过配置和调优Ego-Planner算法,实现无人机的动态避障功能。文章深入解析了其基于局部梯度优化的核心思想,提供了从环境搭建、参数详解到代码集成的完整实践指南,并分享了实战排错经验,旨在彻底解决无人机在复杂动态场景中的碰撞问题。

weixin_29031161的博客 1641

快速排序、归并排序、数组切分(Partition Array)、Quick Select算法总结

文章目录1. 快速排序思路代码2. 归并排序思路代码3. 数组切分问题(Partition Array)3.1 [LintCode-31. Partition Array](https://www.lintcode.com/problem/partition-array/description)题意思路代码3.2 [LintCode373. Partition Array by Odd and E...

超级无敌小胖头 921

STM32F103 超声波风速风向仪实战:0.1m/s精度风洞标定与OLED显示(附源码)

本文详细介绍了基于STM32F103的超声波风速风向仪设计与实现,通过超声波时间差测量技术实现0.1m/s高精度测量。文章涵盖硬件设计、算法实现、风洞标定及OLED显示等关键环节,提供完整源码,适用于气象监测、风力发电等领域。

weixin_42532938的博客 279

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

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

Z_sea的博客 794

物联网开发131 - Micropython ESP32 C3连接TB6600驱动42BYGH39步进电机

如何使用合宙ESP32 C3连接TB6600驱动42BYGH39步进电机,实现控制步进电机正反转和旋转指定角度等

zhusongziye的博客 2387

Unity3d游戏开发工程师笔试题学习

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录前言一、学习背景二、错题记录2.读入数据总结欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML 图表FLowchart流程图导出与导入导出导入 前言 提示:这里可以.

m0_54231818的博客 5472

LeetCode——561. 数组拆分 I(Array Partition I)[简单]——分析及代码(Java)

LeetCode——561. 数组拆分 I[Array Partition I][简单]——分析及代码[Java]一、题目二、分析及代码1. 排序(1)思路(2)代码(3)结果三、其他 一、题目 给定长度为 2n 的整数数组 nums ,你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), …, (an, bn) ,使得从 1 到 n 的 min(ai, bi) 总和最大。 返回该 最大总和 。 示例 1: 输入:nums = [1,4,3,2] 输出:4 解释:所有可能的分法(忽

江南土豆的博客 277

南大计算机2024保研笔试:编译原理等5门专业课考点分布与408复习避坑指南

本文详细解析了南京大学计算机2024保研笔试的考点分布与复习策略,重点突出编译原理(占比28%-32%)和离散数学的特色考核。提供跨课程复习方法、真题题型分析及高效备考建议,帮助考生突破传统408框架,应对南大独特的命题风格。

weixin_29304985的博客 367

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

32位快速加法器设计

32位快速加法器设计

好好学习,天天向上 3305

Codex 100个真实案例 - 用AI自动生成API测试用例(接口测试全覆盖)

API 测试是保障后端质量的关键环节,但手写测试用例既枯燥又容易遗漏。本案例将展示如何用 Codex 从 Swagger/OpenAPI 规范文档自动生成 pytest 测试用例——包括正常流程测试、边界值测试、异常参数测试、性能压力测试,还能自动生成 Mock 数据和精美的 HTML 测试报告。全程自然语言驱动,让 API 测试覆盖率从 0 飙到 90%+!

yueyeguzhuo的专栏 280

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

OpenCASCADE:Direct3D CSharp 示例

OpenCASCADE:Direct3D CSharp 示例OpenCASCADE:Direct3D CSharp 示例 OpenCASCADE:Direct3D CSharp 示例 此示例演示了如何在使用CSharp和Windows Presentation Foundation (WPF)编写的.Net应用程序中使用 OCCT 和 DirectX 库。 .Net、OCCT (C++) 和 DirectX 级别之间的连接由用 C++/CLI 编写的代理库OCCProxy和D3DProxy提供。代理库OCC

希望我的博客,能帮上你解决学习中工作中所遇到的问题 1836

【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

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

LeetCode //C - 561. Array Partition

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

Made in Code 1756

假人有限元模型

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

[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

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

【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

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

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的博客 524
上一篇: [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、付费专栏及课程。

余额充值