Keep the Laptops Running
You have laptops laptops and a pile of batteries; battery i lasts batteries[i] minutes. A laptop runs on one battery at a time, and a battery powers one laptop at a time. Batteries can be swapped between laptops at any moment, as often as you like, and a swap takes no time. Batteries cannot be recharged.
Return the largest whole number of minutes for which all the laptops can run at the same time.
Example 1
laptops 2
batteries [5,2,2]
returns 4
One laptop runs on the 5 for four minutes. The other uses one 2 and then the other 2.
Example 2
laptops 2
batteries [10,1]
returns 1
The small battery lasts one minute, and the big one cannot power two laptops at once.
Constraints
1 <= laptops <= len(batteries) <= 3001 <= batteries[i] <= 10**9
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.