LeetCode 134 Gas Station - Implementation

The approach

Single pass. Reset start when tank goes negative.

def canCompleteCircuit(gas, cost): totalTank = currTank = 0 start = 0

for i in range(len(gas)):
    totalTank += gas[i] - cost[i]
    currTank += gas[i] - cost[i]
    if currTank < 0:
        start = i + 1
        currTank = 0

return start if totalTank >= 0 else -1

O(n)O(n) time, O(1)O(1) space.