Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
You have n computers. You are given the integer n and a 0-indexed integer array batteries where the ith battery can run a computer for batteries[i] minutes. You are interested in running all n computers simultaneously using the given batteries.
Initially, you can insert at most one battery into each computer. After that and at any integer time moment, you can remove a battery from a computer and insert another battery any number of times. The inserted battery can be a totally new battery or a battery from another computer. You may assume that the removing and inserting processes take no time.
Note that the batteries cannot be recharged.
Return the maximum number of minutes you can run all the n computers simultaneously.
Example 1:
Input: n = 2, batteries = [3,3,3] Output: 4 Explanation: Initially, insert battery 0 into the first computer and battery 1 into the second computer. After two minutes, remove battery 1 from the second computer and insert battery 2 instead. Note that battery 1 can still run for one minute. At the end of the third minute, battery 0 is drained, and you need to remove it from the first computer and insert battery 1 instead. By the end of the fourth minute, battery 1 is also drained, and the first computer is no longer running. We can run the two computers simultaneously for at most 4 minutes, so we return 4.
Example 2:
Input: n = 2, batteries = [1,1,1,1] Output: 2 Explanation: Initially, insert battery 0 into the first computer and battery 2 into the second computer. After one minute, battery 0 and battery 2 are drained so you need to remove them and insert battery 1 into the first computer and battery 3 into the second computer. After another minute, battery 1 and battery 3 are also drained so the first and second computers are no longer running. We can run the two computers simultaneously for at most 2 minutes, so we return 2.
Constraints:
1 <= n <= batteries.length <= 1051 <= batteries[i] <= 109Problem summary: You have n computers. You are given the integer n and a 0-indexed integer array batteries where the ith battery can run a computer for batteries[i] minutes. You are interested in running all n computers simultaneously using the given batteries. Initially, you can insert at most one battery into each computer. After that and at any integer time moment, you can remove a battery from a computer and insert another battery any number of times. The inserted battery can be a totally new battery or a battery from another computer. You may assume that the removing and inserting processes take no time. Note that the batteries cannot be recharged. Return the maximum number of minutes you can run all the n computers simultaneously.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Binary Search · Greedy
2 [3,3,3]
2 [1,1,1,1]
minimum-moves-to-equal-array-elements)sell-diminishing-valued-colored-balls)maximum-number-of-tasks-you-can-assign)minimum-time-to-complete-trips)minimum-amount-of-time-to-fill-cups)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2141: Maximum Running Time of N Computers
class Solution {
public long maxRunTime(int n, int[] batteries) {
long l = 0, r = 0;
for (int x : batteries) {
r += x;
}
while (l < r) {
long mid = (l + r + 1) >> 1;
long s = 0;
for (int x : batteries) {
s += Math.min(mid, x);
}
if (s >= n * mid) {
l = mid;
} else {
r = mid - 1;
}
}
return l;
}
}
// Accepted solution for LeetCode #2141: Maximum Running Time of N Computers
func maxRunTime(n int, batteries []int) int64 {
l, r := 0, 0
for _, x := range batteries {
r += x
}
for l < r {
mid := (l + r + 1) >> 1
s := 0
for _, x := range batteries {
s += min(x, mid)
}
if s >= n*mid {
l = mid
} else {
r = mid - 1
}
}
return int64(l)
}
# Accepted solution for LeetCode #2141: Maximum Running Time of N Computers
class Solution:
def maxRunTime(self, n: int, batteries: List[int]) -> int:
l, r = 0, sum(batteries)
while l < r:
mid = (l + r + 1) >> 1
if sum(min(x, mid) for x in batteries) >= n * mid:
l = mid
else:
r = mid - 1
return l
// Accepted solution for LeetCode #2141: Maximum Running Time of N Computers
impl Solution {
pub fn max_run_time(n: i32, batteries: Vec<i32>) -> i64 {
let n = n as i64;
let mut l: i64 = 0;
let mut r: i64 = batteries.iter().map(|&x| x as i64).sum();
while l < r {
let mid = (l + r + 1) >> 1;
let mut s: i64 = 0;
for &x in &batteries {
let v = x as i64;
s += if v < mid { v } else { mid };
}
if s >= n * mid {
l = mid;
} else {
r = mid - 1;
}
}
l
}
}
// Accepted solution for LeetCode #2141: Maximum Running Time of N Computers
function maxRunTime(n: number, batteries: number[]): number {
let l = 0n;
let r = 0n;
for (const x of batteries) {
r += BigInt(x);
}
while (l < r) {
const mid = (l + r + 1n) >> 1n;
let s = 0n;
for (const x of batteries) {
s += BigInt(Math.min(x, Number(mid)));
}
if (s >= mid * BigInt(n)) {
l = mid;
} else {
r = mid - 1n;
}
}
return Number(l);
}
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.