|
| 1 | +# [Problem 2141: Maximum Running Time of N Computers](https://leetcode.com/problems/maximum-running-time-of-n-computers/description/?envType=daily-question) |
| 2 | + |
| 3 | +## Initial thoughts (stream-of-consciousness) |
| 4 | +I need to run n computers simultaneously using batteries, can swap batteries any time. Initially one battery per computer but later can move batteries around. The resources are battery minutes. If I want to run all n computers for T minutes, each computer needs T minutes of battery time, so total required is n*T minutes. Each battery can contribute at most its capacity, but because a single battery can't power more than one computer at the same time, when checking for feasibility of T we should cap each battery contribution at T (a battery of size > T can't contribute more than T simultaneous-minutes toward the "per-computer T" objective). So a necessary condition: sum(min(b_i, T)) >= n*T. Is it sufficient? Intuitively yes: we can slice battery minutes arbitrarily in integer minutes and reassign; if total capped contribution suffices, we can schedule to reach T. That suggests binary searching on T. |
| 5 | + |
| 6 | +Alternate naive approach: sort descending and greedily allocate largest batteries to computers then use remaining to extend times — but reasoning is cleaner with binary search on time and the sum(min(b_i, T)) check. |
| 7 | + |
| 8 | +Constraints: up to 1e5 batteries with capacities up to 1e9, sums up to ~1e14 -> Python int fine. Binary search upper bound can be sum(batteries)//n because that's maximum average minutes per computer. |
| 9 | + |
| 10 | +## Refining the problem, round 2 thoughts |
| 11 | +Binary search T in range [0, sum(batteries)//n]. For each mid, compute total = sum(min(b, mid) for b in batteries). If total >= mid * n, mid is feasible; otherwise not. Use upper-mid bias (mid = (lo+hi+1)//2) to avoid infinite loop. Time complexity: O(m * log S) where m = len(batteries) and S is sum(batteries)//n (log S <= ~60). Space O(1). |
| 12 | + |
| 13 | +Edge cases: |
| 14 | +- n equals number of batteries (still works). |
| 15 | +- Some batteries very large; capping by mid handles it. |
| 16 | +- Very small batteries many of them; summing still fine. |
| 17 | + |
| 18 | +This check is both necessary and sufficient because we can always split battery minutes across computers via swaps; capping per battery at T captures the fact a single battery cannot contribute more than T simultaneous minutes to the n computers' run of length T. |
| 19 | + |
| 20 | +## Attempted solution(s) |
| 21 | +```python |
| 22 | +from typing import List |
| 23 | + |
| 24 | +class Solution: |
| 25 | + def maxRunTime(self, n: int, batteries: List[int]) -> int: |
| 26 | + # Upper bound: average minutes available per computer |
| 27 | + total_minutes = sum(batteries) |
| 28 | + hi = total_minutes // n |
| 29 | + lo = 0 |
| 30 | + |
| 31 | + # helper to test feasibility of running for t minutes |
| 32 | + def can_run(t: int) -> bool: |
| 33 | + # sum of contributions capped at t per battery |
| 34 | + s = 0 |
| 35 | + for b in batteries: |
| 36 | + # early break if already enough to save work |
| 37 | + if s >= t * n: |
| 38 | + return True |
| 39 | + s += b if b <= t else t |
| 40 | + return s >= t * n |
| 41 | + |
| 42 | + # binary search for maximum feasible t |
| 43 | + while lo < hi: |
| 44 | + mid = (lo + hi + 1) // 2 |
| 45 | + if can_run(mid): |
| 46 | + lo = mid |
| 47 | + else: |
| 48 | + hi = mid - 1 |
| 49 | + return lo |
| 50 | +``` |
| 51 | +- Notes: |
| 52 | + - Approach: binary search on T with feasibility check sum(min(b_i, T)) >= n*T. |
| 53 | + - Time complexity: O(m * log S) where m = len(batteries) and S = sum(batteries)//n (log S is small; in practice <= ~60). More concretely O(m * log(total_minutes/n)). |
| 54 | + - Space complexity: O(1) extra space. |
| 55 | + - Implementation details: early exit in can_run if running sum already reaches required threshold to avoid iterating whole array unnecessarily for larger T. |
0 commit comments