↓Skip to main content
  1. LeetCode/

LeetCode 2141: Maximum Running Time of N Computers

·3 mins· ·
LeetCode Hard Greedy Binary-Search Sorting
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: hard First Attempt: 2026-07-28

  • Submissions: Wrong Answer ×2 → Accepted

Approach
#

Batteries can be swapped at any time, so as long as there is enough total charge the time spreads evenly: ideally total // n minutes.

The exception is an unusually large battery: a battery can be in only one computer at a time, so it contributes at most T minutes to one computer. If the largest battery exceeds the average total // n, dedicate it to one computer and waste the rest; remove both and recompute the average. Repeat until the largest battery does not exceed the average (Greedy Template 6).

My Attempts
#

  1. Attempt 1 removed batteries without subtracting them from the total. For n = 2, [7, 5] the answer is 5; after removing 7 it returned 12 // 1 = 12.
  2. Attempt 2 subtracted, but used avg <= batteries[-1], removing batteries equal to the average too. For n = 2, [4, 1, 4] the average is 9 // 2 = 4; removing one 4 leaves [1, 4] for one computer with average 5, but the removed computer only runs 4 minutes. The answer is 4; it returned 5.
  3. Strict < passed.

Why strict is safe: if b > total / n, the new average (total - b) / (n - 1) is below total / n and hence below b, so the dedicated computer outlasts the rest; with equality that is not guaranteed.

Solution
#

class Solution:
    def maxRunTime(self, n: int, batteries: List[int]) -> int:
        batteries.sort()
        total = sum(batteries)
        while batteries[-1] > total // n:
            total -= batteries.pop()
            n -= 1
        return total // n

With n = 1 the average is the total, never below the largest battery, so the loop cannot bring n to 0.

Binary searching the answer T also works: check sum(min(b, T)) >= n * T, since each battery contributes at most T.

Complexity
#

Time O(m log m) for m batteries, space O(1) besides sorting.

Mistakes to Avoid
#

  • Subtract a removed battery from the total.
  • Do not remove a battery equal to the average; use strict greater-than.

Interview Explanation
#

Since batteries can be swapped freely, ideally all computers run for total charge divided by the number of computers. But a battery serves only one computer at a time, so if it exceeds the average I dedicate it to one computer, remove both, and recompute, until the largest battery fits under the average. The comparison must be strict, because removing a battery equal to the average can leave a higher average than it. Time O(m log m).

Related

LeetCode 1793: Maximum Score of a Good Subarray
·3 mins
LeetCode Hard Two-Pointers Greedy Binary-Search Array Monotonic-Stack
LeetCode 1793 note: expand from k toward the larger neighbor each step so the minimum stays as large as possible for each length; at a boundary, only the other side can grow
LeetCode 3413: Maximum Coins From K Consecutive Bags
·3 mins
LeetCode Medium Sliding-Window Greedy Sorting Prefix-Sum Binary-Search Array
LeetCode 3413 note: the best k bags always have one end on a segment boundary; compute “left end at a segment start” and “right end at a segment end” (the latter by mirroring and reusing the same function)
LeetCode 1402: Reducing Dishes
·2 mins
LeetCode Hard Dynamic-Programming Greedy Sorting Array
LeetCode 1402 note: sort and take dishes from most satisfying down; each new dish goes first and shifts the chosen ones one slot later, adding the current chosen sum, so keep going while that sum is positive
LeetCode 757: Set Intersection Size At Least Two
·2 mins
LeetCode Hard Greedy Sorting
LeetCode 757 note: sort by right end (larger left end first on ties) and track only the two rightmost points; add the two right-end points when uncovered, or the right end when only one is covered
LeetCode 2968: Apply Operations to Maximize Frequency Score
·2 mins
LeetCode Hard Sliding-Window Sorting Prefix-Sum Binary-Search Array
LeetCode 2968 note: after sorting, the cheapest common target for a block is its median; prefix sums give the cost in O(1), and it grows with the window, so slide
LeetCode 1838: Frequency of the Most Frequent Element
·2 mins
LeetCode Medium Sliding-Window Sorting Greedy Prefix-Sum Binary-Search Array
LeetCode 1838 note: after sorting, raising the whole window to nums[right] costs nums[right] × length − window sum; shrink the left while that exceeds k