leetcode-study

134. Gas Station

class Solution:
    def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
        """
        Determines the starting gas station index to complete the circuit or returns -1 if not possible.
        
        Parameters:
            gas (List[int]): List containing the amount of gas available at each station.
            cost (List[int]): List containing the cost of gas to travel to the next station.
        
        Returns:
            int: The starting station's index if the circuit can be completed; otherwise, -1.
        """
        total_tank = 0   # Overall gas surplus across all stations.
        current_tank = 0 # Current gas surplus for the ongoing simulation from candidate start.
        start = 0        # Candidate starting station.
        
        # Iterate through each station to simulate traveling.
        for i in range(len(gas)):
            diff = gas[i] - cost[i]  # Net gas after leaving station i.
            total_tank += diff       # Update overall surplus/deficit.
            current_tank += diff     # Update running surplus for current starting candidate.
            
            # If current_tank is negative, the current start cannot reach station i+1.
            if current_tank < 0:
                start = i + 1      # Change candidate start to the next station.
                current_tank = 0   # Reset the current surplus.
        
        # If overall gas is sufficient, return the candidate start; otherwise, return -1.
        return start if total_tank >= 0 else -1

Summary of Techniques and Approaches: