Skip to content

Commit b39bcf2

Browse files
committed
Sync LeetCode submission Runtime - 353 ms (78.14%), Memory - 32.7 MB (40.08%)
1 parent 1634479 commit b39bcf2

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<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>
2+
3+
<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>
4+
5+
<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>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre>
11+
<strong>Input:</strong> nums = [10,1,2,7,1,3], p = 2
12+
<strong>Output:</strong> 1
13+
<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.
14+
The maximum difference is max(|nums[1] - nums[4]|, |nums[2] - nums[5]|) = max(0, 1) = 1. Therefore, we return 1.
15+
</pre>
16+
17+
<p><strong class="example">Example 2:</strong></p>
18+
19+
<pre>
20+
<strong>Input:</strong> nums = [4,2,1,2], p = 1
21+
<strong>Output:</strong> 0
22+
<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.
23+
</pre>
24+
25+
<p>&nbsp;</p>
26+
<p><strong>Constraints:</strong></p>
27+
28+
<ul>
29+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
30+
<li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
31+
<li><code>0 &lt;= p &lt;= (nums.length)/2</code></li>
32+
</ul>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution:
2+
def minimizeMax(self, nums: List[int], p: int) -> int:
3+
nums.sort()
4+
n = len(nums)
5+
6+
def countValidPairs(threshold):
7+
idx, count = 0, 0
8+
while idx < n - 1:
9+
# If a valid pair is found, skip both numbers
10+
if nums[idx + 1] - nums[idx] <= threshold:
11+
count += 1
12+
idx += 1
13+
idx += 1
14+
15+
return count
16+
17+
left, right = 0, nums[-1] - nums[0]
18+
while left < right:
19+
mid = left + (right - left) // 2
20+
21+
# If there are enough pairs, look for a smaller threshold.
22+
# Otherwise, look for a larger threshold.
23+
if countValidPairs(mid) >= p:
24+
right = mid
25+
else:
26+
left = mid + 1
27+
28+
return left
29+

0 commit comments

Comments
 (0)