@@ -0,0 +1,1303 @@
+/**%0A * Say you have an array for which the ith element is the price of a given stock on day i.%0A *%0A * Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and%0A * sell one share of the stock multiple times) with the following restrictions:%0A *%0A * You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).%0A * After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day) Example:%0A *%0A * prices = %5B1, 2, 3, 0, 2%5D maxProfit = 3 transactions = %5Bbuy, sell, cooldown, buy, sell%5D%0A *%0A * Tags: Dynamic Programming%0A * Similar Problems: (M) Best Time to Buy and Sell Stock, (M) Best Time to Buy and Sell Stock II%0A */%0Apublic class BestTimeToBuySellStockWithCooldown %7B%0A%0A    /**%0A     * https://leetcode.com/discuss/71354/share-my-thinking-process%0A     * O(n) Time, O(1) Space%0A     */%0A    public int maxProfit(int%5B%5D prices) %7B%0A        if (prices == null %7C%7C prices.length %3C 2) return 0;%0A        int sell = 0, lastSell = 0, buy = -prices%5B0%5D, lastBuy;%0A        for (int price : prices) %7B%0A            lastBuy = buy;%0A            buy = Math.max(lastSell - price, lastBuy);%0A            lastSell = sell;%0A            sell = Math.max(lastBuy + price, lastSell);%0A        %7D%0A        return sell;%0A    %7D%0A%7D%0A

add 309 best time to buy and sell stock with cooldown