【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 possible.
解析:
数组分割1
给一组2n个整数,分割为n组,一组2个数,使min(a,b)之和最大
Example:
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).
我的解法
很明显每组数值越接近,浪费的数值越小,所得的总和最大,我的解法如下
public class ArrayPartitionI {
public static int arrayPartitionI(int[] array) {
Arrays.sort(array);
int sum = 0;
for (int i = 0; i < array.length - 1; i += 2) {
sum += array[i];
}
return sum;
}
}
最优解法
public class Solution {
public int arrayPairSum(int[] nums) {
Arrays.sort(nums);
int result = 0;
for (int i = 0; i < nums.length; i += 2) {
result += nums[i];
}
return result;
}
}
哈哈几乎一样,后来想了一下不用length-1也可以的
【leetcode】561. Array Partition I的更多相关文章
- 【LeetCode】561. Array Partition I 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...
- 【easy】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】954. Array of Doubled Pairs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】565. Array Nesting 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】Rotate Array
Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = ...
- 【leetcode】1243. Array Transformation
题目如下: Given an initial array arr, every day you produce a new array using the array of the previous ...
- 【leetcode】954. Array of Doubled Pairs
题目如下: Given an array of integers A with even length, return true if and only if it is possible to re ...
- 【leetcode】565. Array Nesting
You are given an integer array nums of length n where nums is a permutation of the numbers in the ra ...
- 【LEETCODE】39、第561题 Array Partition I
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
随机推荐
- 一百三十二:CMS系统之前端动态获取后台添加的轮播图
先准备几张轮播图 排序顺序改为根据优先级倒序排 前端首页接口 @bp.route('/')def index(): banners = BannerModel.query.order_by(Banne ...
- [Scikit-learn] 1.1 Generalized Linear Models - Comparing various online solvers
数据集分割 一.Online learning for 手写识别 From: Comparing various online solvers An example showing how diffe ...
- SqlDbx连接oracle(可用)
解压SqlDbx.zip,将SqlDbx放到C:盘根目录 1.Path里面增加:C:\SqlDbx Path是为了找tnsnames.ora 2.增加系统变量:ORACLE_HOME,路径:C:\S ...
- process.env.NODE_ENV
Node 随记 if (process.env.NODE_ENV === 'production') { module.exports = require('./prod.js') } else { ...
- 去除表视图section的粘性问题
// 去除section的粘性 - (void)scrollViewDidScroll:(UIScrollView *)scrollView{ if (scrollView == self.tabl ...
- Re0:在 .NetCore中 EF的基本使用
整理一下目前在用的EFCore 记得好像是因为懒得写sql,于是开始试着用EF 先根据数据库生成一个好东西,嗯 Scaffold-DbContext "Data Source=localho ...
- 【AMAD】sorl-thumbnail -- Django缩略图
动机 简介 个人评分 动机 生成缩略图是一个烦人的工作. 简介 sorl-thumbnail1的特性包括: 支持不同的storage 实现缩略图的引擎是可以切换的:Pillow, ImageMagic ...
- DC-1靶机
DC-1 靶机获取:http://www.five86.com/ 发现IP:arp-scan --interface=eth0 -localnet arp-scan -l 靶机IP:192.168.0 ...
- Hogan.js的使用
一个比较简单的前端模板引擎,参考一下两篇文件即可学会:https://www.cnblogs.com/zhangruiqi/p/8547268.htmlhttps://www.imooc.com/ar ...
- ahk实现git图床自动预览以及转换markdown格式
ahk实现git图床自动预览以及转换markdown格式 软件地址 https://gitee.com/layty/pic/tree/master/app 软件功能: 检测剪切板,如果剪切板有非文本信 ...