🌟 Minimum Time to Repair Cars — Binary Search Approach 🌟
📚 Problem SummaryWe have n mechanics, each with a certain rank. A mechanic with rank r takes r * n^2 minutes to repair n cars. Given an array ranks representing
# 📚 Problem Summary
We have `n` mechanics, each with a certain rank. A mechanic with rank `r` takes `r * n^2` minutes to repair `n` cars. Given an array `ranks` representing the ranks of the mechanics and an integer `cars` representing the total number of cars to be repaired, our goal is to find the **minimum time** required to repair all cars, considering that all mechanics can work **simultaneously**.

## ✨ Example Walkthrough
### Example 1:
**Input:**  
`ranks = [4,2,3,1]`, `cars = 10`

**Output:**  
`16`

**Explanation:**

| Mechanic Rank | Cars Assigned | Time Taken $$(r * n^2)$$ |
|--------------|--------------|-----------------|
| 4            | 2            | $$4 * 2^2 = 16$$   |
| 2            | 2            | $$2 * 2^2 = 8 $$   |
| 3            | 2            | $$3 * 2^2 = 12$$   |
| 1            | 4            | $$1 * 4^2 = 16$$   |

The minimum time required is **16 minutes**.

---
# 💪 Approach - Binary Search on Time
Since we need to **minimize the time**, we can use **binary search** on the possible values of time.

## **Steps:**
1. **Define the search space**: The minimum time (`l`) is `1` and the maximum possible time (`r`) is a large upper bound (`1e14`).
2. **Use binary search** to find the minimum possible time:
   - Check if `mid` time is feasible.
   - For each mechanic, calculate how many cars they can repair within `mid` time.
   - If they can repair at least `cars`, reduce `r` to `mid - 1`.
   - Otherwise, increase `l` to `mid + 1`.
3. **Return the minimum feasible time** found.

---
# ⌛ Time & Space Complexity
- **Time Complexity**: $$O(n * log(max \ time))$$ where `n` is the number of mechanics and `max time` is the search range (~`1e14`).
- **Space Complexity**: $$O(1)$$, as we use only a few integer variables.

---
![c1ee5054-dd07-4caa-a7df-e21647cfae9e_1709942227.5165014.png](https://assets.leetcode.com/users/images/514c16ae-2e12-45ac-baf3-617e985adb27_1742095818.0139773.png)


# 💻 Code Implementation

```cpp []
#define ll long long
class Solution {
public:
    long long repairCars(vector<int>& ranks, int cars) {
        ll l = 1, r = 1e14, res = 0;
        while (l <= r) {
            ll mid = (l + r) / 2, k = 0;
            for (int rank : ranks) {
                k += sqrt(mid / rank);
                if (k >= cars) break;  // Early stopping
            }
            if (k >= cars) {
                res = mid;
                r = mid - 1;
            } else {
                l = mid + 1;
            }
        }
        return res;
    }
};
```

```python []
class Solution:
    def repairCars(self, ranks, cars):
        l, r, res = 1, 10**14, 0
        while l <= r:
            mid = (l + r) // 2
            k = sum(int(sqrt(mid // rank)) for rank in ranks)
            if k >= cars:
                res = mid
                r = mid - 1
            else:
                l = mid + 1
        return res
```

---
# 💪 Example Walkthrough in Detail

## Example 2:
**Input:** `ranks = [5,1,8]`, `cars = 6`

**Binary Search Steps:**

| Left (`l`) | Right (`r`) | Mid | Feasible? |
|------------|------------|-----|-----------|
| 1          | 1e14       | `5e13`  | Yes       |
| 1          | `5e13`  | `2.5e13` | Yes       |
| 1          | `2.5e13` | `1.25e13` | Yes       |
| ...        | ...        | ... | ...       |
| 16         | 17         | 16  | Yes       |
| 16         | 15         | -   | -         |

Final answer: **16 minutes**

---
# 🎉 Conclusion
This problem is a classic **binary search on answer** type problem, where we search for the minimum possible time that meets the given constraints. Using binary search helps us efficiently find the answer instead of iterating through all possible times.

Did you find this explanation helpful? If yes, don't forget to **upvote** 💪 so I can continue sharing more solutions like this! 🚀

![1HBC0324_COV.jpg](https://assets.leetcode.com/users/images/c088c5b0-6a7c-458c-8295-66150e1b2cd2_1742095800.8121245.jpeg)

['Array', 'Binary Search', 'Python', 'C++']