Java Solution - very easy-to-follow


  • 0
    Z

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

        if(prices==null|prices.length==0)return 0;
        
        int profit = 0;
        int t1 = prices[0];
        int t2 = 0;
        
        for(int i=1;i<prices.length;i++){
            if(prices[i]<t1){
                t2 = t1;
                t1=prices[i];
            }else{
                profit = prices[i]-t1>profit? prices[i]-t1:profit;
            }
        }
        return profit;
    }
    

    }
    ...


Log in to reply
 

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