Commented dp solution


  • 0
    U
    public class Solution {// ith day maxprofit either equals new max profit or the max profit of the previous day.
    public int maxProfit(int[] prices) {
        if(prices.length==0)
            return 0;
        int[] profit= new int[prices.length];
        int lowest= prices[0];
        profit[0]=0;
        for(int i =1; i<prices.length;i++){
            profit[i]=(prices[i]-lowest>profit[i-1]?prices[i]-lowest:profit[i-1]);
            if(prices[i]<lowest)
                lowest=prices[i];
        }
        return profit[prices.length-1];
        
    }
    

    }


Log in to reply
 

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