Simple Logic 💡🔥 With Proper Explanation 🤝 ✅
IntuitionWe need to find the minimum time required to repair all cars using multiple mechanics with different ranks.A mechanic with rank r takes r × x² time to
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
We need to find the minimum time required to repair all cars using multiple mechanics with different ranks.

A mechanic with rank r takes r × x² time to repair x cars.
The goal is to distribute cars optimally so that the total repair time is minimized.
Instead of manually assigning cars to mechanics, we use Binary Search on Time to efficiently find the minimum possible time.

# Approach
<!-- Describe your approach to solving the problem. -->
Define the Search Space

Minimum time = The best (lowest-ranked) mechanic repairing one car → min(ranks) * 1 * 1
Maximum time = The worst (highest-ranked) mechanic repairing all cars → max(ranks) * cars * cars
We search between this range using Binary Search.
Binary Search on Time

Pick a midpoint (guessTime) and check if we can repair all cars within that time.
If possible, try to reduce the time (r = guessTime - 1).
If not possible, increase the time (l = guessTime + 1).
Check Feasibility (isGood Function)
If the total number of repaired cars is greater than or equal to cars, then guessTime is valid.



PLEASE UPVOTE IF IT HELPS ⬆️ ✅
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
O(n log(maxTime))

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->


# Code
```javascript []
/**
 * @param {number[]} ranks
 * @param {number} cars
 * @return {number}
 */
var repairCars = function(ranks, cars) {
    let res = Infinity;
    let l = Math.min(...ranks);
    let r = Math.max(...ranks) * cars * cars;

    while (l <= r) {
        let guessTime = Math.floor((l + r) / 2);
        
        if (isGood(ranks, guessTime, cars)) {
            res = guessTime;
            r = guessTime - 1;
        } else {
            l = guessTime + 1;
        }
    }

    return res;
};

function isGood(ranks, max, cars) {
    let count = 0;

    for (let i = 0; i < ranks.length; i++) {
        let temp = Math.floor(max / ranks[i]);
        count += Math.floor(Math.sqrt(temp));
        if (count >= cars) return true;
    }
    return false;
}

```
['Dynamic Programming', 'Simulation', 'Counting', 'JavaScript']