134. Gas Station
solution
class Solution:
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
for i in range(len(gas)):
rest = gas[i] - cost[i]
next_index = (i + 1) % len(gas)
while rest > 0 and next_index != i:
rest += gas[next_index] - cost[next_index] # 注意这里是next_index
next_index = (next_index + 1) % len(gas)
if rest >= 0 and next_index == i:
return i
return -1Last updated