🧠
Greedy with Total Gas Check and Reset Start
💡 This approach improves efficiency by using a greedy strategy that resets the start station when the tank goes negative, leveraging the insight that if you can't reach station j from i, no station between i and j can be a valid start.
Intuition
If total gas is less than total cost, no solution exists. Otherwise, track the tank while iterating; if tank drops below zero, reset start to next station and reset tank.
Algorithm
- Check if total gas is at least total cost; if not, return -1
- Initialize start = 0, tank = 0
- Iterate over stations, update tank += gas[i] - cost[i]
- If tank < 0, reset start to i+1 and tank to 0
- Return start after iteration
💡 The key insight is that a negative tank means the current start is invalid, so we move start forward.
def canCompleteCircuit(gas, cost):
if sum(gas) < sum(cost):
return -1
start = 0
tank = 0
for i in range(len(gas)):
tank += gas[i] - cost[i]
if tank < 0:
start = i + 1
tank = 0
return start
# Driver code
if __name__ == '__main__':
gas = [1,2,3,4,5]
cost = [3,4,5,1,2]
print(canCompleteCircuit(gas, cost)) # Output: 3
Line Notes
if sum(gas) < sum(cost):Check overall feasibility before proceeding
start = 0Initialize start index
tank += gas[i] - cost[i]Update tank with net gas at station i
if tank < 0:Reset start and tank if current start fails
public class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int totalGas = 0, totalCost = 0;
for (int i = 0; i < gas.length; i++) {
totalGas += gas[i];
totalCost += cost[i];
}
if (totalGas < totalCost) return -1;
int start = 0, tank = 0;
for (int i = 0; i < gas.length; i++) {
tank += gas[i] - cost[i];
if (tank < 0) {
start = i + 1;
tank = 0;
}
}
return start;
}
public static void main(String[] args) {
Solution sol = new Solution();
int[] gas = {1,2,3,4,5};
int[] cost = {3,4,5,1,2};
System.out.println(sol.canCompleteCircuit(gas, cost)); // Output: 3
}
}
Line Notes
if (totalGas < totalCost) return -1;Quick feasibility check
int start = 0, tank = 0;Initialize start and tank
tank += gas[i] - cost[i];Update tank with net gas
if (tank < 0) {Reset start and tank when tank negative
#include <iostream>
#include <vector>
using namespace std;
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int totalGas = 0, totalCost = 0;
for (int i = 0; i < gas.size(); i++) {
totalGas += gas[i];
totalCost += cost[i];
}
if (totalGas < totalCost) return -1;
int start = 0, tank = 0;
for (int i = 0; i < gas.size(); i++) {
tank += gas[i] - cost[i];
if (tank < 0) {
start = i + 1;
tank = 0;
}
}
return start;
}
int main() {
vector<int> gas = {1,2,3,4,5};
vector<int> cost = {3,4,5,1,2};
cout << canCompleteCircuit(gas, cost) << endl; // Output: 3
return 0;
}
Line Notes
if (totalGas < totalCost) return -1;Check if trip is possible overall
int start = 0, tank = 0;Initialize start and tank
tank += gas[i] - cost[i];Update tank with net gas at station i
if (tank < 0) {Reset start and tank if current start fails
function canCompleteCircuit(gas, cost) {
const totalGas = gas.reduce((a,b) => a+b, 0);
const totalCost = cost.reduce((a,b) => a+b, 0);
if (totalGas < totalCost) return -1;
let start = 0, tank = 0;
for (let i = 0; i < gas.length; i++) {
tank += gas[i] - cost[i];
if (tank < 0) {
start = i + 1;
tank = 0;
}
}
return start;
}
// Test
console.log(canCompleteCircuit([1,2,3,4,5], [3,4,5,1,2])); // Output: 3
Line Notes
if (totalGas < totalCost) return -1;Check overall feasibility
let start = 0, tank = 0;Initialize start and tank
tank += gas[i] - cost[i];Update tank with net gas
if (tank < 0) {Reset start and tank when tank negative
Single pass through the arrays with constant extra space.
💡 For n=100000, this means 100000 operations, which is efficient and scalable.
Interview Verdict: Accepted
This is the optimal and accepted approach for interviews.