Minimum starting battery for a task queue

0
Sign in

A server runs a queue of jobs strictly in order, powered by one battery. Job i changes the battery by -usage[i]: a positive usage[i] drains the battery, and a negative one means the job recharges it (for example, a job that runs during an off-peak solar window).

The battery level must stay strictly greater than 0 at every moment: before the first job and after each job. Return the smallest integer starting level that lets the whole queue finish.

def min_start_battery(usage: list[int]) -> int: ...

Constraints

  • 0 <= len(usage) <= 10^5
  • -10^9 <= usage[i] <= 10^9
  • The answer can exceed 32 bits.

Example 1

usage = [4, 2, 7]
output = 14

Starting at 14, the levels are 10, 8, 1. Starting at 13 the last job leaves 0, which is not allowed.

Example 2

usage = [5, -3, 4, -6, 1]
output = 7

Starting at 7, the levels are 2, 5, 1, 7, 6. The lowest point comes after the third job, not at the end.

Part 2. The system also has one shield that can absorb up to shield units of a single job's drain. Return the smallest starting level when the shield is used as well as possible.

Reconstructed from the reported question; details may differ from what interviewers ask.

AmazonCodingEasy

Sign in to see the worked answer

The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.