There are n gas stations along a circular route, where the amount of gas at the ith station is gas[i].
You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from the ith station to its next (i + 1)th station. You begin the journey with an empty tank at one of the gas stations.
Given two integer arrays gas and cost, return the starting gas station’s index if you can travel around the circuit once in the clockwise direction, otherwise return -1. If there exists a solution, it is guaranteed to be unique.
solution
Consider the example below:

Our first thought is to check each station as a starting point in clockwise order, and then return the one that allows us to go the whole way around the loop. However, this takes time.
The key observation is that there is repeated work in the calculation above. See the example below:

We start at index 0 with an initial gas tank of 0, and then fill up with the 7 gas available at the station. We manage to make it to index 1, but can only reach index 2 with a negative gas tank.
The brute force solution would have us start our search over at index 1, but we can prove that there is no need to search this station.
Consider the general case in which our car manages to travel between stations and , but reaches a negative gas tank at index .
When we travelled from station to , we carried over an amount of gas such that
Now, if we were to brute-force and start our simulation again at , we know for sure that we are starting with 0 gas, which is .
Therefore, starting at index is equal or worse to starting at index . By induction, we conclude that all starting points are equal-to or worse than starting point .
As such, we should just continue our search at index .
class Solution:
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
if sum(cost) > sum(gas):
return -1
tank = 0
cur_idx = 0
start = 0
for i in range(len(gas)):
# if our tank went negative, reset
# and start search anew from here
if tank < 0:
tank = 0
start = i
tank += gas[cur_idx]
tank -= cost[cur_idx]
cur_idx += 1
return start