[LeetCode] Minimum Time to Repair Cars

2594. Minimum Time to Repair Cars

You are given an integer array ranks representing the ranks of some mechanics. ranksi is the rank of the ith mechanic. A mechanic with a rank r can repair n cars in r * n2 minutes.

You are also given an integer cars representing the total number of cars waiting in the garage to be repaired.

Return the minimum time taken to repair all the cars.

Note: All the mechanics can repair the cars simultaneously.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
bool helper(vector<int>& A, int c, long long m) {
for(int i = 0; i < A.size(); i++) {
long long x = sqrt(m / A[i]);
c -= x;
if(c <= 0) break;
}
return c <= 0;
}
public:
long long repairCars(vector<int>& ranks, int cars) {
long long l = 0, r = LLONG_MAX, res = LLONG_MAX;
while(l <= r) {
long long m = l + (r - l) / 2;
bool ok = helper(ranks, cars, m);
if(ok) {
res = min(res, m);
r = m - 1;
} else l = m + 1;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/03/18/PS/LeetCode/minimum-time-to-repair-cars/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.