题目描述:两句话发人深思啊。。。。

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.

感觉题目的大致意思就是把数组分成很多个二元数组,对它们以求最小值的方式求和,使得和最大。

思路:

最开始的思路,现在还不知道对不对,就是先排序数组,使用一个指针向后遍历,求最小并求和。⚠️【注意】指针每次累加 2

【正确代码】 一次写对~

 class Solution {
public int arrayPairSum(int[] nums) {
if (nums.length % 2 != 0 || nums == null) {
return -1;
}
Arrays.sort(nums);
int maxSum = 0;
for (int i = 0; i < nums.length - 1; i += 2) {
maxSum += Math.min(nums[i], nums[i + 1]);
}
return maxSum;
}
}

时间复杂度:O(n*logn)

空间复杂度:O(n*logn)

【LeetCode】数组-6(561)-Array Partition I(比较抽象的题目)的更多相关文章

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

  2. Leetcode#561. Array Partition I(数组拆分 I)

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

  3. 561. Array Partition I - LeetCode

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

  4. 561. Array Partition I【easy】

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

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

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

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

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

  8. LeetCode 561 Array Partition I 解题报告

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

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

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

随机推荐

  1. c# 测试通过

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data; using S ...

  2. java中的方法引用(method reference)官方文档总结

    2017/7/5 转载写明出处:http://www.cnblogs.com/daren-lin/p/java-method-reference.html 今天要说的是java中的一项新特性,方法引用 ...

  3. Java分形

    目前笔者接触过的分形主要有一下几种: 1.类似Clifford的分形.这种分形的特点是:分形的初始坐标为(0,0),通过初始坐标经过大量的迭代,得到一系列的点,根据得到的点来绘制分形曲线.这类分形的参 ...

  4. Entity Framework Core 命名约定

    本文翻译自<Entity Framework Core: Naming Convention>,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 注意:我使用的是 Entity ...

  5. zepto.js 的tap事件中点击一次触发两次事件

    html代码: <div class="xh-lxx-cart-count1"> <span class="minus">-</s ...

  6. 真实记录我入门学习Linux系统的经历

    我本身来说并不是计算机专业的学生,因此今天来谈及这个话题,对大家来说,有了更多的客观公正性.对我而言,linux给我最大的财富,并不是编程能力提高了多少,而是视野的开阔.心态的转变和自学能力的提高.我 ...

  7. Linux服务器下对Oracle作Rman备份

    由于工作需要,最近要对几台Linux系统下的Oracle数据库进行Rman备份,就在操作的同时,整理了一下,方便今后作为资料进行查阅. ------------------------Linux服务器 ...

  8. Linux服务器学习(一)

    一.首先连接服务器 下载一个windows下连接linux的ssh工具,我这里用的putty.一次填入HostName(主机名,可以是服务器域名也可以是对应的ip).Port(端口号默认为22).Co ...

  9. 【转载】接触Matlab10年后的一个总结,随时使用Matlab要掌握的一些要点

    来源: http://www.cnblogs.com/asxinyu/p/Basic_Matlab_Experience.html 接触Matlab10年后的一个总结,随时使用Matlab要掌握的一些 ...

  10. python函数(1):初始函数

    在学了前面很多python的基础类型后,我们终于可以进入下一阶段,今天我们将走进一个函数的新世界. 预习: 1.写函数,计算传入字符串中[数字].[字母].[空格] 以及 [其他]的个数 2.写函数, ...