309.Best Time to Buy and Sell Stock with Cooldown

1- dp

https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/discuss/75928/?page=1

class Solution {
    public int maxProfit(int[] prices) {
        if(prices == null || prices.length < 2)
            return 0;
        int buy = -prices[0];
        int sell = Integer.MIN_VALUE;
        int rest = 0;
        for(int i = 1; i < prices.length; i++){
            int tmp = buy;
            buy = Math.max(buy, rest - prices[i]);
            rest = Math.max(rest, sell);
            sell = tmp + prices[i];
        }
        return Math.max(buy, Math.max(sell, rest));
    }
}

results matching ""

    No results matching ""