134. 加油站(Medium)
# 题解
视频题解:https://www.bilibili.com/video/BV1wA411b7qZ?p=23
# Python示例
class Solution:
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
if sum(gas) < sum(cost): return -1
totalGas = 0
start = 0
for i in range(len(gas)):
totalGas = totalGas + gas[i] - cost[i]
if totalGas < 0:
start = i + 1
totalGas = 0
return start
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
编辑 (opens new window)
上次更新: 2022/10/25, 02:40:54