5 lines AC method in C,easy to understand。


  • 0

    C code


        int maxDiff = 0, curMin = prices[0];
        for(int i = 1;i < pricesSize ; i++) {
            maxDiff = maxDiff > (prices[i] - curMin) ? maxDiff : (prices[i] - curMin);
            curMin = curMin < prices[i] ? curMin : prices[i];
        }
        return maxDiff;
    

    get the max difference by using current element minus the minimum number before.


Log in to reply
 

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