code: def max_profit_with_k_transactions(prices, k):
      days = len(prices)
      if days < 2:
          # not enough days for a transaction
          return 0
  
      # transaction = buy + sell (2 separate days)
      # in a day you can sell and after that buy a share
      # (according to this, can't exists more transactions than the number of the prices/days)
      k = min(k, days)
      # create space optimized dp matrix
      dp = [[0 for j in range(days)] for i in range(2)]
  
      for t in range(k):
          max_prev = -math.inf
  
          # compute which row is previous and which is the current one
          prev_idx = (t - 1) % 2
          curr_idx = t % 2
  
          # the values in dp table for these days will be same
          # just ignore them, don't update them (because those combinations were tried)
          past_days = t
          # only save the last one
          dp[curr_idx][past_days] = dp[prev_idx][past_days]
  
          for d in range(past_days + 1, days):
              # first try to buy with the current price
              max_prev = max(
                  max_prev, dp[prev_idx][d - 1] - prices[d - 1]
              )
              # after that try to sell with the current price
              dp[curr_idx][d] = max(
                  dp[curr_idx][d - 1], max_prev + prices[d]
              )
  
      # return the last value from the last transaction
      return dp[(k - 1) % 2][-1]
  
label_desc: Use image node_id 1 for calling a global function with example usage: max_profit_with_k_transactions(prices, k) and returns: dp, int