Easy to understand java solution


  • 2
    O
    public int maxProfit(int[] prices) {
        if(prices.length==0)return 0;
        int answer = 0;
        int min = prices[0];
        for(int i = 1; i<prices.length; i++){
            if(prices[i] < min) min = prices[i];
            if(prices[i] - min > answer) answer = prices[i] - min;
        }
        return answer;
    }

Log in to reply
 

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