← 返回 waymo 的题目列表Gas Station
类型:online_judge
Problem: Gas Station
There are n gas stations arranged in a circle. The i-th station contains gas[i] units of gas.
You have a car with an unlimited gas tank. Traveling from station i to station i + 1 costs cost[i] units of gas.
The car starts with an empty tank. Determine whether there exists a starting station such that the car can travel around the circuit exactly once.
Return the starting station index if possible; otherwise return -1.
It is guaranteed that if a solution exists, it is unique.
Input Format
n
gas[0] gas[1] ... gas[n-1]
cost[0] cost[1] ... cost[n-1]
Output Format
start_index
Constraints
1 <= n <= 10^5
0 <= gas[i], cost[i] <= 10^4
Example
Input:
5
1 2 3 4 5
3 4 5 1 2
Output:
3
Example
Input
5
1 2 3 4 5
3 4 5 1 2
Output
3