Clearly, simple java solution


  • 4
    N

    public class Solution {
    public int maxProfit(int[] prices) {

        if(prices==null || prices.length==0)
            return 0;
        
        int buyPrice = prices[0];
        int max = 0;
        
        int len = prices.length;
        for(int i=0;i<len;i++){
            
            if(prices[i]<buyPrice)
                buyPrice = prices[i];
            else{
                int newProfit = prices[i]-buyPrice ;
                max = ( newProfit>max ? newProfit : max);
            }
        }
        
        return max;
    }
    

    }


  • 0
    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;
            int max=0;
            for(int i=0;i<prices.length;i++){
                min=min<prices[i]? min:prices[i];
                minTo[i]=min;
            }
            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.