题目链接
https://leetcode.cn/problems/gas-station/description/
文章来源地址https://www.toymoban.com/news/detail-709033.html
代码
class Solution:
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
cursum = 0
minfuel = float('inf')
for i in range(len(gas)):
rest = gas[i] - cost[i]
cursum += rest
if cursum < minfuel:
minfuel = cursum
if cursum < 0:
return -1
if minfuel >= 0:
return 0
for i in range(len(gas) - 1, -1, -1):
rest = gas[i] - cost[i]
minfuel += rest
if minfuel >= 0:
return i
class Solution:
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
cursum = 0
total = 0
start = 0
for i in range(len(gas)):
cursum += gas[i] - cost[i]
total += gas[i] - cost[i]
if cursum < 0:
start = i + 1
cursum = 0
if total < 0:
return -1
return start
文章来源:https://www.toymoban.com/news/detail-709033.html
到了这里,关于LeetCode(力扣)134. 加油站Python的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!