Best solution using binary search | C++ | Java | 1ms
IntuitionThe problem asks for the minimum time to repair all cars using mechanics with different ranks, where a mechanic with rank r takes r * n² minutes to rep
# Intuition
The problem asks for the minimum time to repair all cars using mechanics with different ranks, where a mechanic with rank r takes r * n² minutes to repair n cars. The key insight is that we can use binary search to find this minimum time efficiently.
If we have a specific time t, we can determine how many cars each mechanic can repair within that time. Then we can check if all cars can be repaired within time t. This allows us to perform a binary search on the possible time values.

# Approach
1. Define a search range:
   - Minimum time: 1
   - Maximum time: The time it would take if the fastest mechanic repairs all cars alone
   - For each potential time t in our binary search:

2. Calculate how many cars each mechanic can repair in time t
   - If a mechanic with rank r can repair n cars in time t, then: r * n² ≤ t, so n = floor(sqrt(t/r))
   - Sum up the total number of cars all mechanics can repair
   - If this sum is at least equal to the required number of cars, time t is feasible
   - 
3. Use binary search to find the minimum feasible time

# Complexity
- Time complexity: O(m * log(r * n * n)), where m is the number of mechanics (length of ranks), n is the number of cars, and r is the minimum rank.

- Space complexity: O(1)

# Code
```cpp []
class Solution {
public:

    bool f(vector<int>& ranks, int cars, long long mid){
        long long count =0;
        for(int& x: ranks){
            count+=sqrt(mid/x);
            if(count>=cars) return true;
        }
        return false;
    }

    long long repairCars(vector<int>& ranks, int cars) {
        long long i = 1;
        long long j = (long long)*min_element(ranks.begin(),ranks.end())*cars*cars;

        while(i<j){
            long long mid = i + (j - i) / 2;
            if(f(ranks,cars,mid)) j=mid;
            else i = mid+1;
        }
        return i;
        
    }
};
```
```java []
import java.util.*;

class Solution {
    public boolean f(int[] ranks, int cars, long mid) {
        long count = 0;
        for (int x : ranks) {
            count += Math.sqrt(mid / x);
            if (count >= cars) return true;
        }
        return false;
    }

    public long repairCars(int[] ranks, int cars) {
        long i = 1;
        long j = (long) Arrays.stream(ranks).min().getAsInt() * cars * cars;

        while (i < j) {
            long mid = i + (j - i) / 2;
            if (f(ranks, cars, mid)) {
                j = mid;
            } else {
                i = mid + 1;
            }
        }
        return i;
    }
}

```

Please Upvote
['Array', 'Binary Search', 'C++', 'Java']