Skip to content

Commit 32060d5

Browse files
committed
feat: add solution 2616. Minimize the Maximum Difference of Pairs
1 parent 9403170 commit 32060d5

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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>&nbsp;</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>&nbsp;</p>
28+
<p><strong>Constraints:</strong></p>
29+
30+
<ul>
31+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
32+
<li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
33+
<li><code>0 &lt;= p &lt;= (nums.length)/2</code></li>
34+
</ul>
35+
</div>
36+
37+
<p>&nbsp;</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>&nbsp;</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+
```
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} p
4+
* @return {number}
5+
*/
6+
const minimizeMax = function (nums, p) {
7+
const n = nums.length;
8+
9+
nums.sort((a, b) => a - b);
10+
11+
let left = 0;
12+
let right = nums[n - 1] - nums[0];
13+
14+
const pairsCount = diff => {
15+
let index = 1;
16+
let count = 0;
17+
18+
while (index < n) {
19+
const current = nums[index] - nums[index - 1];
20+
21+
if (current <= diff) {
22+
count += 1;
23+
index += 2;
24+
} else {
25+
index += 1;
26+
}
27+
}
28+
29+
return count;
30+
};
31+
32+
while (left < right) {
33+
const mid = Math.floor((left + right) / 2);
34+
35+
pairsCount(mid) >= p ? (right = mid) : (left = mid + 1);
36+
}
37+
38+
return left;
39+
};

0 commit comments

Comments
 (0)