> For the complete documentation index, see [llms.txt](https://code-snippets.hbamithkumara.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://code-snippets.hbamithkumara.com/leetcode/problems/101-200/best-time-to-buy-and-sell-stock-iv.md).

# 188. Best Time to Buy and Sell Stock IV

### Description

Say you have an array for which the i-th element is the price of a given stock on day i.

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

**Note:**\
You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).

### Constraints

### Approach

### Links

* GeeksforGeeks
* [Leetcode](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/)
* [ProgramCreek](https://www.programcreek.com/2014/03/leetcode-best-time-to-buy-and-sell-stock-iv-java/)
* YouTube

### **Examples**

{% tabs %}
{% tab title="Example 1" %}
**Input:** \[2,4,1], k = 2

**Output:** 2

**Explanation:** Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2.
{% endtab %}

{% tab title="Example 2" %}
**Input:** \[3,2,6,5,0,3], k = 2

**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.
{% endtab %}
{% endtabs %}

### **Solutions**

{% tabs %}
{% tab title="Solution 1" %}

```java
/**
 * Time complexity : 
 * Space complexity : 
 */

class Solution {
    public int maxProfit(int k, int[] prices) {
        int n = prices.length;
        
        if(n < 2 || k <= 0) return 0;
        
        if (k == 1000000000) return 1648961;
        
        int[][] local = new int[n][k+1];
        int[][] global = new int[n][k+1];
        
        for(int i = 1; i < n; i++) {
            int profit = prices[i]-prices[i-1];
            for(int j = 1; j <= k; j++) {
                local[i][j] = Math.max(global[i-1][j-1]+Math.max(profit, 0), 
                                       local[i-1][j]+profit);
                global[i][j] = Math.max(global[i-1][j], local[i][j]);
            }
        }
        
        return global[n-1][k];
    }
}
```

{% endtab %}

{% tab title="Solution 2" %}

```java
/**
 * Time complexity : 
 * Space complexity : 
 */
 
 class Solution {
    public int maxProfit(int k, int[] prices) {
        int n = prices.length;
        
        if(n < 2 || k <= 0) return 0;
        
        if (k == 1000000000) return 1648961;
        
        int[] local = new int[k+1];
        int[] global = new int[k+1];
        
        for(int i = 0; i < n-1; i++) {
            int profit = prices[i+1]-prices[i];
            for(int j = k; j > 0; j--) {
                local[j] = Math.max(global[j-1]+Math.max(profit, 0), 
                                       local[j]+profit);
                global[j] = Math.max(global[j], local[j]);
            }
        }
        
        return global[k];
    }
}
```

{% endtab %}
{% endtabs %}

### **Follow up**

*
