LeetCode.561. Array Partition I
題目的大意為給定一個長度為2n的陣列,把陣列分為n對,使從1到n的min(ai,aj)的總和最大。
比如:
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 int arrayPairSum(int[] nums) { Arrays.sort(nums); int sum = 0; for (int i = 0; i < nums.length; i += 2) { sum += nums[i]; } return sum; }
也有其他更好的方法,正在探索當中。
相關推薦
LeetCode 561. Array Partition I(easy難度c++)
return 可能 hid 使用 ide 復雜度 wid () ger 題目: Given an array of 2n integers, your task is to group these integers into n pairs of integer, say
LeetCode 561. Array Partition I (數組分隔之一)
art 需要 style output tco code 個數字 num array Given an array of 2n integers, your task is to group these integers into n pairs of integer,
算法--leetcode 561. Array Partition I
hash vector容器 一個 say 相加 整數 += blog hashtable 題目: Given an array of 2n integers, your task is to group these integers into n pairs of inte
[LeetCode] 561. Array Partition I
return using maximum pre big nat solution .so sort Given an array of 2n integers, your task is to group these integers into n pairs of in
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(a
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
【python3】leetcode 561. Array Partition I (easy)
561. Array Partition I (easy) Given an array of 2n integers, your task is to group these integers into n pairs of integer,
LeetCode.561. Array Partition I
題目的大意為給定一個長度為2n的陣列,把陣列分為n對,使從1到n的min(ai,aj)的總和最大。 比如: Input: [1,4,3,2] Output: 4 Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min
[LeetCode]561. Array Partition I (陣列分割槽 1)
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),
【LeetCode】數組-6(561)-Array Partition I(比較抽象的題目)
you num pos 代碼 images 抽象 時間 思路 bsp 題目描述:兩句話發人深思啊。。。。 Given an array of 2n integers, your task is to group these integers into n pairs of
Leetcode刷題記錄[python]——561 Array Partition I
partition span example ive 記錄 lan all [] result 一、前言 二、題561 Array Partition I Given an array of 2n integers, your task is to group the
561. Array Partition I - LeetCode
class urn src scrip problems 相加 arr for pair Question 561. Array Partition I Solution 題目大意是,給的數組大小是2n,把數組分成n組,每組2個元素,每個組取最小值,這樣就能得到n個值,怎
[leetcode][easy][Array][561][Array Partition I]
Array Partition I 題目連結 題目描述 給定大小為 2n 的整型陣列,你的任務是將這些整數分為n組,即 (a1,b1), (a2, b2),...,(an, bn),使得所有的 min(ai,bi) 之和最大,其中i的值為從1到n。 輸出上述要求中的和。 約定 n是一個正數,
561. Array Partition I【easy】
cas positive output not big ren emp let malle 561. Array Partition I【easy】 Given an array of 2n integers, your task is to group these in
561. Array Partition I
turn cto ati which ble from tor example AS Given an array of 2n integers, your task is to group these integers into n pairs of integer, s
561. Array Partition I 先有序,再求第0,2,4...n-1位置的和
Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..
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 m
[LeetCode] Array Partition I
int pla ger log block turn osi you logs Given an array of 2n integers, your task is to group these integers into n pairs of integer, say
Array Partition I
lis 取出 bject alt object def urn 最小 air 這道題為簡單題 題目: 思路: 其實這道題思路挺簡單的,把兩個數兩兩分成一組提取最小數相加,求相加的最大值。我首先會想把列表中最大的數提取出來,但是這是不可能
Leetcode561.Array Partition I陣列拆分1
給定長度為 2n 的陣列, 你的任務是將這些數分成 n 對, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得從1 到 n 的 min(ai, bi) 總和最大。 示例 1: 輸入: [1,4,3,2] 輸