Java simple 2ms solution


  • 0
    S
     // eg. [20,10, 15, 5, 12, 15, 10]
            if(prices.length < 1 ) return 0;
            
            int min = prices[0]; int diff = 0; int max = 0;
            
            for(int i = 1; i < prices.length; i++){
                if(prices[i] < min) min = prices[i];
                diff = prices[i] - min;
                if(max < diff) max = diff;
            }
            
            return max;

Log in to reply
 

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