Loading
Loading
An electric shuttle drives a closed loop past n charging points, numbered 0 to n - 1 in driving order. At point i it can take on charge[i] units; driving from point i to the next point (wrapping from n - 1 back to 0) uses drain[i] units. The battery starts empty and is large enough never to fill up.
The shuttle picks a starting point, charges there, and must complete one full lap back to that point without the battery ever going negative on a leg. Return the smallest starting index that works, or -1 if no starting point can finish the lap.
charge = [1, 2, 3, 4, 5], drain = [3, 4, 5, 1, 2]start 0: 1 - 3 < 0fails on the first leg → try 1start 1: 2 - 4 < 0fails → try 2start 2: 3 - 5 < 0fails → try 3start 3: +3, +3 → 6points 3 and 4 build a surplustotal surplus 15 - 15 = 0 ≥ 0the lap closes from 33Sign in to keep a note here. It is free, and nobody else can see it.
Sign inExample 1
Sign in to open hints.
Struggling is where the learning happens. Open this once you have genuinely tried, or once the hints have run out.
O(n) time · O(1) space
Input. Run your code to watch it work on this input, one line at a time.
Running code is turned off on this deployment. You can still read the problem, write code, use hints and the visualizer.
Tab moves focus out of the editor. Press Ctrl+M to let Tab indent instead.
No results yet
Run your code against the sample tests, or submit to run the full suite.
Starting at 3 the battery reads 3, 6, 4, 2, 0 after each leg.
Example 2
Total charge 9 is less than total drain 10.