O(n) C Solution of 4ms


  • 2
    R
    int maxProfit(int* p, int pS){
        int i, last, dlast, temp;
    	for(dlast=0, last=0, i=1; i<pS; i++)
    	{
    		temp  = last;
    		last  = last+ p[i]-p[i-1] > dlast ? last+ p[i]-p[i-1] : dlast ;
    		dlast = temp>dlast ? temp : dlast;
    	}
    	return dlast > last ? dlast : last;
    }

Log in to reply
 

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