Minimum Time to Repair Cars - Problem
You are given an integer array ranks representing the ranks of some mechanics. ranks[i] is the rank of the ith mechanic.
A mechanic with a rank r can repair n cars in r × n² 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.
Input & Output
Example 1 — Basic Case
$
Input:
ranks = [4,2,3,1], cars = 10
›
Output:
16
💡 Note:
At time 16: mechanic 1 repairs √(16/4)=2 cars, mechanic 2 repairs √(16/2)=2√2≈2 cars, mechanic 3 repairs √(16/3)≈2 cars, mechanic 4 repairs √(16/1)=4 cars. Total = 2+2+2+4 = 10 cars exactly.
Example 2 — Single Mechanic
$
Input:
ranks = [1], cars = 1
›
Output:
1
💡 Note:
Only one mechanic with rank 1 needs to repair 1 car. Time needed = 1 × 1² = 1 minute.
Example 3 — Multiple Cars per Mechanic
$
Input:
ranks = [5,1,8], cars = 6
›
Output:
16
💡 Note:
At time 16: mechanic 1 repairs √(16/5)≈1 car, mechanic 2 repairs √(16/1)=4 cars, mechanic 3 repairs √(16/8)≈1 car. Total = 1+4+1 = 6 cars.
Constraints
- 1 ≤ ranks.length ≤ 105
- 1 ≤ ranks[i] ≤ 100
- 1 ≤ cars ≤ 106
Visualization
Tap to expand
Understanding the Visualization
1
Input
Mechanics with ranks [4,2,3,1] need to repair 10 cars
2
Process
Each mechanic can repair cars according to formula: time = rank × n²
3
Output
Find minimum time when all mechanics work together
Key Takeaway
🎯 Key Insight: Use binary search on answer space since the problem has monotonic property - if time T works, then T+1 also works
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code