class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int res = 0;
int curCost = 0;
int total = 0;
for(int i = 0; i < gas.length; i++){
curCost += gas[i] - cost[i];
total += gas[i] - cost[i];
if(curCost < 0){
res = i+1;
curCost = 0;
}
}
if(total >= 0 && res != cost.length) return res;
else return -1;
}
}