0ms ,O(n),concise C solution


  • 1
    H
    int canCompleteCircuit(int* gas, int gsz, int* cost, int csz) {
        for(int start = 0; start < csz;start++)// enum the start-station
        {
            int tank =  gas[start]  - cost[start],now = (start + 1)%gsz,stop = start;
            while(tank >= 0 && now != stop)
            {
                tank += gas[now]  - cost[now];
                now++,start++;//update the start-station
                now %= gsz;
            }
           if(tank >= 0 && now == stop)
                return stop;
           if(stop!= start) start--;
       }
        return -1;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.