All problems
0454HardArrayGreedySorting

Smallest Starting Charge For A Round Of Calls

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1665Minimum Initial Energy to Finish Tasks

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A field engineer runs a round of service calls on one battery pack. calls[i] = [drain_i, floor_i] describes call i:

  • drain_i is the charge the call burns, deducted once the call is finished;
  • floor_i is the charge the pack must be showing at the moment the call is started, otherwise the diagnostic tool refuses to run it.

Every call must be made exactly once, and the engineer may take them in any order. The pack is not recharged during the round. Note that a call's threshold is never below its drain, so finishing a call never leaves the pack below zero.

Return the smallest charge the pack can start the round with so that some order gets through all of the calls.

Examples

Example 1

Input
calls = [[4, 9], [7, 8], [2, 6]]
Output
14

A pack starting on 14 gets through the round in the order [4,9], [2,6], [7,8]: 14 clears the threshold 9 and drops to 10, 10 clears 6 and drops to 8, 8 clears 8 and drops to 1.

Example 2

Input
calls = [[5, 5], [3, 3]]
Output
8

Each call burns exactly what its threshold demands, so the pack carries the full 8 into the round: it clears the first threshold, drops to 3, clears the second and finishes on 0.

Example 3

Input
calls = [[1, 10000], [6, 11], [9, 9]]
Output
10000

One call cannot start below 10000, and a pack on 10000 runs it down to 9999, then the call needing 11 down to 9993, then the call needing 9 down to 9984.

Constraints

  • 1 <= calls.length <= 10^5
  • calls[i].length == 2
  • 1 <= drain_i <= floor_i <= 10^4

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def smallest_starting_charge(calls: list[list[int]]) -> int:
Java
public int smallestStartingCharge(int[][] calls)
September 7
Apply