Skip to content
Decoder

Gas Station

Medium

Problem

Gas stations sit on a circular road. Station i gives you gas[i] litres, and driving from station i to station i + 1 (wrapping around at the end) uses cost[i] litres. Your tank starts empty and has no limit. Return the index of the station where you can start and drive one full lap, or -1 if no start works. When an answer exists it is unique.

Example 1

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

Example 2

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

Constraints

  • n == gas.length == cost.length
  • 1 <= n <= 10^5
  • 0 <= gas[i], cost[i] <= 10^4

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.