-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathSolution1552.java
37 lines (29 loc) · 1003 Bytes
/
Solution1552.java
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
package leetcode.binarysearch.greedyalgorithm;
import java.util.Arrays;
public class Solution1552 {
public static void main(String[] args) {
// System.out.println(new Solution1552().maxDistance(new int[]{5, 4, 3, 2, 1, 1000000000}, 2));
System.out.println(new Solution1552().maxDistance(new int[]{1, 2, 3, 4, 7}, 3));
}
public int maxDistance(int[] position, int m) {
Arrays.sort(position);
int left = 1, right = position[position.length - 1] - position[0] + 1;
while (left < right) {
int mid = left + right >> 1;
int l = 0;
int count = 1;
for (int i = 1; i < position.length; i++) {
if (position[i] - position[l] >= mid) {
count++;
l = i;
}
}
if (count < m) {
right = mid;
} else {
left = mid + 1;
}
}
return left - 1;
}
}