Imagine you're on a road trip around a circular highway with n gas stations positioned along the route. Each gas station i has a specific amount of fuel available: gas[i] gallons.
Your car has an unlimited fuel tank but starts completely empty. The catch? It costs cost[i] gallons of fuel to drive from station i to the next station (i+1). Since the route is circular, after the last station, you return to the first one.
Your mission: Find a starting gas station where you can complete the entire circular journey exactly once, or determine if it's impossible.
Input: Two arrays gas[] and cost[] of length n
Output: The index of the starting gas station, or -1 if no solution exists
Guarantee: If a solution exists, it's unique!
Input & Output
Constraints
-
gas.length == n -
cost.length == n -
1 โค n โค 105 -
0 โค gas[i], cost[i] โค 104 - The solution is guaranteed to be unique if it exists