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 Menlo; color: #323333; background-color: #f5f5f5; min-height: 15.0px }
p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "Helvetica Neue"; color: #323333; min-height: 16.0px }
li.li1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "Helvetica Neue"; color: #323333 }
span.s1 { }
span.s2 { font: 11.0px "Helvetica Neue" }
ol.ol1 { list-style-type: decimal }

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

Explanation: n is 2, and the maximum sum of pairs is 4.

Note:

  1. n is a positive integer, which is in the range of [1, 10000].
  2. All the integers in the array will be in the range of [-10000, 10000].

Solution 1: The sum of differences between ai and bi should be smallest and sum of the distances of adjacent elements is the smallest. The proof can be seen in https://discuss.leetcode.com/topic/87206/java-solution-sorting-and-rough-proof-of-algorithm

 1 class Solution {
 2 public:
 3     int arrayPairSum(vector<int>& nums) {
 4         sort(nums.begin(),nums.end());
 5         long long res=0;
 6         for (int i=0;i<nums.size();i+=2){
 7             res+=nums[i];
 8         }
 9         return res;
10     }
11 };

Solution 2: use bucket sort which is O(n)  https://discuss.leetcode.com/topic/87483/c-code-o-n-beats-100

时间: 2024-08-12 17:41:19

561. Array Partition I的相关文章

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(数组拆分 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 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】数组-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. 感觉题目的大致意思就是把数组分成很多个二元数组,对它

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

题目: 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组,求每一组中的较小值,

【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

[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

[LeetCode] 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