leetcode刷题记录--Array Partition I

题目

难度:easy

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).
Note:

n is a positive integer, which is in the range of [1, 10000].
All the integers in the array will be in the range of [-10000, 10000].

第一次解法

/**
 * @param {number[]} nums
 * @return {number}
 */
var arrayPairSum = function(nums) {
    var res = 0
    nums.sort(function(a,b){
        return a-b
    }).forEach(function(item,index){
        if(index%2 === 0){
            res += item
        }
    })
    return res
};
# runtime : 82 ms
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 13,004评论 0 33
  • 宋云走后,马倩影的情绪一直处于悲伤之中,神情愈发地落寞。 “马姐,别这样,石珍姐和云姐也不希望你这样,你这样,受伤...
    乔木木哒阅读 304评论 1 1
  • 《金文诚〈孟子〉学习笔记27梁惠王章句下10-2》 【学习笔记】 今天是丙申年丙申月甲子日,七月初八,2016年8...
    金吾生阅读 567评论 0 0
  • 关于夏天的记忆 仿佛都在八月 忽然聚散的云雨 悄悄来临的离别 仿佛只是看不见的风 摇摆着远方的路 摇摆着路上 被热...
    涛涛不绝82阅读 220评论 0 2

友情链接更多精彩内容