6 ms JS/TS. [Binary Search] and [Priority Queue]. Beats 100.00%
Approach
Binary Search 15 ms
Optimized Binary Search 6 ms
MinQueue 130 ms
Complexity
Time complexity: Binary Search O(n∗log(maxRank∗k2))

Space complexity: Bina
# Approach
1. Binary Search `15 ms`
2. Optimized Binary Search `6 ms`
3. MinQueue `130 ms`

![1.png](https://assets.leetcode.com/users/images/719a40d3-ec02-4430-89a0-fcad936294cd_1742155300.456022.png)


# Complexity
- Time complexity: Binary Search $$O(n * log(maxRank * k^2))$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: Binary Search $$O(1)$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Time complexity: Optimized Binary Search $$O(n + possibleRanks * log(maxRank * k^2))$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: Optimized Binary Search $$O(possibleRanks)$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Time complexity: MinQueue $$O(n + k * log(uniqueRanks))$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: MinQueue $$O(possibleRanks)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```javascript [Binary Search]
const repairCars = (a, k) => {
    const n = a.length;
    const test = (m) => {
        let s = 0;
        for (let i = 0; i < n && s < k; ++i)
            s += Math.trunc(Math.sqrt(m / a[i]));
        return s >= k;
    };
    let i = 1;
    let j = a[0] * k * k;
    while (i < j) {
        const m = Math.trunc((i + j) / 2);
        if (test(m)) j = m;
        else i = m + 1;
    }
    return j;
};
```
```javascript [Optimized Binary Search]
const repairCars = (a, k) => {
    const r = new Int32Array(101);
    const test = (m) => {
        let s = 0;
        for (let i = 1; i < 101 && s < k; ++i)
            s += r[i] * Math.trunc(Math.sqrt(m / i));
        return s >= k;
    };
    for (let i = 0; i < a.length; ++i)
        ++r[a[i]];
    let i = 1;
    let j = a[0] * k * k;
    while (i < j) {
        const m = Math.trunc((i + j) / 2);
        if (test(m)) j = m;
        else i = m + 1;
    }
    return j;
};
```
```javascript [MinQueue]
const repairCars = (a, k) => {
    const r = new Uint32Array(101);
    for (let i = 0; i < a.length; ++i) ++r[a[i]];
    const q = new PriorityQueue((x, y) => x[0] - y[0]);
    for (let i = 1; i < 101; ++i)
        if (r[i] > 0) q.push([i, i, 1, r[i]]);
    let time = 0
    while (k > 0) {
        const d = q.pop();
        time = d[0];
        ++d[2];
        k -= d[3];
        d[0] = d[1] * d[2] ** 2;
        q.push(d);
    }
    return time;
};
```
['Array', 'Binary Search', 'Heap (Priority Queue)', 'TypeScript', 'JavaScript']