{"id":8503,"date":"2021-08-06T23:23:24","date_gmt":"2021-08-07T06:23:24","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8503"},"modified":"2021-08-06T23:23:45","modified_gmt":"2021-08-07T06:23:45","slug":"leetcode-1877-minimize-maximum-pair-sum-in-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-1877-minimize-maximum-pair-sum-in-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1877. Minimize Maximum Pair Sum in Array"},"content":{"rendered":"\n<p>The&nbsp;<strong>pair sum<\/strong>&nbsp;of a pair&nbsp;<code>(a,b)<\/code>&nbsp;is equal to&nbsp;<code>a + b<\/code>. The&nbsp;<strong>maximum pair sum<\/strong>&nbsp;is the largest&nbsp;<strong>pair sum<\/strong>&nbsp;in a list of pairs.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For example, if we have pairs&nbsp;<code>(1,5)<\/code>,&nbsp;<code>(2,3)<\/code>, and&nbsp;<code>(4,4)<\/code>, the&nbsp;<strong>maximum pair sum<\/strong>&nbsp;would be&nbsp;<code>max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8<\/code>.<\/li><\/ul>\n\n\n\n<p>Given an array&nbsp;<code>nums<\/code>&nbsp;of&nbsp;<strong>even<\/strong>&nbsp;length&nbsp;<code>n<\/code>, pair up the elements of&nbsp;<code>nums<\/code>&nbsp;into&nbsp;<code>n \/ 2<\/code>&nbsp;pairs such that:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Each element of&nbsp;<code>nums<\/code>&nbsp;is in&nbsp;<strong>exactly one<\/strong>&nbsp;pair, and<\/li><li>The&nbsp;<strong>maximum pair sum&nbsp;<\/strong>is&nbsp;<strong>minimized<\/strong>.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the minimized&nbsp;<strong>maximum pair sum<\/strong>&nbsp;after optimally pairing up the elements<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [3,5,2,3]\n<strong>Output:<\/strong> 7\n<strong>Explanation:<\/strong> The elements can be paired up into pairs (3,3) and (5,2).\nThe maximum pair sum is max(3+3, 5+2) = max(6, 7) = 7.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [3,5,4,2,4,6]\n<strong>Output:<\/strong> 8\n<strong>Explanation:<\/strong> The elements can be paired up into pairs (3,5), (4,4), and (6,2).\nThe maximum pair sum is max(3+5, 4+4, 6+2) = max(8, 8, 8) = 8.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == nums.length<\/code><\/li><li><code>2 &lt;= n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>n<\/code>&nbsp;is&nbsp;<strong>even<\/strong>.<\/li><li><code>1 &lt;= nums[i] &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy<\/strong><\/h2>\n\n\n\n<p>Sort the elements, pair nums[i] with nums[n &#8211; i &#8211; 1] and find the max pair.<\/p>\n\n\n\n<p>Time complexity: O(nlogn) -&gt; O(n) counting sort.<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int minPairSum(vector<int>& nums) {\n    const int n = nums.size();\n    int ans = 0;\n    sort(begin(nums), end(nums));\n    for (int i = 0; i < n \/ 2; ++i)\n      ans = max(ans, nums[i] + nums[n - i - 1]);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The&nbsp;pair sum&nbsp;of a pair&nbsp;(a,b)&nbsp;is equal to&nbsp;a + b. The&nbsp;maximum pair sum&nbsp;is the largest&nbsp;pair sum&nbsp;in a list of pairs. For example, if we have pairs&nbsp;(1,5),&nbsp;(2,3), and&nbsp;(4,4),&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[20,88,177,148],"class_list":["post-8503","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-array","tag-greedy","tag-medium","tag-pair","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8503","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8503"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8503\/revisions"}],"predecessor-version":[{"id":8505,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8503\/revisions\/8505"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}