My Simple C program


  • 0
    J
    int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize) {
        	int *c=(int*)malloc(gasSize*sizeof(int));
        	int i;
        	int index;
        	int sum;
        	int n;
        	for(i=0;i<gasSize;i++)
        	{
        		c[i]=gas[i]-cost[i];
        	}
        	for(sum=0,index=0;index<gasSize;)
        	{
        	    for(i=index,n=0;n<gasSize;i++,n++)
        	    {
                    sum+=c[i%gasSize];
        			if(sum<0)
        			{
        				index=i+1;
        				break;
        			}
        	    }
        	    if(sum>=0)break;
        	    sum=0;
        	}
        	free(c);
        	if(index>=gasSize)index=-1;
        	return index;
    }

Log in to reply
 

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