Ctrl K

The Battery Must Not Run Dry

Easy
An e-scooter rides a route made of several legs. The array changes says how its charge changes on each leg in order: a negative number is consumption, a positive one is recharging. The starting charge is a positive integer. After every leg the charge (the starting value plus all changes up to and including that leg) must stay **at least 1**. Return the smallest starting charge that keeps this true for the whole route.
Example 1
changes=[-2, 3, -6, 1] → 6

Starting with 5 gives 3, 6, 0: the charge hits zero on the third leg. Starting with 6 gives 4, 7, 1, 2, never below 1.

Example 2
changes=[4, 0, 2] → 1

The charge only grows, but the starting charge must be positive, so the answer is 1.

Example 3
changes=[-7] → 8
Constraints
  • 1 ≤ len(changes) ≤ 10⁵
  • -100 ≤ changes[i] ≤ 100
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBalance Point Index EasyBalance After Each Day EasyTotals of Odd-Length Slices EasyThe Hiker's Highest Point EasyThe Busiest Year Easy
loading Python…
Input
changes=[-2, 3, -6, 1]
Expected
6

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