Skip to content

Commit 99157b2

Browse files
authored
Create Minimize the Maximum Difference of Pairs.py
1 parent 889ddb7 commit 99157b2

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'''
2+
You are given a 0-indexed integer array nums and an integer p. Find p pairs of indices of nums such that the maximum difference amongst all the pairs is minimized. Also, ensure no index appears more than once amongst the p pairs.
3+
4+
Note that for a pair of elements at the index i and j, the difference of this pair is |nums[i] - nums[j]|, where |x| represents the absolute value of x.
5+
6+
Return the minimum maximum difference among all p pairs. We define the maximum of an empty set to be zero.
7+
'''
8+
9+
'''
10+
Intuition
11+
To minimize the maximum difference amongst all the pairs, we need to find a maximum difference that satisfies the given condition of having p pairs of indices of nums. Since we want to minimize the maximum difference, we can use binary search to find the smallest maximum difference that satisfies the given condition.
12+
13+
Approach
14+
First, we sort the input list of integers. We define the minimum and maximum possible values for the maximum difference. The minimum value is 0 since the difference between any two elements is at least 0. The maximum value is the difference between the largest and smallest elements in the list since that is the maximum difference possible.
15+
16+
We perform binary search for the smallest maximum difference that satisfies the given condition. In each iteration, we calculate the mid-point of the possible range of maximum differences. We then count the number of pairs of adjacent integers with a difference less than or equal to the mid-point maximum difference. If the number of such pairs is greater than or equal to p, we decrease the maximum possible value of the maximum difference. Otherwise, we increase the minimum possible value of the maximum difference.
17+
18+
We continue this process until the minimum and maximum values converge to the same value, which is the smallest maximum difference that satisfies the given condition.
19+
'''
20+
21+
22+
23+
class Solution:
24+
def minimizeMax(self, nums: List[int], p: int) -> int:
25+
# Sort the input list of integers
26+
nums.sort()
27+
n = len(nums)
28+
29+
# Define the minimum and maximum possible values for the maximum difference
30+
min_max_diff = 0
31+
max_max_diff = nums[-1] - nums[0]
32+
33+
# Binary search for the smallest maximum difference that satisfies the given condition
34+
while min_max_diff < max_max_diff:
35+
mid_max_diff = (min_max_diff + max_max_diff) // 2
36+
37+
# Count the number of pairs of adjacent integers with a difference less than or equal to mid_max_diff
38+
pair_count = 0
39+
i = 1
40+
while i < n:
41+
if nums[i] - nums[i-1] <= mid_max_diff:
42+
pair_count += 1
43+
i += 1
44+
i += 1
45+
46+
# If the number of such pairs is greater than or equal to p, decrease the maximum possible value of the maximum difference
47+
if pair_count >= p:
48+
max_max_diff = mid_max_diff
49+
# Otherwise, increase the minimum possible value of the maximum difference
50+
else:
51+
min_max_diff = mid_max_diff + 1
52+
53+
# Return the smallest maximum difference that satisfies the given condition
54+
return min_max_diff

0 commit comments

Comments
 (0)