Ctrl K

Keep Every Lamp Running

Hard
You have devices lamps and a set of batteries: battery i can power one lamp for charges[i] minutes. Batteries cannot be recharged. At the start you may put at most one battery into each lamp. After that, at any whole minute you may pull a battery out and put in another one — a fresh one or one taken from a different lamp; swaps are instant and unlimited. Return the largest whole number of minutes for which all devices lamps can shine at the same time.
Example 1
devices=3, charges=[4, 4, 4, 3] → 5

15 minutes of charge for three lamps is 5 each. No battery holds more than 5, so the charge can be shared evenly: 5.

Example 2
devices=2, charges=[20, 2, 3] → 5

The 20-minute battery could run one lamp for a long time, but the other lamp gets only 2 + 3 = 5 minutes, so both cannot shine longer than 5.

Example 3
devices=1, charges=[1, 1, 1] → 3

One lamp runs on all three batteries in turn: 3 minutes.

Constraints
  • 1 ≤ devices ≤ charges.length ≤ 10⁵
  • 1 ≤ charges[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLifters and Energy Drinks HardFlowerbed Beauty on a Budget HardCommon Setting at Different Step Prices HardShortest Server Uptime HardDeal Out From the Lowest Card Hard
loading Python…
Input
devices=3, charges=[4, 4, 4, 3]
Expected
5

Run checks the first 3 tests in your browser. Submit checks all 117 tests on the server, hidden ones included.