2594. Minimum Time to Repair Cars LeetCode Solution

In this guide, you will get 2594. Minimum Time to Repair Cars LeetCode Solution with the best time and space complexity. The solution to Minimum Time to Repair Cars problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Minimum Time to Repair Cars solution in C++
  4. Minimum Time to Repair Cars solution in Java
  5. Minimum Time to Repair Cars solution in Python
  6. Additional Resources
2594. Minimum Time to Repair Cars LeetCode Solution image

Problem Statement of 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.

Example 1:

Input: ranks = [4,2,3,1], cars = 10
Output: 16
Explanation:
– The first mechanic will repair two cars. The time required is 4 * 2 * 2 = 16 minutes.
– The second mechanic will repair two cars. The time required is 2 * 2 * 2 = 8 minutes.
– The third mechanic will repair two cars. The time required is 3 * 2 * 2 = 12 minutes.
– The fourth mechanic will repair four cars. The time required is 1 * 4 * 4 = 16 minutes.
It can be proved that the cars cannot be repaired in less than 16 minutes.​​​​​

Example 2:

Input: ranks = [5,1,8], cars = 6
Output: 16
Explanation:
– The first mechanic will repair one car. The time required is 5 * 1 * 1 = 5 minutes.
– The second mechanic will repair four cars. The time required is 1 * 4 * 4 = 16 minutes.
– The third mechanic will repair one car. The time required is 8 * 1 * 1 = 8 minutes.
It can be proved that the cars cannot be repaired in less than 16 minutes.​​​​​

Constraints:

1 <= ranks.length <= 105
1 <= ranks[i] <= 100
1 <= cars <= 106

Complexity Analysis

  • Time Complexity: O(\log (\min(\texttt{ranks}) \cdot \texttt{cars}^2))
  • Space Complexity: O(1)

2594. Minimum Time to Repair Cars LeetCode Solution in C++

class Solution {
 public:
  long long repairCars(vector<int>& ranks, int cars) {
    long l = 0;
    long r = static_cast<long>(ranges::min(ranks)) * cars * cars;

    while (l < r) {
      const long m = (l + r) / 2;
      if (numCarsFixed(ranks, m) >= cars)
        r = m;
      else
        l = m + 1;
    }

    return l;
  }

 private:
  long numCarsFixed(const vector<int>& ranks, long minutes) {
    long carsFixed = 0;
    //    r * n^2 = minutes
    // -> n = sqrt(minutes / r)
    for (const int rank : ranks)
      carsFixed += sqrt(minutes / rank);
    return carsFixed;
  }
};
/* code provided by PROGIEZ */

2594. Minimum Time to Repair Cars LeetCode Solution in Java

class Solution {
  public long repairCars(int[] ranks, int cars) {
    long l = 0;
    long r = (long) Arrays.stream(ranks).min().getAsInt() * cars * cars;

    while (l < r) {
      final 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;
    //    r * n^2 = minutes
    // -> n = sqrt(minutes / r)
    for (final int rank : ranks)
      carsFixed += Math.sqrt(minutes / rank);
    return carsFixed;
  }
}
// code provided by PROGIEZ

2594. Minimum Time to Repair Cars LeetCode Solution in Python

class Solution:
  def repairCars(self, ranks: list[int], cars: int) -> int:
    def numCarsFixed(minutes: int) -> int:
      #    r * n^2 = minutes
      # -> n = sqrt(minutes / r)
      return sum(math.isqrt(minutes // rank) for rank in ranks)

    return bisect.bisect_left(range(min(ranks) * cars**2), cars,
                              key=numCarsFixed)
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.