-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
40 lines (37 loc) · 832 Bytes
/
Solution.cs
File metadata and controls
40 lines (37 loc) · 832 Bytes
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
public class Solution
{
public int MinimizeMax(int[] nums, int p)
{
if (p == 0) return 0;
Array.Sort(nums);
int max = nums[^1], min = nums[0];
int low = 0, high = max - min, ans = high;
while (low <= high)
{
int mid = low + (high - low) / 2;
if (Ok(nums, mid, p))
{
ans = mid;
high = mid - 1;
}
else
{
low = mid + 1;
}
}
return ans;
}
bool Ok(int[] nums, int mid, int p)
{
int n = nums.Length;
for (int i = 1; i < n && p > 0; i++)
{
if (nums[i] - nums[i - 1] <= mid)
{
p--;
i++;
}
}
return p == 0;
}
}