Minimum Time to Repair Cars [C++]
IntuitionThe problem requires us to find the minimum time needed to repair a given number of cars using mechanics with different repair speeds. Each mechanic's
# Intuition
The problem requires us to find the minimum time needed to repair a given number of cars using mechanics with different repair speeds. Each mechanic's repair time follows a quadratic pattern based on their rank. Since time is a monotonic function in this problem, we can apply **binary search** on time to efficiently determine the minimum time required.

# Approach
1. **Binary Search on Time:**  
   - The minimum possible time is `1` (theoretically), and the maximum possible time is determined by the slowest mechanic (`min(rank) * cars^2`).
   - We search within this range using binary search.

2. **Check Feasibility (`canRepair` function):**  
   - Given a time `T`, we determine if it's possible to repair all cars within this time.
   - Each mechanic can repair `floor(sqrt(T / rank))` cars in `T` time.
   - If the total repaired cars meet or exceed the required number, `T` is feasible.

3. **Narrow the Search Space:**  
   - If the given `mid` time is sufficient, move left (`right = mid`).
   - Otherwise, move right (`left = mid + 1`).
   - The final result is stored in `left`, which holds the minimum possible time.

# Complexity
- **Time complexity:**  
  - The binary search runs in **O(log T)**, where `T = min(rank) * cars^2` (worst-case upper bound).
  - Checking if a time `T` is feasible runs in **O(n)** (iterating over `ranks`).
  - Overall complexity: **O(n log T)**.

- **Space complexity:**  
  - **O(1)** since only a few extra variables are used.

# Code
```cpp
class Solution {
public:
    long long repairCars(vector<int>& ranks, int cars) {
        long long left = 1, right = (long long)*min_element(ranks.begin(), ranks.end()) * cars * cars;
        
        auto canRepair = [&](long long time) {
            long long repaired = 0;
            for (int r : ranks) {
                repaired += (long long)sqrt(time / r);
                if (repaired >= cars) return true;
            }
            return false;
        };
        
        while (left < right) {
            long long mid = left + (right - left) / 2;
            if (canRepair(mid)) right = mid;
            else left = mid + 1;
        }
        
        return left;
    }
};
```
['C++']