Optimal Time to Repair Cars Using Binary Search
IntuitionThe goal of the problem is to find the minimum time required to repair all cars by distributing them optimally among the mechanics. Each mechanic with
## Intuition
The goal of the problem is to find the minimum time required to repair all cars by distributing them optimally among the mechanics. Each mechanic with rank `r` can repair `n` cars in `r * n^2` minutes. Since the repair time increases quadratically, we can use binary search to efficiently find the optimal time.

## Approach
1. **Define the search range**
   - The minimum possible time is `1` because at least one car must be repaired.
   - The maximum possible time is `ranks[0] * cars^2`, which assumes the slowest mechanic repairs all cars alone.

2. **Use binary search to find the optimal time**
   - Compute the mid value (`(min + max) / 2`) and check if it is possible to repair all cars within this time.
   - For each mechanic, calculate the number of cars they can repair within this time using `Math.floor(Math.sqrt(mid / rank))`.
   - If the total number of repaired cars is sufficient, update `max = mid - 1`; otherwise, update `min = mid + 1`.
   - The search ends when the smallest possible time is found.

## Complexity
- **Time Complexity:** $$O(n \log T)$$ 
  - `T` is the maximum possible time (`ranks[0] * cars^2`).
  - Binary search runs in `O(log T)`, and each check runs in `O(n)`, so the total complexity is `O(n log T)`.

- **Space Complexity:** $$O(1)$$ 
  - Only a few extra variables are used, so no additional space is required.

## Code
```typescript
function repairCars(ranks: number[], cars: number): number {
     let min = 1, max = ranks[0] * cars**2;
     const helper = (num: number): boolean => {
         let sum = cars;
         for (const rank of ranks) {
             sum -= Math.floor(Math.sqrt(num / rank));
         }
         return sum <= 0;
     }
     while (min <= max) {
         const mid = Math.floor((min + max) / 2);
         if (helper(mid)) {
             max = mid - 1;
         } else {
            min = mid + 1;
         }
     }
   return min;
};
```


['TypeScript']