> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Minimum Time to Repair Cars Python Solution

> Tested Python solution for LeetCode 2594 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 2594, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Binary Search](/catalog/topics/binary-search). [View on LeetCode](https://leetcode.com/problems/minimum-time-to-repair-cars/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2594   # by problem number
lcpy gen -s minimum_time_to_repair_cars   # by problem name
```

## Problem

You are given an integer array \<code>ranks\</code> representing the \<strong>ranks\</strong> of some mechanics. \<code>ranks\[i]\</code> is the rank of the \<code>i\<sup>th\</sup>\</code> mechanic. A mechanic with a rank \<code>r\</code> can repair \<code>n\</code> cars in \<code>r \* n\<sup>2\</sup>\</code> minutes.

You are also given an integer \<code>cars\</code> representing the total number of cars waiting in the garage to be repaired.

Return \<em>the \<strong>minimum\</strong> time taken to repair all the cars.\</em>

\<strong>Note:\</strong> All the mechanics can repair the cars simultaneously.

### Examples

```
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.
```

```
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 \<= 10\<sup>5\</sup>
* 1 \<= ranks\[i] \<= 100
* 1 \<= cars \<= 10\<sup>6\</sup>

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_time_to_repair_cars/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_time_to_repair_cars/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from math import isqrt


class Solution:
    # Time: O(m * log(min(ranks) * cars^2)) where m = len(ranks)
    # Space: O(1)
    def repair_cars(self, ranks: list[int], cars: int) -> int:
        lo, hi = 1, min(ranks) * cars * cars
        while lo < hi:
            mid = (lo + hi) // 2
            if sum(isqrt(mid // r) for r in ranks) >= cars:
                hi = mid
            else:
                lo = mid + 1
        return lo
```

## Complexity

| Time | Space |
| - | - |
| O(m \* log(min(ranks) \* cars^2)) where m = len(ranks) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.