问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3718 访问。

给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从1 到 n 的 min(ai, bi) 总和最大。

输入: [1,4,3,2]

输出: 4

解释: n 等于 2, 最大总和为 4 = min(1, 2) + min(3, 4).

提示:

n 是正整数,范围在 [1, 10000].

数组中的元素范围在 [-10000, 10000].


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.

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


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3718 访问。

public class Program {

    public static void Main(string[] args) {
int[] nums = null; nums = new int[] { 1, 4, 3, 2 };
var res = ArrayPairSum(nums);
Console.WriteLine(res); Console.ReadKey();
} private static int ArrayPairSum(int[] nums) {
Array.Sort(nums);
var sum = 0;
for(int i = 0; i < nums.Length; i += 2) {
sum += nums[i];
}
return sum;
} }

以上给出1种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3718 访问。

4

分析:

LeetCode 并没有把这题打上贪心标签,我觉得是可以讨论的。我们要解的是所有子数组 min 之后的最大和,这是一种典型的需要贪心思想求解的题目,以局部最优解合并得出整体最优解,所以关键点在于贪心策略的选择。那么对于该题来说贪心策略显然为每个子数组中的2个数的差的绝对值在最小时,整个数组合并出的 min 值最大;否则,若2个数的差的绝对值过大,那么对于整个拆分过后的数组来说,损失了过多的值。也就是说,我们直接排序之后,取出奇()数值相加即可。

显而易见,以上算法的时间复杂度基于 Array.Sort() 所采用的排序算法。

C#LeetCode刷题之#561-数组拆分 I(Array Partition I)的更多相关文章

  1. C#LeetCode刷题-树状数组

    树状数组篇 # 题名 刷题 通过率 难度 218 天际线问题   32.7% 困难 307 区域和检索 - 数组可修改   42.3% 中等 315 计算右侧小于当前元素的个数   31.9% 困难 ...

  2. leetCode刷题(找出数组里的两项相加等于定值)

    最近被算法虐了一下,刷一下leetcode,找找存在感 如题: Given an array of integers, return indices of the two numbers such t ...

  3. C#LeetCode刷题之#643-子数组最大平均数 I( Maximum Average Subarray I)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3728 访问. 给定 n 个整数,找出平均数最大且长度为 k 的连 ...

  4. LeetCode 561:数组拆分 I Array Partition I

    文章全部来自公众号:爱写bug 算法是一个程序的灵魂. Given an array of 2n integers, your task is to group these integers into ...

  5. [Swift]LeetCode561. 数组拆分 I | Array Partition I

    Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...

  6. C#LeetCode刷题-数组

    数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...

  7. LeetCode刷题指南(字符串)

    作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...

  8. LeetCode刷题总结-数组篇(下)

    本期讲O(n)类型问题,共14题.3道简单题,9道中等题,2道困难题.数组篇共归纳总结了50题,本篇是数组篇的最后一篇.其他三个篇章可参考: LeetCode刷题总结-数组篇(上),子数组问题(共17 ...

  9. Java实现 LeetCode 561 数组拆分 I(通过排序算法改写PS:难搞)

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

随机推荐

  1. [译]使用DOT语言和GraphvizOnline来可视化你的ASP.NETCore3.0终结点01

    这是系列文章中的第一篇:使用GraphvizOnline可视化ASP.NETCore3.0终结点.. 第1部分-使用DOT语言来可视化你的ASP.NETCore3.0终结点(本文) 第2部分-向ASP ...

  2. Maven如何利用父工程对版本进行统一管理

    项目开发中我们该怎么对项目依赖的版本进行统一管理呢 答:创建一个父级工程,让所有的业务模块都继承该父级工程,即所有的业务都为Module 在父级工程pom文件添加<dependencyManag ...

  3. mybatis的<if>标签,<foreach>标签,<collection>标签,<association>标签以及useGeneratedKeys用法

    <if>标签 1.判断非空或不等于 <if test="assessTypes!= null and assessTypes!='' "> AND FIND ...

  4. oracle 启动报ORA-01105 ORA-19808

    bash-4.4$ srvctl start instance -i jfcddb2 -d jfcddb PRCR-1013 : Failed to start resource ora.jfcddb ...

  5. less : 解决升级后报错的问题

    vue2项目. 上版本. { "name": "xxx", "version": "1.0.0", "desc ...

  6. Electron-vue 项目搭建

    Electron 应用技术体系推荐 目录结构 demo(项目名称) ├─ .electron-vue(webpack配置文件) │  └─ build.js(生产环境构建代码) | └─ dev-cl ...

  7. MySQL 高级性能优化架构 千万级高并发交易一致性系统基础

    一.MySQL体系架构 由图,可以看出MySQL最上层是连接组件.下面服务器是由连接池.管理服务和工具组件.SQL接口.查询解析器.查询优化器.缓存.存储引擎.文件系统组成. 1.连接池 管理.缓冲用 ...

  8. 记IntelliJ IDEA创建spring mvc一次坑爹的操作!!!!

    本人刚开始学习spring mvc,遇到一问题,现在分享一下. 点击Next,创建项目完成,你会发现缺少很多东西. lib文件没有,里面的jar更没有.applicationContext.xml和d ...

  9. 火狐浏览器如何使用二次验证码/虚拟MFA/两步验证/谷歌验证器?

    一般点账户名——设置——安全设置中开通虚拟MFA两步验证 具体步骤见链接  火狐浏览器如何使用二次验证码/虚拟MFA/两步验证/谷歌验证器? 二次验证码小程序于谷歌身份验证器APP的优势 1.无需下载 ...

  10. Git别名和配置文件

    目录 备注: 配置别名 配置文件 备注: 本文参考于廖雪峰老师的博客Git教程.依照其博客进行学习和记录,感谢其无私分享,也欢迎各位查看原文. 配置别名 如果,如果这么神器的Git版本控制系统,可以简 ...