<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day, and an integer <code>k</code>.</p>

<p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k</code> times and sell at most <code>k</code> times.</p>

<p><strong>Note:</strong> You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

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

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> k = 2, prices = [3,2,6,5,0,3]
<strong>Output:</strong> 7
<strong>Explanation:</strong> 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.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= k &lt;= 100</code></li>
	<li><code>1 &lt;= prices.length &lt;= 1000</code></li>
	<li><code>0 &lt;= prices[i] &lt;= 1000</code></li>
</ul>

class Solution:
    def maxProfit(self, k: int, prices: List[int]) -> int:
        if k < 0:
            raise ValueError("k can not be negative!")

        if not k or not prices:
            return 0

        return self.max_profit_helper_bottom_up(prices, k)

        return self.max_profit_helper(prices, 0, 2 * k, True, {})


    # Ideally, an InputParam should be created, will refactor it if I get time.
    def max_profit_helper(self, prices, current_index, max_transaction, can_buy_stock, cache):
        if current_index >= len(prices) or max_transaction <= 0:
            return 0

        key = (current_index, max_transaction)

        if key in cache:
            return cache[key]
        
        if can_buy_stock:
            buy = -prices[current_index] + self.max_profit_helper(prices, current_index + 1, max_transaction - 1, False, cache)
            skip_buying = self.max_profit_helper(prices, current_index + 1, max_transaction, True, cache)
            cache[key] = max(buy, skip_buying)
        else:
            sell = prices[current_index] + self.max_profit_helper(prices, current_index + 1, max_transaction - 1, True , cache)
            skip_selling = self.max_profit_helper(prices, current_index + 1, max_transaction, False , cache)
            cache[key] = max(sell, skip_selling)

        return cache[key]

    
    def max_profit_helper_bottom_up(self, prices, k):
        if k < 0:
            raise ValueError("k can not be negative!")

        if not k or not prices:
            return 0

        n = len(prices)
        max_transaction = 2 * k

        # dp[day][remaining_transactions][can_buy] where can_buy is 1 for True, 0 for False
        dp = [[[0] * 2 for _ in range(max_transaction + 1)] for _ in range(n + 1)]

        # Fill the DP table from the end to the beginning
        for current_index in range(n - 1, -1, -1):
            for remaining_transactions in range(1, max_transaction + 1):
                # Case where we can buy
                dp[current_index][remaining_transactions][1] = max(
                    -prices[current_index] + dp[current_index + 1][remaining_transactions - 1][0],  # Buy stock
                    dp[current_index + 1][remaining_transactions][1]  # Skip buying
                )

                # Case where we can sell
                dp[current_index][remaining_transactions][0] = max(
                    prices[current_index] + dp[current_index + 1][remaining_transactions - 1][1],  # Sell stock
                    dp[current_index + 1][remaining_transactions][0]  # Skip selling
                )

        # We start on day 0, with 2*k transactions allowed, and the ability to buy
        return dp[0][max_transaction][1]
                


    def get_dp_grid(prices_length, k):
        dp_grid = []
        for row in range(prices_length  + 1):
            temp_dp = [0 for col in range(k + 1)]
            dp_grid.append(temp_dp)
        
        return dp_grid


        
        


        
39471