Array Partition I

题目描述

Given an array of2nintegers, your task is to group these integers intonpairs 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 = min(1, 2) + min(3, 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].

题目链接

https://leetcode-book.guazidou.com/Array/pascals-triangle.html

Java

思路

先排序然后取奇数索引的数值累加

代码

class Solution {
public int arrayPairSum(int[] nums) {
Arrays.sort(nums);
int maxSum = 0;
for (int i = 0; i < nums.length; i++) {
if (i % 2 == 0) maxSum += nums[i];
}
return maxSum;
}
}

Golang

package main
import (
"fmt"
"sort"
)
func ArrayPairSum(nums []int)int{
sort.Ints(nums)
maxSum := 0
for i:=0; i < len(nums); i++{
if i % 2 == 0 {
maxSum += nums[i]
}
}
return maxSum;
}
func main() {
nums := []int{1, 3, 4, 2}
fmt.Println(ArrayPairSum(nums))
}