N
gas stations are placed along a circular path and each station i
has available[i]
amount of gas.
There is a bus with an unlimited gas tank and requirement[i]
of gas is required to travel from station i
to the next station i+1
.
The bus starts with an empty tank from one of the gas stations.
Write a method to check whether you can travel around the circular route once else, return the starting gas station’s index (0 to n-1). If no route exists, return -1
.
Assume that a unique solution exists.
isRoute({1,3,4,5,4},{1,5,4,2,2}) ==> 2
isRoute({1,3,1,2,1},{1,4,2,2,2}) ==> -1
public static int isRoute(int[] available, int[] requirement){ }
C
Java
Python