Gas Station

Medium (Trung bình) Python 🔗 Xem trên LeetCode

📋 Đề Bài

Chưa có mô tả.

💻 Lời Giải

Python 0134-gas-station.py
class Solution:
    def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
        n, total, surplus, start = len(gas), 0, 0, 0
        
        for i in range(n):
            total += gas[i] - cost[i]
            surplus += gas[i] - cost[i]
            if surplus < 0:
                surplus = 0
                start = i + 1
                
        return -1 if (total < 0) else start