/134. Gas Station

134. Gas Station

Medium
Arrays47.6% acceptance

Given two integer arrays fuel_supply and travel_cost, each of length n, representing the amount of fuel available at each station and the cost to travel to the next station in a circular route, determine the starting station index from which a complete circuit can be made. If no such starting index exists, return -1. The solution is guaranteed to be unique if it exists.

Example 1

Input: fuel_supply = [4, 6, 7, 3], travel_cost = [6, 5, 3, 5]

Output: 1

Explanation: Starting at index 1, you can complete the circuit.

Example 2

Input: fuel_supply = [5, 1, 2, 3, 4], travel_cost = [4, 4, 1, 5, 1]

Output: 4

Explanation: Starting at index 4, you can complete the circuit.

Example 3

Input: fuel_supply = [2, 2, 2], travel_cost = [3, 3, 3]

Output: -1

Explanation: No starting index allows completing the circuit.

Constraints

  • 1 <= len(fuel_supply) == len(travel_cost) <= 105
  • 0 <= fuel_supply[i], travel_cost[i] <= 104
  • The answer, if it exists, is unique
Python (current runtime)

Case 1

Input: fuel_supply = [3, 1, 2, 5, 4], travel_cost = [2, 2, 2, 2, 2]

Expected: 3

Case 2

Input: fuel_supply = [1, 2, 3, 4], travel_cost = [2, 2, 2, 2]

Expected: 2

Case 3

Input: fuel_supply = [7, 1, 0, 11, 2], travel_cost = [5, 2, 3, 4, 6]

Expected: 3

Case 4

Input: fuel_supply = [0, 0, 0], travel_cost = [0, 0, 0]

Expected: 0

Case 5

Input: fuel_supply = [10, 0, 0, 0], travel_cost = [1, 1, 1, 8]

Expected: 0