小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

0188. Best Time to Buy and Sell Stock IV (H)

 頭號(hào)碼甲 2022-10-17 發(fā)布于北京

Best Time to Buy and Sell Stock IV (H)

題目

You are given an integer array prices where prices[i] is the price of a given stock on the ith day.

Design an algorithm to find the maximum profit. You may complete at most k transactions.

Notice that you may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).

Example 1:

Input: k = 2, prices = [2,4,1]
Output: 2
Explanation: Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2.

Example 2:

Input: k = 2, prices = [3,2,6,5,0,3]
Output: 7
Explanation: Buy on day 2 (price = 2) and sell on day 3 (price = 6), profit = 6-2 = 4. Then buy on day 5 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3. 

Constraints:

  • 0 <= k <= 10^9
  • 0 <= prices.length <= 10^4
  • 0 <= prices[i] <= 1000

題意

股票買(mǎi)賣問(wèn)題之四,允許最多k次交易(k次買(mǎi)入k次賣出)。

思路

解法在 0309. Best Time to Buy and Sell Stock with Cooldown 的基礎(chǔ)上增加一個(gè)k次交易的限制。

\(hold[i][j]\)表示在第i天仍持有股票,且最多已進(jìn)行了j次交易。
\(sold[i][j]\)表示在第i天未持有任何股票,且最多已進(jìn)行了j次交易。

可以得到如下遞推關(guān)系:

\[\begin{cases} hold[i][j]=max(sold[i-1][j-1]-prices[i],\ hold[i-1][j])\\\sold[i][j]=max(hold[i-1][j]+prices[i],\ sold[i-1][j]) \end{cases} \]

邊界條件是:\(\forall{j\ge1},\ hold[0][j]=-prices[0]\)

需要注意的是,case可能會(huì)使壞給一個(gè)巨大的k值,導(dǎo)致超時(shí)。這里的一個(gè)技巧是,當(dāng)k大于數(shù)組長(zhǎng)度一半時(shí)時(shí),問(wèn)題就等同于可以進(jìn)行不限次數(shù)的交易,可以直接用 0122. Best Time to Buy and Sell Stock II 中的一次遍歷方法解決。


代碼實(shí)現(xiàn)

Java

class Solution {
    public int maxProfit(int k, int[] prices) {
        if (prices.length == 0) {
            return 0;
        }

        if (k > prices.length / 2) {
            return maxProfit(prices);
        }

        int[][] hold = new int[prices.length][k + 1];
        int[][] sold = new int[prices.length][k + 1];

        for (int i = 0; i < prices.length; i++) {
            for (int j = 1; j <= k; j++) {
                if (i == 0) {
                    hold[i][j] = -prices[i];
                } else {
                    hold[i][j] = Math.max(hold[i - 1][j], sold[i - 1][j - 1] - prices[i]);
                    sold[i][j] = Math.max(hold[i - 1][j] + prices[i], sold[i - 1][j]);
                }
            }
        }

        return sold[prices.length - 1][k];
    }

    private int maxProfit(int[] prices) {
        int profit = 0;
        for (int i = 1; i < prices.length; i++) {
            if (prices[i] > prices[i - 1]) {
                profit += prices[i] - prices[i - 1];
            }
        }
        return profit;
    }
}

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多