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
Minimum Time to Repair Cars: Optimal Work DistributionRank 4Slower2 carsRank 2Fast2 carsRank 3Medium2 carsRank 1Fastest4 carsTotal Cars102+2+2+4=10Formula: mechanic with rank r repairs n cars in r × n² minutesAt time t, mechanic with rank r can repair ⌊√(t/r)⌋ carsOptimal Time: 16 minutesAll mechanics work simultaneously to minimize total time
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
Asked in
Google 35 Amazon 28 Microsoft 22 Facebook 18
68.4K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen