← switch

134. Gas Station

medium

Array · Greedy

read the problem on leetcode →

first solved 11 Sept 2026

python
class Solution:
    def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
        if sum(gas) < sum(cost):
            return -1

        start = 0
        tank = 0

        for i in range(len(gas)):
            tank += gas[i] - cost[i]

            if tank < 0:
                start = i + 1
                tank = 0

        return start
Gas Station · switch