2594. Minimum Time to Repair Cars
UnknownView on LeetCode
Problem Overview
Minimum Time to Repair Cars (Unknown) asks you to solve a structured algorithmic task. This is a common Array / Binary Search pattern in coding interviews. Binary search on time; mechanic with rank r fixes floor(sqrt(t/r)) cars; validate sum >= n.
A full step-by-step explanation is being added. See the study guide for pattern-based practice.
Approach
Binary search on time; mechanic with rank r fixes floor(sqrt(t/r)) cars; validate sum >= n.
Related patterns: Array, Binary Search
2594.cs
C#
// Approach: Binary search on time; mechanic with rank r fixes floor(sqrt(t/r)) cars; validate sum >= n.
// Time: O(n log(min * n²)) Space: O(1)
public class Solution
{
public long RepairCars(int[] ranks, int cars)
{
long l = 0;
long r = (long)ranks.Min() * cars * cars;
while (l < r)
{
long m = (l + r) / 2;
if (NumCarsFixed(ranks, m) >= cars)
r = m;
else
l = m + 1;
}
return l;
}
private long NumCarsFixed(int[] ranks, long minutes)
{
long carsFixed = 0;
foreach (var rank in ranks)
carsFixed += (long)Math.Sqrt(minutes / rank);
return carsFixed;
}
}Was this solution helpful?