图片说明

/**
  * 
  * @param gas int整型一维数组 
  * @param cost int整型一维数组 
  * @return int整型
  */
function canCompleteCircuit( gas ,  cost ) {
    // write code here
    var start = gas.length - 1;
    var end = 0;
    var sum = gas[start] - cost[start]
    while(start>end){
        if(sum>=0){
            sum += gas[end] - cost[end];
            ++end;
        }else{
            --start;
            sum += gas[start] - cost[start];
        }
    }
    return sum>=0 ? start : -1;
}
module.exports = {
    canCompleteCircuit : canCompleteCircuit
};