FastPrepGas Station Circular Tour

Gas Station Circular Tour

Apple logoApple● MediumFULLTIMEOA
Learn

Problem statement

Petrol pumps numbered 0 through n - 1 lie on a circular route. Pump i provides gas[i] litres, and reaching the next pump consumes cost[i] litres. Your truck starts with an empty tank of unlimited capacity and collects the fuel at every pump it visits.

Return the smallest pump index from which the truck can complete one full clockwise circuit without its tank becoming negative. Return -1 if no starting pump works. Fuel may reach exactly zero.

Function

gasStation(gas: int[], cost: int[]) → int

Examples

Example 1

gas = [1,2,3,4,5]cost = [3,4,5,1,2]return = 3

Pump three accumulates enough fuel to cross the earlier deficits.

Example 2

gas = [2,3,4]cost = [3,4,3]return = -1

Total fuel is smaller than total travel cost.

Constraints

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

More Apple problems

See Apple hiring insights
public int gasStation(int[] gas, int[] cost) {
    // Write your solution here.
}
gas[1,2,3,4,5]
cost[3,4,5,1,2]
expected3
Checking account…