-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathMinimize the Maximum Difference of Pairs.cpp
45 lines (35 loc) · 1.26 KB
/
Minimize the Maximum Difference of Pairs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
Solution by Rahul Surana
***********************************************************
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.
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.
Return the minimum maximum difference among all p pairs.
We define the maximum of an empty set to be zero.
***********************************************************
*/
class Solution {
public:
int minimizeMax(vector<int>& nums, int p) {
sort(nums.begin(),nums.end());
int s = 0, e = nums.back()-nums.front();
auto df = [&](int m){
int v = 0;
for(int j = 0; j < nums.size()-1; j++){
if(nums[j+1]-nums[j] <= m){
j++;
v++;
}
}
return v;
};
while(s < e){
int m = s+(e-s)/2;
if(df(m) >= p) e = m;
else s = m + 1;
}
return s;
}
};