🔥BEATS 💯 % 🎯 |✨SUPER EASY BEGINNERS 👏| JAVA | C | C++ | PYTHON| JAVASCRIPT | DART
IntuitionWe have multiple mechanics, each with a different rank, and they can repair cars. A mechanic with rank r takes r * t² minutes to repair t cars. We need
![image.png](https://assets.leetcode.com/users/images/90dcadb7-578d-4d45-995d-bd14a05cbe35_1742104306.9921484.png)

------


![1000120811.png](https://assets.leetcode.com/users/images/f97d26c6-0ba9-418f-9e55-1c7ac570806e_1742122337.2373128.png)



---

# **Intuition**  
We have multiple mechanics, each with a different **rank**, and they can repair cars. A mechanic with rank `r` takes `r * t²` minutes to repair `t` cars. We need to find the **minimum time** required to repair `cars` number of cars when all mechanics work in parallel.  

Since the time taken depends on **squared** terms (`t²`), and we are looking for the **minimum** possible time, we can use **binary search** to efficiently find the optimal answer.

---

# **Approach**  
1. **Binary Search on Time:**  
   - The **minimum possible time** (`left`) is `1` minute.  
   - The **maximum possible time** (`right`) is given by the slowest possible case:  
     \[
     \text{max time} = \min(ranks) \times \text{cars}^2
     \]
     This assumes only the fastest mechanic is working on all cars.

2. **Checking Feasibility (`canRepair` function):**  
   - For a given `mid` time, determine how many cars can be repaired by all mechanics.  
   - A mechanic with rank `r` can repair at most:
     \[
     \text{floor}(\sqrt{\frac{\text{mid}}{r}})
     \]
     cars in `mid` time.  
   - Sum up all cars repaired by all mechanics and check if it **covers `cars`**.

3. **Binary Search Execution:**  
   - If we can repair `cars` within `mid` minutes, **reduce the search range** (`right = mid - 1`).  
   - Otherwise, **increase the time** (`left = mid + 1`).  
   - The minimum valid `mid` that allows all cars to be repaired is the answer.

---

# **Complexity Analysis**  
- **Time Complexity:**  
  - Binary search runs in **O(log (R × C²))**, where `R` is the minimum rank and `C` is the total number of cars.  
  - For each `mid` value, we iterate over `ranks` (**O(N)**).  
  - **Total Complexity:** **O(N log (R × C²))**  

- **Space Complexity:** **O(1)** (Only a few variables used, no extra data structures).

---


```dart []
import 'dart:math';

class Solution {
  int repairCars(List<int> ranks, int cars) {
    int left = 1;
    int right = ranks.reduce(min) * cars * cars; // Worst case time
    int ans = right;

    while (left <= right) {
      int mid = (left + right) ~/ 2;
      int totalCars = 0;

      for (int rank in ranks) {
        int maxCars = sqrt(mid ~/ rank).floor();
        totalCars += maxCars;
      }

      if (totalCars >= cars) {
        ans = mid; 
        right = mid - 1; // Try smaller time
      } else {
        left = mid + 1; // Increase time
      }
    }

    return ans;
  }
}
```


```java []
import java.util.*;

class Solution {
    public int repairCars(int[] ranks, int cars) {
        int left = 1, right = Arrays.stream(ranks).min().getAsInt() * cars * cars;
        int ans = right;

        while (left <= right) {
            int mid = (left + right) / 2;
            int totalCars = 0;

            for (int rank : ranks) {
                totalCars += Math.sqrt(mid / rank);
            }

            if (totalCars >= cars) {
                ans = mid;
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }

        return ans;
    }
}
```


```cpp []
#include <vector>
#include <cmath>
#include <algorithm>

using namespace std;

class Solution {
public:
    int repairCars(vector<int>& ranks, int cars) {
        int left = 1, right = *min_element(ranks.begin(), ranks.end()) * cars * cars;
        int ans = right;

        while (left <= right) {
            int mid = left + (right - left) / 2;
            int totalCars = 0;

            for (int rank : ranks) {
                totalCars += floor(sqrt(mid / rank));
            }

            if (totalCars >= cars) {
                ans = mid;
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }

        return ans;
    }
};
```


```python []
import math
from typing import List

class Solution:
    def repairCars(self, ranks: List[int], cars: int) -> int:
        left, right = 1, min(ranks) * cars * cars
        ans = right

        while left <= right:
            mid = (left + right) // 2
            total_cars = sum(math.floor(math.sqrt(mid // r)) for r in ranks)

            if total_cars >= cars:
                ans = mid
                right = mid - 1
            else:
                left = mid + 1

        return ans
```


```javascript []
var repairCars = function(ranks, cars) {
    let left = 1, right = Math.min(...ranks) * cars * cars, ans = right;

    while (left <= right) {
        let mid = Math.floor((left + right) / 2);
        let totalCars = 0;

        for (let rank of ranks) {
            totalCars += Math.floor(Math.sqrt(mid / rank));
        }

        if (totalCars >= cars) {
            ans = mid;
            right = mid - 1;
        } else {
            left = mid + 1;
        }
    }

    return ans;
};
```

---





------

![image.png](https://assets.leetcode.com/users/images/d81a7cee-fa73-41a2-82dc-65bf475d960c_1722267693.3902693.png) {:style='width:250px'}
['Array', 'Binary Search', 'C', 'Python', 'C++', 'Java', 'Python3', 'JavaScript', 'Dart', 'Python ML']