LeetCode 2594. 修车的最少时间 C#
原题链接
中等
作者:
hpstory
,
2023-03-19 14:32:56
,
所有人可见
,
阅读 129
C# 代码
public class Solution {
public long RepairCars(int[] ranks, int cars) {
int n = ranks.Length;
long left = 1, right = long.MaxValue;
while (left < right){
long mid = left + (right - left >> 1);
if (Check(mid)) right = mid;
else left = mid + 1;
}
return left;
bool Check(long x){
long ans = 0;
for (int i = 0; i < n; i++){
ans += (long)Math.Sqrt(x / ranks[i]);
if (ans >= cars) return true;
}
return ans >= cars;
}
}
}