class Main {
public static void main(String[] args) {
int[] gas = { 1, 2, 3, 4, 5 };
int[] cost = { 3, 4, 5, 1, 2 };
Main main = new Main();
System.out.println("CanComplete:" + main.canCompleteCircuit(gas,cost););
}
public int canCompleteCircuit(int[] gas, int[] cost) {
int rest = 0, curr = 0, station = 0;
for(int i = 0 ; i < gas.length ; i++){
rest += gas[i] - cost[i];
curr += gas[i] - cost[i];
if(curr < 0){
station = i + 1;
curr = 0;
}
}
return rest < 0 ? -1 : station;
}
}