My easy understanding solution


  • 0

    My thought was trying to find the minimum one in the prices array and compare the difference after this min and find the max profit.

    public class Solution {
        public int maxProfit(int[] prices) {
            if(prices == null || prices.length == 0)
                return 0;
            int profit = 0;
            int min = prices[0];
            
            for(int i = 1; i < prices.length; i++){
                if(prices[i] < prices[i-1])
                    min = Math.min(prices[i], min);
                profit = Math.max(profit, prices[i]- min);
            }
            return profit;
        }
    }
    

Log in to reply
 

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