Beats 92% - well explained solution
IntuitionThe problem requires us to find the minimum time needed to repair all cars, given that multiple mechanics can work in parallel and each mechanic's repa
# Intuition
The problem requires us to find the **minimum time** needed to repair all cars, given that multiple mechanics can work in parallel and each mechanic's repair speed is determined by their rank.

A **brute-force approach** would be to iterate minute by minute, distributing cars among mechanics, but this would be too slow. Instead, we can use **binary search** on the minimum time required to efficiently find the optimal solution.

# Approach
1. **Determine the Search Space**:
   - The **minimum possible time (`l`)** is the time taken by the fastest mechanic to repair a single car (`min(ranks)`).
   - The **maximum possible time (`r`)** can be determined by considering the scenario where the fastest mechanic repairs all the cars alone. This can be estimated as `min(ranks) * cars^2`. Although the actual time might extend beyond this, this value is guaranteed to be a feasible upper bound, making it a reasonable choice for our binary search limit.

2. **Binary Search on Time**:
   - We perform binary search on `minutes`, the total time required.
   - If it's **possible** to repair at least `cars` within `mid` minutes, update `ans = mid` and search for a smaller time (`r = mid - 1`).
   - Otherwise, increase the lower bound (`l = mid + 1`).

3. **Feasibility Check (`isPossible` function)**:
   - Each mechanic can repair `floor(sqrt(minutes / rank))` cars in `minutes` time.
   - We sum up the number of cars repaired by all mechanics and check if it meets or exceeds `cars`.

This **binary search approach** efficiently finds the minimum required time in **logarithmic time** instead of iterating over all possibilities.

# Complexity
- **Time complexity:**  
  - **Binary search** runs in **$$O(\log T)$$**, where `T` is the upper bound of time (`min(ranks) * cars^2`).
  - The `isPossible` function runs in **$$O(n)$$** for each binary search step.
  - Thus, the total complexity is **$$O(n \log T)$$**.

- **Space complexity:**  
  - We use only a few integer and long variables, so the space complexity is **$$O(1)$$** (constant space).

# Code
```java
class Solution {
    public long repairCars(int[] ranks, int cars) {
        long minVal = 10000000;
        
        // Find the minimum rank
        for (int rank : ranks) {
            minVal = Math.min(minVal, rank);
        }
        
        long l = minVal, r = minVal * (long) cars * cars, mid, ans = 0;
        
        // Binary search for the minimum required time
        while (l <= r) {
            mid = (l + r) / 2;
            if (isPossible(ranks, cars, mid)) {
                ans = mid;  // Update answer
                r = mid - 1; // Try to find a smaller time
            } else {
                l = mid + 1;
            }
        }
        
        return ans;
    }
    
    // Function to check if all cars can be repaired within 'minutes'
    public boolean isPossible(int[] ranks, int cars, long minutes) {
        for (int rank : ranks) {
            cars -= (int) Math.sqrt(minutes / rank);
            if (cars <= 0) {
                return true;  // Successfully repaired all cars
            }
        }
        
        return false; // Not enough cars repaired
    }
}

['Binary Search', 'Java']