134.加油站
力扣链接
class Solution:def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:curSum = 0 # 当前累计的剩余油量totalSum = 0 # 总剩余油量start = 0 # 起始位置for i in range(len(gas)):curSum += gas[i] - cost[i]totalSum += gas[i] - cost[i]if curSum < 0: # 当前累计剩余油量curSum小于0start = i + 1 # 起始位置更新为i+1curSum = 0 # curSum重新从0开始累计if totalSum < 0:return -1 # 总剩余油量totalSum小于0,说明无法环绕一圈return start