Minimum Initial Energy to Finish Tasks
Real-world version: Smallest Starting Charge For A Round Of CallsThe statement here is our own: the same inputs, outputs and technique.
A field engineer runs a round of service calls on one battery pack. calls[i] = [drain_i, floor_i] describes call i:
drain_iis the charge the call burns, deducted once the call is finished;floor_iis 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 starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def smallest_starting_charge(calls: list[list[int]]) -> int:public int smallestStartingCharge(int[][] calls)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 12 test cases: 12 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's