🚗💨 Minimum Time to Repair All Cars | Binary Search Optimization 🚀
⚒️ IntuitionImagine you're running a car repair shop, and you have multiple mechanics with different skill levels (ranks). Some mechanics are super fast ⚡, whil
# ⚒️ Intuition
Imagine you're running a car repair shop, and you have multiple mechanics with different skill levels (ranks). Some mechanics are super fast ⚡, while others take a bit longer 🐢.

The time a mechanic takes to fix x cars follows this formula:

$$time = rank × 𝑥^2$$
 
Rearranging, we get:

$$x = \left\lfloor \sqrt{\frac{\text{time}}{\text{rank}}} \right\rfloor$$
​
So, given a time, we can calculate how many cars can be repaired. Our goal is to find the minimum time needed to repair all cars.

Since time is a monotonic function (more time → more cars repaired), we can efficiently binary search on time! 🧐🔍

# 🚀 Approach
1️⃣ Binary Search on Time

We start with left = 1 (minimum possible time).
The right boundary is set to max(rank) * (cars^2), the worst case where one mechanic does all repairs.
2️⃣ Feasibility Check (repPoss function)

For a given time, calculate the total number of cars that can be repaired using:
$$x = \left\lfloor \sqrt{\frac{\text{time}}{\text{rank}}} \right\rfloor$$
If we can repair at least cars, this time is valid, so we try for a smaller time.
Otherwise, we increase the time.
3️⃣ Final Answer

The minimum valid time found through binary search is our answer.

# Complexity
- Time complexity:
$$O\left(n \log \left(\text{maxRank} \times \text{cars}^2 \right) \right)$$


- Space complexity:
O(1)

# Code
```java []
class Solution {
    public boolean repPoss(int[] ranks, int cars, long time){
        long carsRep = 0;
        for(int rank : ranks){
            long carsR = time/rank;
            carsRep += (long)Math.sqrt(carsR);
            if(carsRep>=cars) return true;
        }
        return false;
    }
    public long repairCars(int[] ranks, int cars) {
        long left = 1, maxRank = 0, ans = 0;
        for(int rank : ranks) maxRank = Math.max(rank,maxRank);
        long right = maxRank*((long)cars*cars);
        while(left<=right){
            long mid = left + (right-left)/2;
            if(repPoss(ranks,cars,mid)){
                ans = mid;
                right = mid-1;
            }else left = mid+1;
        }
        return ans;
    }
}
```
```C++ []
class Solution {
public:
    bool repPoss(vector<int>& ranks, int cars, long time) {
        long carsRep = 0;
        for(int rank : ranks) {
            long carsR = time / rank;
            carsRep += (long) sqrt(carsR);
            if(carsRep >= cars) return true;
        }
        return false;
    }

    long long repairCars(vector<int>& ranks, int cars) {
        long long left = 1, maxRank = 0, ans = 0;
        for(int rank : ranks) maxRank = max(maxRank, (long long)rank);
        long long right = maxRank * ((long long) cars * cars);

        while(left <= right){
            long long mid = left + (right - left) / 2;
            if(repPoss(ranks, cars, mid)){
                ans = mid;
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return ans;
    }
};

```
``` Python []
import math

class Solution:
    def repPoss(self, ranks: List[int], cars: int, time: int) -> bool:
        carsRep = 0
        for rank in ranks:
            carsR = time // rank
            carsRep += int(math.sqrt(carsR))
            if carsRep >= cars:
                return True
        return False

    def repairCars(self, ranks: List[int], cars: int) -> int:
        left, maxRank = 1, max(ranks)
        right = maxRank * (cars * cars)
        ans = 0

        while left <= right:
            mid = left + (right - left) // 2
            if self.repPoss(ranks, cars, mid):
                ans = mid
                right = mid - 1
            else:
                left = mid + 1
                
        return ans
```
# 💫 Summary
✅ Binary Search on Time to find the minimum required time.
✅ Mathematical Formula to efficiently compute cars repaired per mechanic.
✅ Optimized Approach ensures fast execution, even for large inputs.

If this helped, please upvote! 🚀✨ It motivates me to keep sharing awesome solutions! 🔥🔥🔥
Happy Coding! 😃💻
['Array', 'Binary Search', 'Python', 'C++', 'Java']