561. Array Partition I - LeetCode
Question

Solution
题目大意是,给的数组大小是2n,把数组分成n组,每组2个元素,每个组取最小值,这样就能得到n个值,怎样分组才能使这n个数相加最小。
思路:有点田忌赛马的意思,肯定最大和第二大一组,取最小值即第二大的数,依次类推。。。这样就需要排序,隔一个取一个。
Java实现:
public int arrayPairSum(int[] nums) {
Arrays.sort(nums);
int total = 0;
for (int i=0; i<nums.length; i+=2) {
total += nums[i];
}
return total;
}
561. Array Partition I - LeetCode的更多相关文章
- Leetcode#561. Array Partition I(数组拆分 I)
题目描述 给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从1 到 n 的 min(ai, bi) 总和最 ...
- 561. Array Partition I【easy】
561. Array Partition I[easy] Given an array of 2n integers, your task is to group these integers int ...
- 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 ...
- 【LeetCode】561. Array Partition I 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...
- 【LeetCode】数组-6(561)-Array Partition I(比较抽象的题目)
题目描述:两句话发人深思啊.... Given an array of 2n integers, your task is to group these integers into n pairs o ...
- leetcode 561.Array Partition I-easy
Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...
- LeetCode 561 Array Partition I 解题报告
题目要求 Given an array of 2n integers, your task is to group these integers into n pairs of integer, sa ...
- [LeetCode] 561. Array Partition I_Easy tag: Sort
Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...
- [LeetCode&Python] Problem 561. Array Partition I
Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...
随机推荐
- 完美解决 scipy.misc.imread 报错 TypeError: Image data cannot be converted to float
File "/home/harrison/anaconda3/lib/python3.7/site-packages/matplotlib/image.py", line 634, ...
- (stm32学习总结)—对寄存器的理解
芯片里面有什么 我们看到的 STM32 芯片是已经封装好的成品,主要由内核和片上外设组成.若与电脑类比,内核与外设就如同电脑上的 CPU 与主板.内存.显卡.硬盘的关系.STM32F103 采用的是 ...
- 用css动态实现圆环百分比分配——初探css3动画
最近的小程序项目有个设计图要求做一个圆环,两种颜色分配,分别代表可用金额和冻结金额.要是就直接这么显示,感觉好像挺没水平??于是我决定做个动态! 在mdn把新特性gradients(渐变).trans ...
- 小程序入门系列之 tabBar
本系列为简单入门系列,以一定概括性思路来叙述内容,具体可以查看官网 大部分的电商应用都是底部或顶部多 tab 的模式. 下面我们从配置角度来分析一下: 第一个:position 配置如下: 默认是 b ...
- C#设计编写一个控制台应用程序
设计编写一个控制台应用程序,练习类的继承. (1) 编写一个抽象类 People,具有"姓名","年龄"字段,"姓名"属性,Work 方法. ...
- java中Error和Exception用法上有什么区别,Error是怎么回事?
顺便提一句, 和Exception 相对应的,还有Error,Error(错误)表示系统级的错误和程序不必处理的异常,是JRE(java运行环境)的内部错误或者硬件问题,比如,另外 某一处地方的bug ...
- 静态变量和成员变量的区别、final修饰特点、创建对象的内存图、静态内存图
静态变量和成员变量的区别* 静态变量也叫类变量 成员变量也叫对象变量* A:所属不同 * 静态变量属于类,所以也称为为类变量 * 成员变量属于对象,所以也称为实例变量(对象变量)* B:内存中位置不 ...
- JavaScript中数组的方法和字符串方法总结
数组是首先的一个对象, 可以通过Array构造器创建一个数组,数组方法总结如下 cacat() 链接两个数组 join() 将数组链接成字符串 pop() 删除最后一个元素 shift() 删 ...
- 使用pyttsx3实现简单tts服务
操作系统:Windows 10_x64 python版本:Python 3.9.2_x64 pyttsx3版本: 2.90 pyttsx3是一个tts引擎包装器,可对接SAPI5.NSSS(NSS ...
- C++---初识C++
C和C++的关系 C语言是结构化和模块化的语言, 面向过程. C++是在C语言的基础上, 增加了面向对象的机制, 并对C语言的功能进行了扩充. 变量的定义可以出现在程序中的任何行 提供了标准输入输出流 ...