原题:

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的更多相关文章

  1. 【LeetCode】561. Array Partition I 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...

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

  3. 【LeetCode】954. Array of Doubled Pairs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. 【LeetCode】565. Array Nesting 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. 【LeetCode】Rotate Array

    Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = ...

  6. 【leetcode】1243. Array Transformation

    题目如下: Given an initial array arr, every day you produce a new array using the array of the previous ...

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

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

  9. 【LEETCODE】39、第561题 Array Partition I

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

随机推荐

  1. 深入理解 iptables 和 netfilter 架构

    [译] 深入理解 iptables 和 netfilter 架构 Published at 2019-02-18 | Last Update 译者序 本文翻译自 2015 年的一篇英文博客 A Dee ...

  2. golang web框架设计6:上下文设计

    context,翻译为上下文,为什么要设计这个结构?就是把http的请求和响应,以及参数结合在一起,便于集中处理信息,以后框架的扩展等.好多框架比如gin,都是有这个上下文结构. context结构为 ...

  3. Flutter参数的传递和接收

    上次只写了方法和参数,这次写了完整的示例,页面间参数的传递和接收的示例. 1.参数传递 用在程序上解释就是比如你进入一个商品选择列表,当你想选择一个商品的具体信息的时候,你就要传递商品编号,详细页面接 ...

  4. 【ABAP系列】SAP ABAP Break Point

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP Break P ...

  5. mysql/mariadb数据库在插入表数据时,ID竟然成奇数增加了?看完下面内容就知道怎么处理了。

    今天突然被问到一个问题,mysql数据库插入表数据时,设置了ID自增,但是插入数据后,ID却呈奇数增加,不是123456类型,而是13579形式,突然有点懵,研究了一会,发现是auto_increme ...

  6. redis的发布和订阅操作

  7. golang写入csv

    package main import ( "encoding/csv" "fmt" "os" ) func main() { file, ...

  8. TP5.1框架中的模型关联

    一对一关联 hasOne('关联模型','外键','主键'); 关联模型(必须):关联的模型名或者类名 外键:默认的外键规则是当前模型名(不含命名空间,下同)+_id ,例如user_id 主键:当前 ...

  9. Mac安装postgresql和卸载PostgreSQL

    1.homebrew安装 brew install postgresql 2.初始化 initdb /usr/local/var/postgres 3.创建数据库及查看数据库 (1)先创建db. cr ...

  10. Sumitomo Mitsui Trust Bank Programming Contest 2019 Task F. Interval Running

    Link. There is a nice approach to this problem that involves some physical insight. In the following ...