0
点赞
收藏
分享

微信扫一扫

[220121] Gas Station

infgrad 2022-02-07 阅读 70
class Solution:
    def canCompleteCircuit(self, gas, cost):

        # 记录总 gas 能不能支持全部旅程、当前 gas、起始站
        total = 0
        in_tank = 0
        start = 0

        for i in range(len(gas)):
            total += gas[i] - cost[i]
            in_tank += gas[i] - cost[i]

            # 重置起点位置
            if in_tank < 0:
                start = i + 1
                in_tank = 0

        return start if total >= 0 else -1

 

举报

相关推荐

0 条评论