Problem 394822 · hard · Level 03 Linear Management & Searching

Longest Run on a Bank of Batteries

binary search on the answer · greedy · math

You need to keep n machines running simultaneously. Each machine holds exactly one battery at a time, and batteries[i] is how many minutes battery i lasts. Batteries may be moved between machines at any moment, as often as you like, with no loss. Return the maximum number of whole minutes during which all n machines can run at the same time.

Examples

Input:  n = 2, batteries = [3, 3, 3]
Output: 4
Explanation: machine 1 uses battery A then half of C; machine 2 uses B then the other half of C.

Input:  n = 2, batteries = [1, 1, 1, 1]
Output: 2

Input:  n = 3, batteries = [10, 10, 3, 5]
Output: 8

Constraints

  • 1 <= n <= len(batteries) <= 10**5
  • 1 <= batteries[i] <= 10**9
  • Required time: O(m log(sum(batteries) / n)). The answer can exceed every single battery.

Goals

  • Recognise that battery swapping lets you cap each battery's contribution
  • Search a value range far larger than any single input
Starting Python…