Soohan-Kim / Leetcode

0 stars 0 forks source link

[HW2] Gas Station #134 #18

Open Soohan-Kim opened 3 months ago

Soohan-Kim commented 3 months ago

https://leetcode.com/problems/gas-station/description/

Soohan-Kim commented 3 months ago
class Solution:
    def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
        if sum(gas) < sum(cost):
            return -1

        curgas, idx = 0, 0
        for i in range(len(gas)):
            curgas += gas[i] - cost[i] # cumulative net gas from start

            if curgas < 0: # reset starting point to next index
                curgas, idx = 0, i+1

        return idx