561. 数组拆分 I

地址:https://leetcode-cn.com/problems/array-partition-i/

<?php
/**
给定长度为 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).

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/array-partition-i
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
 */
class Solution {

    /**
     * @param Integer[] $nums
     * @return Integer
     */
    function arrayPairSum($nums) {
        sort($nums);

        $num = 0;

        for ($i = 0, $count = count($nums); $i < $count; $i += 2) {
            $num += $nums[$i];
        }

        return $num;
    }
}

 

posted @ 2020-09-27 14:04  花花妹子。  阅读(98)  评论(0编辑  收藏  举报