2Brute2Best
🔥0

Gas Station

Medium·LeetCode #134 Pro OnlySlight Variation
LeetCode
01

Problem Statement

There are n gas stations on a circular road. Station i gives gas[i] litres of fuel. Driving from station i to the next station takes cost[i] litres. Your tank has no size limit, and you start with an empty tank at one of the stations.

Return the index of a station where you can start and drive once all the way around the circle, going the same direction at every step. If no start works, return -1. The input is set up so that at most one answer exists.

Input

Two arrays gas and cost of the same length n.

Output

The index of the starting station, or -1 if no start works.

Example 1
Input: gas = [1, 2, 3, 4, 5], cost = [3, 4, 5, 1, 2]
Output: 3

Start at station 3 with 4 litres. Each leg uses its cost and the next station refills the tank. You reach station 3 again with exactly 0 litres left, so the trip works.

Example 2
Input: gas = [2, 3, 4], cost = [3, 4, 3]
Output: -1

The total fuel is 9, but the total cost is 10. No start can cover the whole circle.

Constraints
  • • 1 ≤ n ≤ 10^5
  • • `gas` and `cost` both have length `n`.
  • • 0 ≤ gas[i], cost[i] ≤ 10^4
  • • The input is set up so that the answer is unique, if there is one.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“circular route”
→ We drive around once, so the totals must balance, and the start can be any station.
“unlimited gas tank”
→ The fuel never caps out, so only the running balance matters.
“return the starting index, or -1”
→ We need one valid start, and the total tells us early whether any start can exist.
03

Approach Complexity Ladder

Brute Force — drive from every startO(N²)O(1)

Try each station as the start and simulate the whole loop

Optimal — one pass, move the start after each failureO(N)O(1)

Track the total and a running tank; when the tank drops below 0, the next station becomes the start

Brute Force Intuition

For each start s, drive around the circle, filling up at each station and then paying the cost to reach the next one. If the tank ever goes below 0, this start fails. If you finish the loop with the tank at 0 or more, return s. If no start passes, return -1.

Recognition rule for this sub-pattern:

Keep a running balance; when it fails, the start must move past this point. Two passes handle left and right rules.
Remember this · Greedy
Trigger
Maximise count / minimise cost with a natural "best next step", sorting makes the choice obvious, reachability.
Think
Is there a choice I can always make safely without looking ahead?
Key question
Why can't the greedy choice be worse than the optimal one? (Try to break it with a small example.)
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Greedy translate into production architectures and low-level memory mechanics.