LeetCode 134 Gas Station - Solution

The core idea

Two key observations:

1.1. If total gas < total cost, it's impossible. Otherwise, a solution exists.

2.2. If starting from station A you run out of gas at station B, then no station between A and B can be the answer (you'd run out even sooner).

Track cumulative balance. When it goes negative, reset the starting candidate to the next station. One pass suffices.