|
| 1 | +# [2616. Minimize the Maximum Difference of Pairs](https://leetcode.com/problems/minimize-the-maximum-difference-of-pairs) |
| 2 | + |
| 3 | +## Description |
| 4 | + |
| 5 | +<div class="elfjS" data-track-load="description_content"><p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>p</code>. Find <code>p</code> pairs of indices of <code>nums</code> such that the <strong>maximum</strong> difference amongst all the pairs is <strong>minimized</strong>. Also, ensure no index appears more than once amongst the <code>p</code> pairs.</p> |
| 6 | + |
| 7 | +<p>Note that for a pair of elements at the index <code>i</code> and <code>j</code>, the difference of this pair is <code>|nums[i] - nums[j]|</code>, where <code>|x|</code> represents the <strong>absolute</strong> <strong>value</strong> of <code>x</code>.</p> |
| 8 | + |
| 9 | +<p>Return <em>the <strong>minimum</strong> <strong>maximum</strong> difference among all </em><code>p</code> <em>pairs.</em> We define the maximum of an empty set to be zero.</p> |
| 10 | + |
| 11 | +<p> </p> |
| 12 | +<p><strong class="example">Example 1:</strong></p> |
| 13 | + |
| 14 | +<pre><strong>Input:</strong> nums = [10,1,2,7,1,3], p = 2 |
| 15 | +<strong>Output:</strong> 1 |
| 16 | +<strong>Explanation:</strong> The first pair is formed from the indices 1 and 4, and the second pair is formed from the indices 2 and 5. |
| 17 | +The maximum difference is max(|nums[1] - nums[4]|, |nums[2] - nums[5]|) = max(0, 1) = 1. Therefore, we return 1. |
| 18 | +</pre> |
| 19 | + |
| 20 | +<p><strong class="example">Example 2:</strong></p> |
| 21 | + |
| 22 | +<pre><strong>Input:</strong> nums = [4,2,1,2], p = 1 |
| 23 | +<strong>Output:</strong> 0 |
| 24 | +<strong>Explanation:</strong> Let the indices 1 and 3 form a pair. The difference of that pair is |2 - 2| = 0, which is the minimum we can attain. |
| 25 | +</pre> |
| 26 | + |
| 27 | +<p> </p> |
| 28 | +<p><strong>Constraints:</strong></p> |
| 29 | + |
| 30 | +<ul> |
| 31 | + <li><code>1 <= nums.length <= 10<sup>5</sup></code></li> |
| 32 | + <li><code>0 <= nums[i] <= 10<sup>9</sup></code></li> |
| 33 | + <li><code>0 <= p <= (nums.length)/2</code></li> |
| 34 | +</ul> |
| 35 | +</div> |
| 36 | + |
| 37 | +<p> </p> |
| 38 | + |
| 39 | +## Solutions |
| 40 | + |
| 41 | +**Solution: `Binary Search + Greedy`** |
| 42 | + |
| 43 | +- Time complexity: <em>O(nlogn+nlog(maxDiff))</em> |
| 44 | +- Space complexity: <em>O(1)</em> |
| 45 | + |
| 46 | +<p> </p> |
| 47 | + |
| 48 | +### **JavaScript** |
| 49 | + |
| 50 | +```js |
| 51 | +/** |
| 52 | + * @param {number[]} nums |
| 53 | + * @param {number} p |
| 54 | + * @return {number} |
| 55 | + */ |
| 56 | +const minimizeMax = function (nums, p) { |
| 57 | + const n = nums.length; |
| 58 | + |
| 59 | + nums.sort((a, b) => a - b); |
| 60 | + |
| 61 | + let left = 0; |
| 62 | + let right = nums[n - 1] - nums[0]; |
| 63 | + |
| 64 | + const pairsCount = diff => { |
| 65 | + let index = 1; |
| 66 | + let count = 0; |
| 67 | + |
| 68 | + while (index < n) { |
| 69 | + const current = nums[index] - nums[index - 1]; |
| 70 | + |
| 71 | + if (current <= diff) { |
| 72 | + count += 1; |
| 73 | + index += 2; |
| 74 | + } else { |
| 75 | + index += 1; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return count; |
| 80 | + }; |
| 81 | + |
| 82 | + while (left < right) { |
| 83 | + const mid = Math.floor((left + right) / 2); |
| 84 | + |
| 85 | + pairsCount(mid) >= p ? (right = mid) : (left = mid + 1); |
| 86 | + } |
| 87 | + |
| 88 | + return left; |
| 89 | +}; |
| 90 | +``` |
0 commit comments