Gas Station 🧠
LeetCode Link: Gas Station
Difficulty: Medium
Problem Explanation 📝
Problem: LeetCode 134 - Gas Station
Description: There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations. Return the starting gas station's index if you can travel around the circuit once in the clockwise direction, otherwise return -1.
Intuition: To determine if there exists a valid starting gas station, we can follow a greedy approach. If the total gas available is greater than or equal to the total cost of traveling the circuit, then there must be a starting gas station that allows us to complete the circuit without running out of gas.
Approach:
- Initialize variables
totalGas
andtotalCost
to store the total gas available and the total cost of traveling the circuit, respectively. - Initialize variables
currentGas
andstart
to store the current gas in the tank and the starting gas station index, respectively. - Iterate through the gas stations in a circular manner using a for loop.
- For each gas station
i
, calculate the differencediff
between the gas availablegas[i]
and the cost to travel to the next stationcost[i]
. Adddiff
tocurrentGas
to simulate traveling to the next station. - If
currentGas
becomes negative at any station, it means we cannot reach the next station. In this case, resetcurrentGas
to 0 and updatestart
to be the next station index. - Keep adding
diff
tototalGas
andtotalCost
as we traverse through the circular route. - At the end of the loop, check if
totalGas
is greater than or equal tototalCost
. If true, returnstart
as the starting gas station index. - If
totalGas
is less thantotalCost
, it means there is no valid starting gas station. Return -1.
⌛ Time Complexity: The time complexity is O(n), where n is the number of gas stations. We only traverse through the gas stations once.
💾 Space Complexity: The space complexity is O(1), as we use only a constant amount of extra space for the variables.
Solutions 💡
Cpp 💻
class Solution {
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
int totalGas = 0; // Total gas available
int totalCost = 0; // Total cost of traveling the circuit
int currentGas = 0; // Current gas in the tank
int start = 0; // Starting gas station index
for (int i = 0; i < gas.size(); i++) {
int diff = gas[i] - cost[i]; // Difference between gas available and cost to travel to the next station
totalGas += gas[i];
totalCost += cost[i];
currentGas += diff;
// If currentGas becomes negative, reset it to 0 and update start to be the next station index
if (currentGas < 0) {
currentGas = 0;
start = i + 1;
}
}
// If totalGas is greater than or equal to totalCost, there exists a valid starting gas station
if (totalGas >= totalCost) {
return start;
}
// If totalGas is less than totalCost, there is no valid starting gas station
return -1;
}
};
Python 🐍
class Solution:
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
total_gas = 0
current_gas = 0
start_station = 0
for i in range(len(gas)):
total_gas += gas[i] - cost[i]
current_gas += gas[i] - cost[i]
if current_gas < 0:
start_station = i + 1
current_gas = 0
return start_station if total_gas >= 0 else -1