class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
def check(x: int) -> bool:
pre = position[0]
cnt = 1
for i in range(1, len(position)):
if position[i] - pre >= x:
pre = position[i]
cnt += 1
return cnt >= m
position.sort()
left, right, ans = 1, position[-1] - position[0], -1
while left <= right:
mid = (left + right) // 2;
if check(mid):
ans = mid
left = mid + 1
else:
right = mid - 1
return ans
两球之间的磁力(中等题)
最新推荐文章于 2024-09-15 15:43:59 发布