4 ms 17-line C solution


  • 0

    ・Number of multiplicand ↑, Absolute value of product ↑.

    ・When multiplicand == 0, the '0' will divide array into two part: Maximum of part 1 and part 2.

    int maxProduct(int* s, int ns) 
    {
    	int maxv = INT_MIN;
    	int headv = 1; //multiply elements from head to tail
    	int tailv = 1; //multiply elements from tail to head
    	int i, t;
    	for (i = 0; i < ns; ++i)
    	{
    		headv *= s[i];
    		tailv *= s[ns - 1 - i];
    		t = headv>tailv ? headv : tailv;
    		maxv = t > maxv ? t : maxv;
    		if (!headv) headv = 1; //if element == 0, array is divided, reset the product = 0.
    		if (!tailv) tailv = 1;
    	}
    	return maxv;
    }

Log in to reply
 

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