Open Soohan-Kim opened 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
https://leetcode.com/problems/gas-station/description/