134. Gas Station
Question
Gas Station with Math Proofs | Leetcode 134 - YouTube
Code
- if the current sum is negative, or turned negative, it would be not the start position, since the car cannot move further
- if it is positive, the res still doesn't change, since the answer is unique, and we need to find the one with max difference
class Solution(object):
def canCompleteCircuit(self, gas, cost):
if sum(gas) < sum(cost):
return -1
curSum = 0
res = 0
for i in range(len(gas)):
curSum += (gas[i] - cost[i])
if curSum < 0:
res = i + 1
curSum = 0
return res