Here another simple 5ms solution


  • 0
    S
    #define min(x,y) ((y) + (((x) - (y)) & (((x) - (y)) >> (sizeof(int) * CHAR_BIT - 1))))
    #define max(x,y) ((x) - (((x) - (y)) & (((x) - (y)) >> (sizeof(int) * CHAR_BIT - 1))))
    
    int maxProfit(int prices[], int n) {
        int max_profit = 0;
        int i, curr_min; 
        if (n >= 2) {
            curr_min = prices[0];
            for (i = 1; i < n; i++) {
                curr_min = min(curr_min, prices[i]);
                max_profit = max(max_profit, prices[i] - curr_min);
            }
        }
        return max_profit;
    }

Log in to reply
 

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