Stage 4: Advanced Techniques·Binary Search on Answer
Hard#21417 min readBinary SearchGreedyLeetCode

Maximum Running Time of N Computers

Binary search on minutes

Binary search on time + greedy check on battery allocation.

Published Apr 16, 2026

Problem

You have n computers and a list of batteries, each with a charge duration. At any moment each running computer consumes one unit of charge from one battery. You may swap which battery powers which computer at any time — batteries can be shared across computers (but only one computer uses a given battery at any instant).

What is the maximum number of minutes you can keep all n computers running simultaneously?

Input
n = 2, batteries = [3, 3, 3]
Output
4
Explanation
With 4 minutes target, each computer needs 4 units. Total needed = 8. Total charge = 9. Each battery contributes min(3, 4) = 3, sum = 9 >= 8. Feasible.
Input
n = 2, batteries = [1, 1, 1, 1]
Output
2
Explanation
Total charge = 4. Per computer = 4/2 = 2. Each battery contributes min(1, 2) = 1, sum = 4 >= 4. Feasible at 2. At 3: sum of min(1,3) = 4 < 6. Not feasible.

Intuition

The key insight: a battery with charge b can contribute at most min(b, m) minutes when the target is m, because no single computer can use more than m minutes total, and swapping lets us redistribute excess charge to other computers.

This means m minutes is feasible if and only if sum(min(b_i, m)) >= m * n. The left side is monotonically non-decreasing in m while the right side grows linearly — so there is a threshold where feasibility flips. Binary search finds it.

Upper bound: sum(batteries) / n (total charge evenly distributed). Lower bound: 0 (trivially feasible).

Interactive concept

Battery Runtime Feasibility

For a guessed runtime t, each battery contributes at most t usable minutes.

Cap each battery by target time

Extra charge above t cannot help a single computer run longer than t in the feasibility check.

try t3
b1
3
b2
3
b3
3
b4
1

Approach

Brute Force

TimeO(M · n)
SpaceO(1)

Linear scan from m = 1 upward, checking feasibility each time. Stops at sum/n worst case. Works but slow for large charge sums.

impl Solution {
    pub fn max_run_time(n: i32, batteries: Vec<i32>) -> i64 {
        let n = n as i64;
        let total: i64 = batteries.iter().map(|&b| b as i64).sum();
        let hi = total / n;

        for m in (0..=hi).rev() {
            let contrib: i64 = batteries.iter().map(|&b| (b as i64).min(m)).sum();
            if contrib >= m * n {
                return m;
            }
        }

        0
    }
}
Rust · runnable

Binary Search on Minutes

TimeO(n log M)
SpaceO(1)
Recommended

Binary search m in [0, sum(batteries) / n]. The feasibility predicate is sum(min(b_i, m)) >= m * n. Each check is O(n), and the search space is O(log(sum/n)).

impl Solution {
    pub fn max_run_time(n: i32, batteries: Vec<i32>) -> i64 {
        let n = n as i64;
        let total: i64 = batteries.iter().map(|&b| b as i64).sum();

        let mut lo: i64 = 0;
        let mut hi: i64 = total / n;

        while lo < hi {
            let mid= lo + (hi - lo + 1) / 2;
            let contrib: i64= batteries.iter().map(|&b| (b as i64).min(mid)).sum();
            if contrib >= mid * n {
                lo = mid;
            } else {
                hi = mid - 1;
            }
        }

        lo
    }
}
Rust · runnable

Note: this is an upper-bound binary search (maximize m). We round mid upward (lo + ceil(...)) and shrink from the right on failure. The search converges to the largest feasible m.

Trace

n = 2, batteries = [3, 3, 3]. Search space [0, 4] (total 9, hi = 9/2 = 4):

Binary search#2141

Maximum Running Time of N Computers walkthrough

n = 2, batteries = [3,3,3]

Step 1 of 30%
m
2
contrib
6
needed
4
feasible
yes

Each of the three batteries contributes at most 2.

Test m = 2

Decision

Total contribution 6 covers needed 4.

Update

2 is feasible; raise lo.

Why it works

We are maximizing the feasible time.

Invariant

A target time m is feasible when sum(min(battery, m)) >= n * m.

Finish

The maximum simultaneous running time is 4 minutes.

Cap each battery contribution at m; one battery cannot power two computers at the same time.Use wide integers for total charge.

Converges at lo = hi = 4.

Edge cases

  • One computer — answer is sum(batteries) since all charge feeds one machine.
  • n batteries, each = 1 — answer is 1 (each computer gets exactly one minute).
  • One massive battery — capped at m contribution; the rest must cover (n-1) * m minutes.
  • Overflow — batteries can sum to 10^14; use i64 / safe integer math.

Takeaway

The min(b, m) cap is the key observation. Once you see that a battery's contribution saturates at the target duration, the problem collapses from a scheduling puzzle into a one-line feasibility check — which is monotone, which means binary search.

Same pattern applies to real capacity planning: "given a pool of resources with different sizes, what's the longest all machines can run?" The answer is sum(min(resource, target)) >= target * count.

More in Binary Search on Answer