Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 72e95a8

Browse files
hot9cupsignacio-chiazzo
authored andcommitted
Added Minimize Maximum Pair Sum in Array
- Added Minimize Maximum Pair Sum in Array and corresponding tests.
1 parent 69ee702 commit 72e95a8

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Minimize Maximum Pair Sum in Array
3+
https://leetcode.com/problems/minimize-maximum-pair-sum-in-array/description/
4+
5+
The pair sum of a pair (a,b) is equal to a + b. The maximum pair sum is the largest pair sum in a list of pairs.
6+
7+
For example, if we have pairs (1,5), (2,3), and (4,4), the maximum pair sum would be max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8.
8+
Given an array nums of even length n, pair up the elements of nums into n / 2 pairs such that:
9+
10+
Each element of nums is in exactly one pair, and
11+
The maximum pair sum is minimized.
12+
Return the minimized maximum pair sum after optimally pairing up the elements.
13+
14+
15+
16+
Example 1:
17+
18+
Input: nums = [3,5,2,3]
19+
Output: 7
20+
Explanation: The elements can be paired up into pairs (3,3) and (5,2).
21+
The maximum pair sum is max(3+3, 5+2) = max(6, 7) = 7.
22+
23+
24+
Example 2:
25+
26+
Input: nums = [3,5,4,2,4,6]
27+
Output: 8
28+
Explanation: The elements can be paired up into pairs (3,5), (4,4), and (6,2).
29+
The maximum pair sum is max(3+5, 4+4, 6+2) = max(8, 8, 8) = 8.
30+
*/
31+
32+
/**
33+
* @param {number[]} nums
34+
* @return {number}
35+
*/
36+
var minPairSum = function(nums) {
37+
nums.sort((a, b) => a-b);
38+
let i = 0, j = nums.length - 1;
39+
let max = -Infinity;
40+
while (i < j) {
41+
max = Math.max(max, nums[i++] + nums[j--]);
42+
}
43+
return max;
44+
};
45+
46+
module.exports.minPairSum = minPairSum;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const assert = require('assert');
2+
const minPairSum = require('../../LeetcodeProblems/Algorithms/Minimize_Maximum_Pair_Sum_In_Array').minPairSum;
3+
4+
var test = function () {
5+
assert.equal(minPairSum([3,5,2,3]), 7);
6+
assert.equal(minPairSum([3,5,4,2,4,6]), 8);
7+
assert.equal(minPairSum([1,1,1,1]), 2);
8+
}
9+
10+
module.exports.test = test;

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ To run a specific problem in your console run `node <problem_file_path>` (e.g.
6666
| [Time Needed to Rearrange a Binary String](/LeetcodeProblems/Algorithms/Time_Needed_Rearrange_Binary_String.js)| Medium | https://leetcode.com/problems/time-needed-to-rearrange-a-binary-string/ |
6767
| [Find Subarrays With Equal Sum ](/LeetcodeProblems/Algorithms/Find_Subarrays_With_Equal_Sums.js) | Medium | https://leetcode.com/problems/find-subarrays-with-equal-sum/ |
6868
| [Reverse Integer](/LeetcodeProblems/Algorithms/Reverse_Integer.js) | Medium | https://leetcode.com/problems/reverse-integer/ |
69+
| [Minimize Maximum Pair Sum in Array ](/LeetcodeProblems/Algorithms/Minimize_Maximum_Pair_Sum_In_Array.js) | Medium | https://leetcode.com/problems/minimize-maximum-pair-sum-in-array/ |
6970
| [Flood Fill ](/LeetcodeProblems/Algorithms/Flood_Fill.js) | Easy | https://leetcode.com/problems/flood-fill/ |
7071
| [Implement stack using queues ](/LeetcodeProblems/Algorithms/Implement_stack_using_queues.js) | Easy | https://leetcode.com/problems/implement-stack-using-queues/ |
7172
| [Number of Segments in a String ](/LeetcodeProblems/Algorithms/Number_of_Segments_in_a_String.js) | Easy | https://leetcode.com/problems/number-of-segments-in-a-string/ |

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /