1 ms Java solution, beats 94% of java submissions


  • 1
    K
    public class Solution {
        public int maxProfit(int[] prices) {
            int l = prices.length;
            if( l < 2 )    return 0;
            int min = Integer.MAX_VALUE, profit=0;
            for(int i=0; i<l; i++){
                min = ( prices[i] < min) ? prices[i] : min; 
                profit = ( prices[i] - min > profit ) ? prices[i] - min : profit;
            }
            return profit;
        }
    }

Log in to reply
 

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