2 pointer java solution


  • 0
    A

    public int maxProfit(int[] prices) {
    int i = prices.length - 1;
    int j = prices.length - 2;
    int maxDiff = 0;

        while(j>=0){
            if(prices[i]-prices[j] < 0){
                i=j;
                j--;
            }
            else{
                maxDiff = Math.max(maxDiff,prices[i]-prices[j]);
                j--;
            }
        }
        return maxDiff;
    }

Log in to reply
 

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