Java DP clean solution 1ms easy understand


  • 0
    S
    public int maxProfit(int[] prices) {
    	int buy = 0, sell = 0, cd = 0;
    	for (int i = 1; i < prices.length; i++) {
    		int preBuy = buy, preSell = sell;
    		buy = Math.max(preBuy, cd);
    		sell = Math.max(preBuy, preSell)+prices[i]-prices[i-1];
    		cd = Math.max(preSell, cd);
    	}
    	return sell>cd? sell:cd;
    }
    

Log in to reply
 

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