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 INPUT Mechanics Ranks Array 4 M1 2 M2 3 M3 1 M4 Cars to Repair 10 Time Formula time = rank × n² n = number of cars Example: Rank 1 repairs 4 cars 1 × 4² = 16 min ranks=[4,2,3,1], cars=10 ALGORITHM STEPS 1 Binary Search Setup low=1, high=rank[0]×cars² high = 4×10² = 400 2 Check Mid Time For each mechanic, count cars fixable in mid time: n = floor(sqrt(mid/r)) 3 Sum Total Cars If total >= cars needed: high = mid Else: low = mid + 1 4 Find Minimum When low == high, we found minimum time! Binary Search Range: [1...400] --> 16 FINAL RESULT Minimum Time Found 16 min Work Distribution M1 (r=4) 2 cars 4×2²=16 M2 (r=2) 2 cars 2×2²=8 M3 (r=3) 2 cars 3×2²=12 M4 (r=1) 4 cars 1×4²=16 Total: 10 cars [OK] Output: 16 Key Insight: Binary search on the answer! Since all mechanics work simultaneously, we search for the minimum time T where the total cars all mechanics can repair in time T is >= required cars. For time T and rank r: cars = sqrt(T/r). Time complexity: O(n × log(max_rank × cars²)) TutorialsPoint - Minimum Time to Repair Cars | Binary Search Approach
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