# 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**

*


---

# 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://code-snippets.hbamithkumara.com/leetcode/problems/101-200/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.
