 All Problems
Gas Station
medium
array
greedy
amazon
google
bloomberg

There are n gas stations along a circular route. You have an array gas[i] and cost[i] where gas[i] is the fuel at station i and cost[i] is the fuel to travel to the next station.

Return the starting gas station's index if you can travel around the circuit once in the clockwise direction, otherwise return -1.

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
Run to check your code against the sample cases, or submit to run every case