Java 1 ms, beats 95% submissions


  • 1
    J
    public class Solution {
        public int maxProfit(int[] prices) {
            if(prices.length < 2) return 0;
            int low = prices[0];
            int high;
            int max = 0;
            for(int i = 0; i < prices.length; i++){
                high = prices[i];
                if(low > high){
                    low = high;
                }else{
                    max = max > (high - low) ? max : (high - low);
                }
            }
            return max;
        }
    }
    

Log in to reply
 

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