Sharing my Java Solution, faster than First 2 most voted solutions


  • 0
    S

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


Log in to reply
 

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