Advertisement

Gas Station - LeetCode 134 Solution

Gas Station - Complete Solution Guide

Gas Station is LeetCode problem 134, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

There are n gas stations along a circular route, where the amount of gas at the i th station is gas[i] . You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from the i th 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 so

Detailed Explanation

The Gas Station problem presents a scenario with 'n' gas stations arranged in a circle. Each station 'i' has a certain amount of gas, `gas[i]`. You have a car with an unlimited gas tank and need to travel around the circuit once, starting from one of the stations. The cost to travel from station 'i' to the next station '(i + 1)' is `cost[i]`. The goal is to find the starting station index that allows you to complete the circular route. If no such starting station exists, return -1. The problem guarantees a unique solution if one exists.

Solution Approach

The solution employs a greedy approach to efficiently find the starting station. It maintains two variables: `total_tank` to track the overall gas surplus (or deficit) and `current_tank` to track the gas in the tank while traversing the circuit. The algorithm iterates through the stations, calculating the net gain (gas - cost) at each station. If `current_tank` becomes negative, it indicates that the current starting station is not feasible. The algorithm resets `current_tank` to 0 and updates `start_station` to the next station (i+1). If `total_tank` remains negative after iterating through all stations, it means no solution exists. Otherwise, the final `start_station` is the answer.

Step-by-Step Algorithm

  1. Step 1: Initialize `total_tank` and `current_tank` to 0, and `start_station` to 0.
  2. Step 2: Iterate through the gas and cost arrays from i = 0 to n-1.
  3. Step 3: Calculate `net_gain = gas[i] - cost[i]` for the current station.
  4. Step 4: Update `total_tank` by adding `net_gain` to it: `total_tank += net_gain`.
  5. Step 5: Update `current_tank` by adding `net_gain` to it: `current_tank += net_gain`.
  6. Step 6: If `current_tank` becomes negative, it means we cannot reach the next station starting from the current `start_station`. Therefore, update `start_station = i + 1` and reset `current_tank = 0`.
  7. Step 7: After the loop finishes, check if `total_tank` is negative. If it is, return -1 because there's no solution.
  8. Step 8: If `total_tank` is non-negative, return `start_station` as the starting station.

Key Insights

  • Insight 1: If the total gas available is less than the total cost, a solution is impossible. This can be determined by summing `gas[i] - cost[i]` for all i. If the sum is negative, there's no solution.
  • Insight 2: If a starting point 's' allows you to reach station 'i' but not station 'i+1', then any starting point between 's' and 'i' cannot be a valid starting point. This is because you'd have even less gas when starting at stations between s and i. This insight allows for the greedy approach.
  • Insight 3: The problem guarantees a unique solution, which greatly simplifies the search. Without this guarantee, we would need to test all possible starting locations.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Array, Greedy.

Companies

Asked at: BNY Mellon, BitGo, Cisco, CureFit, Dream11, Flipkart, Freecharge, Goldman Sachs, Infosys, Lucid, Mastercard, Oracle, PhonePe, ServiceNow, Yahoo, Zepto.