Problem
There are n
gas stations along a circular route, where the amount of gas at the ith
station is gas[i]
.
You have a car with an unlimited gas tank and it costs cost[i]
of gas to travel from the ith
station to its next (i + 1)th
station. You begin the journey with an empty tank at one of the gas stations.
Given two integer arrays gas
and cost
, return the starting gas station’s index if you can travel around the circuit once in the clockwise direction, otherwise return -1
. If there exists a solution, it is guaranteed to be unique.
Example
Examples
Example 1
|
|
Example 2
|
|
Solution
Understanding the Problem
Lets try to understand the problem. gas
array tells us gas at ith station, and cost
array tells us cost of reaching next station, or amount of fuel we will burn to reach next station. So to sum up:
- Each index in the gas array represents the amount of gas available to fill up your car with.
- Each index in the cost array represents the amount of gas that will be used when travelling from this gas station to next.
So, lets take eg. 1. We have array:
|
|
Lets calculate diff array:
|
|
So, we cannot start at index 0, 1 and 2 as cost is higher than the fuel. But if we have infinte fuel tank. So, we start at index 3, we can save 3 litres of gas and move to index 4. There also we save 3 liters. Then we use this total 6 litres on circular array’s indices - 0, 1 and 2.
So, sum(gas) >= sum(cost)
arrays.
Method 1 - Naive Solution
An intuitive solution involves testing each petrol station as the starting point and checking whether completing the circuit is possible. If a valid starting station is found, it is returned as the result.
The naive approach involves simulating the journey starting from each gas station:
- For each station, calculate the net fuel balance while traversing the circuit.
- If the fuel balance becomes negative during the traversal, terminate the journey prematurely and move to the next starting point.
- If the vehicle successfully completes the circuit, check if the fuel balance meets the criteria (
>= 0
) and return the start index.
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n^2)
, because for each starting stationO(n)
, it simulates a journey along the entire circuitO(n)
. - 🧺 Space complexity:
O(1)
Method 2 - Track Total Surplus and Current Surplus
The brute-force ran a simulation, such that, as soon as a gas balance became negative, it moved the car to the next station as a starting point. But this is inefficient and inorder for us to understand why? we have to look at what makes car stop.
Look at the array:
|
|
The first three stations (indices 0, 1, and 2) are invalid starting points, as they cannot sustain the journey even temporarily. This means fuel accumulation compared to consumption results in a surplus or, at the very least, breaks even before the failure. If the fuel deficit occurred anywhere earlier, the car would have stopped before reaching those stations.
Thus, if we fail to begin at the first station, indices 1, 2, and 3 are also not viable starting points. To succeed, the condition sum(gas) >= sum(cost)
must hold, which is tracked using totalSurplus
. A secondary variable, surplus
, keeps track of the local fuel balance and helps identify the viable starting index.
Approach
- Iterate through the stations and calculate the net gas balance (
gas[i] - cost[i]
). - Maintain two variables:
totalSurplus
: Tracks the overall fuel surplus across all stations to determine feasibility (sum(gas) >= sum(cost)
).surplus
: Tracks the local fuel balance during traversal to reset the starting index when the balance turns negative.
- If
totalSurplus
is negative at the end of the iteration, return-1
, as no valid starting station exists. Otherwise, return the identified starting index.
Code
|
|
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n)
, as it performs a single pass through the arrays while calculating fuel balances. - No nested iterations, unlike the brute-force approach.
- 🧺 Space complexity:
O(1)