【LeetCode】数组-6(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.

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

思路:

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

【正确代码】 一次写对~

 1 class Solution {
 2     public int arrayPairSum(int[] nums) {
 3         if (nums.length % 2 != 0 || nums == null) {
 4             return -1;
 5         }
 6         Arrays.sort(nums);
 7         int maxSum = 0;
 8         for (int i = 0; i < nums.length - 1; i += 2) {
 9             maxSum += Math.min(nums[i], nums[i + 1]);
10         }
11         return maxSum;
12     }
13 }

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

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

时间: 2024-11-05 18:33:56

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

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

题目描述 给定长度为 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). 提示: n 是正整数,范围在 [1, 10000]. 数组中的元素范围在 [-10000, 10000]. 思路 分组之后min(ai, bi)的和最大

Leetcode刷题记录[python]——561 Array Partition I

一.前言 二.题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. Example 1: Inp

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. Example 1: Input: [1,4,3,2] Output: 4 Explan

LeetCode 561. Array Partition I(easy难度c++)

题目: 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. Example 1: Input: [1,4,3,2] Output: 4 Expl

算法--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. 题意: 给出一个长度为2n的数组,将他们两个一组,分为n组,求每一组中的较小值,

[Swift]LeetCode561. 数组拆分 I | 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. Example 1: Input: [1,4,3,2] Output: 4 Explan

561. Array Partition I

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "Helvetica Neue"; color: #323333 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #323333; background-color: #f5f5f5 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px

【LEETCODE】39、第561题 Array Partition I

package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * @ClassName: ArrayPairSum * @Author: xiaof * @Description: 561. Array Partition I * Given an array of 2n integers, your task is to group these integers

LeetCode --- 88. Merge Sorted Array

题目链接:Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements init