Very simple O(n) solution


  • 4
    R
    int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize) {
        int sum=0,minsum=0,minind=-1,i;
        for(i=0;i<gasSize;i++)
        {sum=sum+*(gas+i)-*(cost+i);
           if(sum<minsum)
           {minsum=sum;
              minind=i ;
           } }
           if(sum<0)return -1;
           return minind+1;  
    }

Log in to reply
 

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