leetCode刷题记录71_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
Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4).
给定一个整数数组,长度为2的倍数,将这个数组中的数两两组合,求两两组合的数的最小数的和的最大值
/*****************************************************我的解答*************************************************
完全是直觉解题,先由小到大排序,然后奇数位的和即为最后结果。
/**
 * @param {number[]} nums
 * @return {number}
 */
var arrayPairSum = function(nums) {
    nums.sort((a,b) => {return a - b;});
    var ret = 0;
    for(var index = 0; index < nums.length; index += 2)
    {
        ret += nums[index];
    }    
    return ret;
};
 

发布了135 篇原创文章 · 获赞 10 · 访问量 6250

猜你喜欢

转载自blog.csdn.net/gunsmoke/article/details/89081006
今日推荐