diff --git a/work/1/Medium/Array/134.gas-station.py b/work/1/Medium/Array/134.gas-station.py index 2c0a148..b93c5d6 100644 --- a/work/1/Medium/Array/134.gas-station.py +++ b/work/1/Medium/Array/134.gas-station.py @@ -17,7 +17,7 @@ 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. - + Example 1: @@ -50,19 +50,31 @@ you only have 3. Therefore, you can't travel around the circuit once no matter where you start. - + Constraints: - • n == gas.length == cost.length + • n == gas.length == cost.length - • 1 <= n <= 10^5 + • 1 <= n <= 10^5 - • 0 <= gas[i], cost[i] <= 10^4 + • 0 <= gas[i], cost[i] <= 10^4 - • The input is generated such that the answer is unique. + • The input is generated such that the answer is unique. """ + class Solution: def canCompleteCircuit(self, gas: list[int], cost: list[int]) -> int: - + start, tank, total = 0, 0, 0 + + for i in range(len(gas)): + drive = gas[i] - cost[i] + tank = tank + drive + + if tank < 0: + tank = 0 + start = i + 1 + total = total + drive + + return start if total >= 0 else -1