Skip to content

Commit

Permalink
New Problem Solution - "1877. Minimize Maximum Pair Sum in Array"
Browse files Browse the repository at this point in the history
  • Loading branch information
haoel committed Nov 12, 2021
1 parent 8055bef commit ab313d1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ LeetCode
|1882|[Process Tasks Using Servers](https://leetcode.com/problems/process-tasks-using-servers/) | [C++](./algorithms/cpp/processTasksUsingServers/ProcessTasksUsingServers.cpp)|Medium|
|1881|[Maximum Value after Insertion](https://leetcode.com/problems/maximum-value-after-insertion/) | [C++](./algorithms/cpp/maximumValueAfterInsertion/MaximumValueAfterInsertion.cpp)|Medium|
|1880|[Check if Word Equals Summation of Two Words](https://leetcode.com/problems/check-if-word-equals-summation-of-two-words/) | [C++](./algorithms/cpp/checkIfWordEqualsSummationOfTwoWords/CheckIfWordEqualsSummationOfTwoWords.cpp)|Easy|
|1877|[Minimize Maximum Pair Sum in Array](https://leetcode.com/problems/minimize-maximum-pair-sum-in-array/) | [C++](/Users/chenhao/GitHub/leetcode/minimizeMaximumPairSumInArray/MinimizeMaximumPairSumInArray.cpp)|Medium|
|1876|[Substrings of Size Three with Distinct Characters](https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/submissions/) | [C++](/Users/chenhao/GitHub/leetcode/substringsOfSizeThreeWithDistinctCharacters/SubstringsOfSizeThreeWithDistinctCharacters.cpp)|Easy|
|1871|[Jump Game VII](https://leetcode.com/problems/jump-game-vii/) | [C++](./algorithms/cpp/jumpGame/jumpGame.VII.cpp)|Medium|
|1870|[Minimum Speed to Arrive on Time](https://leetcode.com/problems/minimum-speed-to-arrive-on-time/) | [C++](./algorithms/cpp/minimumSpeedToArriveOnTime/MinimumSpeedToArriveOnTime.cpp)|Medium|
Expand Down
53 changes: 53 additions & 0 deletions minimizeMaximumPairSumInArray/MinimizeMaximumPairSumInArray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Source : https://leetcode.com/problems/minimize-maximum-pair-sum-in-array/
// Author : Hao Chen
// Date : 2021-11-12

/*****************************************************************************************************
*
* 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.
*
* 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.
*
* Given an array nums of even length n, pair up the elements of nums into n / 2 pairs such that:
*
* Each element of nums is in exactly one pair, and
* The maximum pair sum is minimized.
*
* Return the minimized maximum pair sum after optimally pairing up the elements.
*
* Example 1:
*
* Input: nums = [3,5,2,3]
* Output: 7
* Explanation: The elements can be paired up into pairs (3,3) and (5,2).
* The maximum pair sum is max(3+3, 5+2) = max(6, 7) = 7.
*
* Example 2:
*
* Input: nums = [3,5,4,2,4,6]
* Output: 8
* Explanation: The elements can be paired up into pairs (3,5), (4,4), and (6,2).
* The maximum pair sum is max(3+5, 4+4, 6+2) = max(8, 8, 8) = 8.
*
* Constraints:
*
* n == nums.length
* 2 <= n <= 10^5
* n is even.
* 1 <= nums[i] <= 10^5
******************************************************************************************************/

class Solution {
public:
int minPairSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
int i = 0, j = nums.size() - 1;
int m = 0;
for(int i = 0, j = nums.size() - 1; i < j; i++,j--) {
m = max(m, nums[i] + nums[j]);
}
return m;
}
};

0 comments on commit ab313d1

Please sign in to comment.