【LeetCode】561. Array Partition I

本文介绍了一种针对数组的算法问题,即将一个包含2n个整数的数组分成n组,每组两个数,目标是使所有组内的最小值之和尽可能大。文章详细解释了解决方案的步骤,包括对数组进行排序并选取每组中的较小值以达到最优解。

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:
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].

题目:
假设有一个含有2n项的数组,将这个数组分为n组,两个数为一组,将每组中的最小数相加,得出的和即为输出。

根据给出的例子分析得出:
首先,需要将数组以从小到大的顺序重新排列;
然后,我们需要将两个相邻的数划分为一组,并取其中较小的一个数,
因为这些数已经按照从小到大的顺序排列了,所以,每组中的较小的那个数只能是每组的第一个,也就是数组索引为偶数的数,
故只需要将重新排列后的数组中索引为偶数的数相加即可。

/**
 * @param {number[]} nums
 * @return {number} sum
 */
var arrayPairSum = function(nums) {
    var len = nums.length;
    for (var i = 0; i < len; i++) {
        for (var j = 0; j < len-1-i; j++) {
            if (nums[j] > nums[j+1]) {
                var temp  = nums[j];
                nums[j] = nums[j+1];
                nums[j+1] = temp;
            }
        }
    }
    var sum = 0;
    for (var k = 0; k < len; k += 2) {
        sum += nums[k];
    }
    return sum;
};

在上面这段我实现的代码中,数组排序我采用的是冒泡排序。
考虑到最优性,可以采用数组的sort()方法来进行排序。
排序的代码如下:

nums.sort(function(a,b){
    return a-b;
});

或者使用箭头函数来表示:

nums.sort((x,y)=>x-y);

【注】关于sort()方法的实现,数量小于10的数组使用insertSort排序,大于10时使用quickSort排序。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值