Skip to content

Commit c610f2a

Browse files
committed
Add solution #561
1 parent c97ea8f commit c610f2a

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,390 LeetCode solutions in JavaScript
1+
# 1,391 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -445,6 +445,7 @@
445445
558|[Logical OR of Two Binary Grids Represented as Quad-Trees](./solutions/0558-logical-or-of-two-binary-grids-represented-as-quad-trees.js)|Medium|
446446
559|[Maximum Depth of N-ary Tree](./solutions/0559-maximum-depth-of-n-ary-tree.js)|Easy|
447447
560|[Subarray Sum Equals K](./solutions/0560-subarray-sum-equals-k.js)|Medium|
448+
561|[Array Partition](./solutions/0561-array-partition.js)|Easy|
448449
563|[Binary Tree Tilt](./solutions/0563-binary-tree-tilt.js)|Easy|
449450
564|[Find the Closest Palindrome](./solutions/0564-find-the-closest-palindrome.js)|Hard|
450451
565|[Array Nesting](./solutions/0565-array-nesting.js)|Medium|

solutions/0561-array-partition.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* 561. Array Partition
3+
* https://leetcode.com/problems/array-partition/
4+
* Difficulty: Easy
5+
*
6+
* Given an integer array nums of 2n integers, group these integers into n pairs (a1, b1),
7+
* (a2, b2), ..., (an, bn) such that the sum of min(ai, bi) for all i is maximized.
8+
* Return the maximized sum.
9+
*/
10+
11+
/**
12+
* @param {number[]} nums
13+
* @return {number}
14+
*/
15+
var arrayPairSum = function(nums) {
16+
return nums.sort((a, b) => a - b).reduce((sum, v, i, a) =>
17+
sum + (i % 2 === 0 ? Math.min(v, a[i + 1]) : 0), 0);
18+
};

0 commit comments

Comments
 (0)