class Solution:
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
n = len(gas)
oil = 0
for i in range(n):
if gas[i]<cost[i]:
continue
else:
oil = gas[i]
j=i
while oil>=0:
if j%n == i and j>i:
return j%n
break
if oil >= cost[j%n]:
oil = oil-cost[j%n]+gas[(j+1)%n]
j+=1
else:
break
return -1
class Solution:
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
cursum,totsum = 0,0
n = len(gas)
res = 0
for i in range(n):
cursum += gas[i]-cost[i]
totsum += gas[i]-cost[i]
if cursum<0:
res = i+1
cursum = 0
if totsum < 0:
return -1
else:
return res