难度:中等
思路源于题目标签 “二分”:
- 二分的上界应该是所有车都给修车能力值最小的人修,下界我设为0
- 每次搜索时判断当前时间下,每位机械工总共能修 n 辆车,n >= cars 则右边界左移,否则左边界右移
class Solution:def repairCars(self, ranks: List[int], cars: int) -> int:l, r = 0, min(ranks) * (cars ** 2)while l < r:mid = (l + r) >> 1n = 0for i in ranks:n += int((mid//i) ** 0.5)if n >= cars:breakif n >= cars:r = midelse:l = mid + 1return r
if n >= cars:break 提前结束循环可以快很多很多
官解一句话完成转移判断
sum([floor(sqrt(m // x)) for x in ranks]) >= cars