20250613 finished.
This commit is contained in:
parent
52d9629a5c
commit
b70046b7da
|
@ -0,0 +1,60 @@
|
||||||
|
/**
|
||||||
|
* [2616] Minimize the Maximum Difference of Pairs
|
||||||
|
*/
|
||||||
|
#include <bits/stdc++.h>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
|
||||||
|
// submission codes start here
|
||||||
|
|
||||||
|
class Solution
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
int minimizeMax(vector<int> &nums, int p)
|
||||||
|
{
|
||||||
|
ranges::sort(nums);
|
||||||
|
|
||||||
|
auto check = [&](int value) -> bool
|
||||||
|
{
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < nums.size() - 1; ++i)
|
||||||
|
{
|
||||||
|
if (nums[i + 1] - nums[i] <= value)
|
||||||
|
{
|
||||||
|
count += 1;
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return count >= p;
|
||||||
|
};
|
||||||
|
|
||||||
|
int left = 0, right = nums.back() - nums[0];
|
||||||
|
|
||||||
|
while (left < right)
|
||||||
|
{
|
||||||
|
int middle = (left + right) >> 1;
|
||||||
|
if (check(middle))
|
||||||
|
{
|
||||||
|
right = middle;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
left = middle + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return left;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// submission codes end.
|
||||||
|
|
||||||
|
TEST(P2616, Test1)
|
||||||
|
{
|
||||||
|
vector nums = {10, 1, 2, 7, 1, 3};
|
||||||
|
Solution s;
|
||||||
|
ASSERT_EQ(s.minimizeMax(nums, 2), 1);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user