# 188. Best Time to Buy and Sell Stock IV

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LekNH5IywF8mjBxFcnu%2F-Mf2wdBcT5VuScEmKZWw%2F-Mf6iOsciIiyQuNk0cWE%2Fimage.png?alt=media\&token=bbaa8268-c471-4796-9c56-ade280f5c3f2)

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LekNH5IywF8mjBxFcnu%2F-Mf2wdBcT5VuScEmKZWw%2F-Mf6iRhS1a6vXPi8GpSB%2Fimage.png?alt=media\&token=176097a0-2063-4456-b188-4795bc2ba18e)

## dp

time: O(nk)

space: O(nk)

注意 n/2 這件事

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LekNH5IywF8mjBxFcnu%2F-Mf2wdBcT5VuScEmKZWw%2F-Mf6gehrK0JQNyC7T03o%2Fimage.png?alt=media\&token=0741dc7e-246f-4587-aaf3-e299b3ea65f0)

```java
class Solution {
    public int maxProfit(int k, int[] prices) {
        /*
            dp[i][k][0] = max(dp[i-1][k][0], dp[i-1][k][1] + prices[i])
            dp[i][k][1] = max(dp[i-1][k][1], dp[i-1][k-1][0] - prices[i])
        */
        
        if (prices == null || prices.length == 0 || prices.length < 2 || k == 0) return 0;
        
        int n = prices.length;
        
        if (k >= n/2) return maxProfit(prices);

        int dp[][][] = new int[n][k+1][2];

        for (int i = 0; i <= k; i++) {
            dp[0][i][0] = 0;
            dp[0][i][1] = -prices[0];
        }
        
        for (int i = 1; i < n; i++) {
            for (int j = 1; j <= k; j++) {
                dp[i][j][0] = Math.max(dp[i-1][j][0], dp[i-1][j][1] + prices[i]);
                dp[i][j][1] = Math.max(dp[i-1][j][1], dp[i-1][j-1][0] - prices[i]);
            }
        }
        
        return dp[n-1][k][0];
    }
    
    private int maxProfit(int[] prices) {
        int res = 0;
        for (int i = 1; i < prices.length; i++) {
            if (prices[i] > prices[i - 1]) {
                res += prices[i] - prices[i - 1];
            }
        }
        return res;
    }

}
```

## more simple

因為其實只用到 i 天和 i-1 的資料, 所以第一維可以去掉

time: O(nk)

space: O(nk)

```java
class Solution {
    public int maxProfit(int k, int[] prices) {
        /*
            dp[i][k][0] = max(dp[i-1][k][0], dp[i-1][k][1] + prices[i])
            dp[i][k][1] = max(dp[i-1][k][1], dp[i-1][k-1][0] - prices[i])
        */
        
        if (prices == null || prices.length == 0 || prices.length < 2 || k == 0) return 0;
        
        int n = prices.length;
        
        if (k >= n/2) return maxProfit(prices);

        
        int dp[][] = new int[k+1][2];

        for (int i = 0; i <= k; i++) {
            dp[i][0] = 0;
            dp[i][1] = -prices[0];
        }
        
        int res = Integer.MIN_VALUE;
        for (int i = 1; i < n; i++) {
            for (int j = 1; j <= k; j++) {
                dp[j][0] = Math.max(dp[j][0], dp[j][1] + prices[i]);
                dp[j][1] = Math.max(dp[j][1], dp[j-1][0] - prices[i]);
            }
        }
        
        for (int i = 1; i < n; i++) {
            for (int j = 0; j <= k; j++) {
                res = Math.max(res, dp[j][0]);
            }
        }
        return dp[k][0];
    }
    
    private int maxProfit(int[] prices) {
        int res = 0;
        for (int i = 1; i < prices.length; i++) {
            if (prices[i] > prices[i - 1]) {
                res += prices[i] - prices[i - 1];
            }
        }
        return res;
    }

}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://timmybeeflin.gitbook.io/cracking-leetcode/dynamic-programming/stock-tips/188.-best-time-to-buy-and-sell-stock-iv.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
