Clean Java Solution beat 95.21%


  • 1
    X
    public class Solution {
        public int maxProfit(int[] prices) {
            if(prices==null||prices.length==1) return 0;
            
            int[] minTo= new int[prices.length];
            int[] maxFrom= new int[prices.length];
    
            int min=Integer.MAX_VALUE;
            for(int i=0;i<prices.length;i++){
                min=min<prices[i]? min:prices[i];
                minTo[i]=min;
            }
            int max=0;
            for(int i=prices.length-1;i>=0;i--){
                max=max>prices[i]? max:prices[i];
                maxFrom[i]=max;
            }
            
            int maxProfit=0;
            for(int i=0;i<prices.length-1;i++){
                int profit=maxFrom[i+1] - minTo[i];
                maxProfit=maxProfit>profit? maxProfit:profit;
            }
    
            return maxProfit;
        }
    
    }

Log in to reply
 

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