|
| 1 | +# Maximum Running Time of N Computers |
| 2 | + |
| 3 | +You are given an integer, n, representing the number of computers, and a 0-indexed integer array, batteries, where |
| 4 | +batteries[i] denotes the number of minutes the ith battery can power a computer. |
| 5 | + |
| 6 | +Your goal is to run all n computers simultaneously for the maximum possible number of minutes using the available |
| 7 | +batteries. |
| 8 | + |
| 9 | +Initially, you may assign at most one battery to each computer. After that, at any moment, you may remove a battery |
| 10 | +from a computer and replace it with another battery—either an unused battery or one taken from another computer. |
| 11 | +This replacement process can be repeated any number of times and takes no time. |
| 12 | + |
| 13 | +Each battery can power any computer multiple times, but only until it is completely drained. Batteries cannot be |
| 14 | +recharged. |
| 15 | + |
| 16 | +Return the maximum number of minutes you can run all n computers simultaneously. |
| 17 | + |
| 18 | +Constraints: |
| 19 | + |
| 20 | +- 1 ≤ `n` ≤ `batteries.length` ≤ 10^5 |
| 21 | +- 1 ≤ `batteries[i]` ≤ 10^5 |
| 22 | + |
| 23 | +## Examples |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +## Solution |
| 30 | + |
| 31 | +This solution aims to find the maximum number of minutes all `n` computers can run simultaneously using a set of batteries. |
| 32 | +We use a modified binary search pattern on the possible runtime values to achieve this. The key observation is that if |
| 33 | +it’s possible to run all n computers for x minutes, it is also possible to run them for any time less than `x`. This |
| 34 | +monotonic property makes binary search applicable. To solve this, we set the search space from 0 to `total_power // n`, |
| 35 | +where `total_power` is the sum of all battery capacities. At each step, we check whether running all computers for mid |
| 36 | +minutes is feasible by verifying that the sum of the available battery contributions (each battery contributing up to |
| 37 | +mid minutes) is at least `n * mid`. This feasibility check helps us efficiently narrow down the maximum achievable runtime. |
| 38 | + |
| 39 | +Now, let’s look at the solution steps below: |
| 40 | + |
| 41 | +1. Set `left=0` and `right=sum(batteries) // n` to define the minimum and maximum possible simultaneous runtime. |
| 42 | +2. While `left < right`: |
| 43 | + - Calculate `mid = right - (rught - left) // 2` (biases the midpoint toward the higher end to avoid infinite loops). |
| 44 | + - Initialize `usable=0` to store the sum of usable power |
| 45 | + - For each battery, add `min(b, mid)` to `usable` |
| 46 | + - If `usable >= mid * n`, the target feasible, so set `left=mid` to search for a longer time. |
| 47 | + - Otherwise, set `right = mid-1` to search in the lower half |
| 48 | +3. After the loop completes, `left` holds the maximum number of minutes that all `n` computers can run simultaneously. |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +### Time Complexity |
| 61 | + |
| 62 | +The time complexity of the solution is `O(n⋅logT)`), where `n` is the number of batteries and `T` is the total power of |
| 63 | +one computer, `T = sum(batteries) // n)`. This is because binary search runs in `O(logT)` iterations, and in each |
| 64 | +iteration, we compute the usable power by iterating through all `n` batteries, which takes `O(n)` time. |
| 65 | + |
| 66 | +### Space Complexity |
| 67 | + |
| 68 | +The space complexity of the solution os `O(1)` because no extra space is used |
0 commit comments