class Solution:
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
n=len(gas)
for i in range(n):
if gas[i]<cost[i]:
continue
c=0
x=0
j=i
while c<n:
x+=gas[j]-cost[j]
if x<0:
break
c+=1
j=(j+1)%n
if c==n:
return i
if i==n-1:
return -1
力扣29——加油站
最新推荐文章于 2024-08-03 20:02:22 发布