Maximum Running Time of N Computers - Problem

Imagine you're managing a data center with n computers that need to run simultaneously for as long as possible. You have a collection of rechargeable batteries, each with different capacities measured in minutes.

Here's the challenge: You can hot-swap batteries between computers at any time - meaning you can remove a depleted battery from one computer and instantly insert it into another, or swap in a fresh battery. The goal is to keep all n computers running simultaneously for the maximum possible duration.

Input: An integer n (number of computers) and an array batteries where batteries[i] represents the capacity of the i-th battery in minutes.

Output: The maximum number of minutes you can keep all n computers running simultaneously.

Key Insight: This isn't about assigning batteries to computers - it's about optimally distributing the total battery capacity across time to maximize runtime!

Input & Output

example_1.py โ€” Basic Case
$ Input: n = 2, batteries = [3,3,3]
โ€บ Output: 4
๐Ÿ’ก Note: We have 2 computers and 3 batteries each with 3 minutes. Total capacity = 9 minutes. We can run 2 computers for 4 minutes (requiring 8 total minutes) by smart battery swapping. At minute 3, we swap batteries to keep both computers running for the 4th minute.
example_2.py โ€” Uneven Distribution
$ Input: n = 2, batteries = [1,1,1,1]
โ€บ Output: 2
๐Ÿ’ก Note: Total capacity = 4 minutes. We can run 2 computers for 2 minutes total (4 total minutes needed). We can achieve this by swapping the 1-minute batteries optimally.
example_3.py โ€” Large Battery Edge Case
$ Input: n = 3, batteries = [10,10,3,5]
โ€บ Output: 8
๐Ÿ’ก Note: Total capacity = 28 minutes. For 3 computers, we might think we can run for 28/3 โ‰ˆ 9 minutes, but the constraint is that each battery can contribute at most the target runtime. For 8 minutes: contributions are min(10,8) + min(10,8) + min(3,8) + min(5,8) = 8+8+3+5 = 24 โ‰ฅ 3ร—8. For 9 minutes: 8+8+3+5 = 24 < 3ร—9=27.

Constraints

  • 1 โ‰ค n โ‰ค 105
  • 1 โ‰ค batteries.length โ‰ค 105
  • 1 โ‰ค batteries[i] โ‰ค 109
  • You can hot-swap batteries between computers at any integer time moment

Visualization

Tap to expand
Data Center Power ManagementBattery10minBattery10minBattery3minBattery5minServer 1ONLINEServer 2ONLINEServer 3ONLINEBinary Search Result:Maximum Runtime: 8 minutesTotal Power Available: 28 minPower Required: 3 ร— 8 = 24 min โœ“
Understanding the Visualization
1
Identify the Constraint
Each battery can contribute at most T minutes to a T-minute solution due to hot-swapping logistics
2
Binary Search Setup
Search space is [0, total_battery_capacity / n_computers]
3
Feasibility Check
For runtime T: sum all min(battery_capacity, T) and check if โ‰ฅ n ร— T
4
Converge to Optimum
Binary search converges to maximum feasible runtime
Key Takeaway
๐ŸŽฏ Key Insight: The problem transforms from complex scheduling to simple binary search when you realize that if runtime T is achievable, then any runtime < T is also achievable (monotonic property).
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
78.5K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen