classSolution: defcheck(self, m: int, d: int) -> bool: last = -1000000000 for t inself.position: if t - last >= d: m -= 1 ifnot m: returnTrue last = t returnFalse
defmaxDistance(self, position: List[int], m: int) -> int: position.sort() self.position = position l, r = 1, position[-1] - position[0] while l < r: mid = (l + r + 1) >> 1 ifself.check(m, mid): l = mid else: r = mid - 1 return l
privatebooleancheck(int m, int d) { intlast= -1000000000; for (int t : v) { if (t - last >= d) { if (--m == 0) { returntrue; } last = t; } } returnfalse; }
publicintmaxDistance(int[] position, int m) { Arrays.sort(position); v = position; intl=1, r = v[v.length - 1] - v[0]; while (l < r) { intmid= (l + r + 1) >> 1; if (check(m, mid)) { l = mid; } else { r = mid - 1; } } return l; } }
funccheck_MFB2B(v []int, m, d int)bool { last := -1000000000 for _, t := range v { if t - last >= d { m-- if m <= 0 { returntrue } last = t } } returnfalse }
funcmaxDistance(position []int, m int)int { slices.Sort(position) l, r := 1, position[len(position) - 1] - position[0] for l < r { mid := (l + r + 1) >> 1 if check_MFB2B(position, m, mid) { l = mid } else { r = mid - 1 } } return l }