[220121] Gas Station

infgrad

关注

阅读 70

2022-02-07

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)

0 0 举报