GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • LeetCodeJavascript.com
  • Star on GitHub

This view

  • Original page
  • New address

GeezerVison is showing leetcodejavascript.com — #561 - Array Partition - LeetCode JavaScript Solutions

Back to all solutions

#561 - Array Partition

Easy • View on LeetCode • View on GitHub
Array Sorting Greedy Counting Sort

Problem Description

Given an integer array nums of 2n integers, group these integers into n pairs (a1, b1), (a2, b2), ..., (an, bn) such that the sum of min(ai, bi) for all i is maximized.

Return the maximized sum.

Solution

/**
 * @param {number[]} nums
 * @return {number}
 */
var arrayPairSum = function(nums) {
  return nums.sort((a, b) => a - b).reduce((sum, v, i, a) =>
    sum + (i % 2 === 0 ? Math.min(v, a[i + 1]) : 0), 0);
};
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.