134. Gas Station

Question

CleanShot 2024-11-09 at 15.25.42.png
Gas Station with Math Proofs | Leetcode 134 - YouTube

Code

class Solution(object):
    def canCompleteCircuit(self, gas, cost):
        if sum(gas) < sum(cost):
            return -1

        curSum = 0
        res = 0
        for i in range(len(gas)):
            curSum += (gas[i] - cost[i])
            
            if curSum < 0:
                res = i + 1
                curSum = 0

        return res