Tap here to switch tabs
Problem
Ask
Submissions

Problem: Maximum Running Time of N Computers

hard
40 min
Explore how to calculate the maximum running time of n computers powered by a set of batteries. Learn to apply a modified binary search method to optimize battery usage by assigning and swapping batteries effectively, ensuring all computers run simultaneously for the longest duration possible.

Statement

You are given an integer, n, representing the number of computers, and a 0-indexed integer array, batteries, where batteries[i] denotes the number of minutes the ithi^{th} battery can power a computer.

Your goal is to run all n computers simultaneously for the maximum possible number of minutes using the available batteries.

Initially, you may assign at most one battery to each computer. After that, at any moment, you may remove a battery from a computer and replace it with another battery—either an unused battery or one taken from another computer. This replacement process can be repeated any number of times and takes no time.

Each battery can power any computer multiple times, but only until it is completely drained. Batteries cannot be recharged.

Return the maximum number of minutes you can run all n computers simultaneously.

Constraints:

  • 1 \leq n \leq batteries.length \leq 10510^{5}

  • 1 \leq batteries[i] \leq 10510^{5}

Tap here to switch tabs
Problem
Ask
Submissions

Problem: Maximum Running Time of N Computers

hard
40 min
Explore how to calculate the maximum running time of n computers powered by a set of batteries. Learn to apply a modified binary search method to optimize battery usage by assigning and swapping batteries effectively, ensuring all computers run simultaneously for the longest duration possible.

Statement

You are given an integer, n, representing the number of computers, and a 0-indexed integer array, batteries, where batteries[i] denotes the number of minutes the ithi^{th} battery can power a computer.

Your goal is to run all n computers simultaneously for the maximum possible number of minutes using the available batteries.

Initially, you may assign at most one battery to each computer. After that, at any moment, you may remove a battery from a computer and replace it with another battery—either an unused battery or one taken from another computer. This replacement process can be repeated any number of times and takes no time.

Each battery can power any computer multiple times, but only until it is completely drained. Batteries cannot be recharged.

Return the maximum number of minutes you can run all n computers simultaneously.

Constraints:

  • 1 \leq n \leq batteries.length \leq 10510^{5}

  • 1 \leq batteries[i] \leq 10510^{5}