Java Simple DP Solutions-3ms


  • 5
    C
    public int maxProfit(int[] prices) {
        if(prices.length<=1) return 0;
        int buy=Integer.MAX_VALUE;
        int sell=0;
        
        for(int i=0;i<prices.length;i++){
            sell=Math.max(sell,prices[i]-buy);
            buy=Math.min(buy,prices[i]);
        }
        return sell;
    }

  • 0

    The name of the variable is beautiful!


  • 3
    P

    @cheng48 I have a stupid question to ask.. but why is it a DP solution? Does DP require an array or something?
    Thanks in advance!


  • 0
    R

    The name of the variable is misleading and confusing. It's not "sell" that you're calculating, it's the "profit"....


Log in to reply
 

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