题目描述

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

示例 1:

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

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

提示:

  1. n 是正整数,范围在 [1, 10000].
  2. 数组中的元素范围在 [-10000, 10000].

思路

分组之后min(ai, bi)的和最大,同组两个数相差越小越好,所以需要先对数组进行排序后,再取偶数位的和即可。

代码实现

package Array;

import java.util.Arrays;

/**
* 561. Array Partition I(数组拆分 I)
* 给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从1 到 n 的 min(ai, bi) 总和最大。
*/
public class Solution561 {
public static void main(String[] args) {
Solution561 solution561 = new Solution561();
int[] nums = {1, 4, 3, 2};
System.out.println(solution561.arrayPairSum(nums));
} public int arrayPairSum(int[] nums) {
int sum = 0;
Arrays.sort(nums);
for (int i = 0; i < nums.length; i += 2) {
sum += nums[i];
}
return sum;
}
}

Leetcode#561. Array Partition I(数组拆分 I)的更多相关文章

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

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

  3. LeetCode 561 Array Partition I 解题报告

    题目要求 Given an array of 2n integers, your task is to group these integers into n pairs of integer, sa ...

  4. LeetCode 561. Array Partition I (C++)

    题目: Given an array of 2n integers, your task is to group these integers into npairs of integer, say ...

  5. [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 ...

  6. Leetcode561.Array Partition I数组拆分1

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

  7. 561. Array Partition I - LeetCode

    Question 561. Array Partition I Solution 题目大意是,给的数组大小是2n,把数组分成n组,每组2个元素,每个组取最小值,这样就能得到n个值,怎样分组才能使这n个 ...

  8. 561. Array Partition I【easy】

    561. Array Partition I[easy] Given an array of 2n integers, your task is to group these integers int ...

  9. 【LeetCode】数组-6(561)-Array Partition I(比较抽象的题目)

    题目描述:两句话发人深思啊.... Given an array of 2n integers, your task is to group these integers into n pairs o ...

随机推荐

  1. [luogu1970][花匠]

    题目地址 https://www.luogu.org/problemnew/show/P1970 题目描述 花匠栋栋种了一排花,每株花都有自己的高度.花儿越长越大,也越来越挤.栋栋决定 把这排中的一部 ...

  2. javascript 实现页面显示当前时间 动态读秒

    用户进入网站后,出于友好目的,可以添加一些欢迎语句,并且显示系统当前时间,动态读秒的操作.还是直接粘贴代码吧 <script type="text/javascript"&g ...

  3. Day9--Python--函数入门

    函数神马是函数: 函数是对功能或动作的封装函数的定义: def 函数名(形参列表): #参数 函数体(return) 调用: ret = 函数名(实参列表) 函数名就是变量名: 函数名的命名规则:变量 ...

  4. java十进制转三十六进制

    import java.util.HashMap; public class Ten2Thirty { private static final String X36 = "01234567 ...

  5. 9、字符串转数字用int、因为input里面接受的全部是字符串

    #!/user/bin/python# -*- coding:utf-8 -*-num = int(input('请输入数字:'))if num == 1: print(666)elif num == ...

  6. Tensorflow搞一个聊天机器人

    catalogue . 前言 . 训练语料库 . 数据预处理 . 词汇转向量 . 训练 . 聊天机器人 - 验证效果 0. 前言 不是搞机器学习算法专业的,3个月前开始补了一些神经网络,卷积,神经网络 ...

  7. Python的基础详情

    Python的基础信息 Python是一种动态解释性高级语言 Python即可面向对象,也可以面向过程 解释行语言 无需编译 程序以'行'为单位进行执行 执行速度慢 开发效率快 可跨平台 编译型语言 ...

  8. vue基础篇---路由的实现《2》

    现在我们来实现这样一个功能: 一个页面,包含登录和注册,点击不同按钮,实现登录和注册页切换: 编写父组件 index.html <div id="app"> <s ...

  9. .aspx、MasterPage、.ascx加载顺序

    1.    Master page中的用户控件的 page_init2.    Aspx页面中的用户控件的 page_init3.    Master page的page_init4.    Aspx ...

  10. js拼接HTML页面元素a标签遇到的问题

    业务,如下图需要做一个广告轮播的图片链接 使用了ajax请求后台,在js拼接html,关键代码: $("#scroll_img").html(""); for ...