A 4 ms C solution


  • 1
    int maxProfit(int* p, int ps) 
    {
    	int low, top, cur, max, gain;
    	max = cur = 0;
    	top = low = 0;
    	for (cur = 1; cur < ps; ++cur)
    	{
    		if (p[cur] < p[low])
    			top = low = cur;
    		if (p[cur]>p[top])
    			top = cur;
    		if (top>low)
    		{
    			gain = p[top] - p[low];
    			max = max > gain ? max : gain;
    		}	
    	}
    	return max;
    }

Log in to reply
 

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