title: Maximum Profit By Buying And Selling A Share At Most K Times 1
signature: function maximum_profit_by_buying_and_selling_a_share_at_most_k_times_1(price, n, k)
test_code: 
  
  -- SOLUTION CODE
  -- ============================================
  PLACEHOLDER_CODE_BODY
  
  -- TESTING CODE 
  -- ============================================
  function isCorrect(actual, expected)
      if type(expected) ~= type(actual) then
          return false
      end
      
      if type(expected) == 'table' then
          for k,v_expected in pairs(expected) do
              
              if actual[k] == nil then
                  return false
              end
              local v_actual = actual[k]
              if not isCorrect(v_actual, v_expected) then
                  return false
              end
              actual[k]=nil
          end
      
          if next(actual) then 
              return false
          end
          return true
      else
          return actual == expected
      end
  end
  
  function validateSolution(actual, expected)
      return isCorrect(actual,expected)
  end
  
  function driver(price, n, k, expected)
      local exec_result;
      local status, error_msg = pcall(function () 
          exec_result=PLACEHOLDER_FN_NAME(price, n, k) 
      end)
      if status then
          if validateSolution(exec_result,expected) then
              return "PASSED"
          end
          return "FAILED"
      else
          return string.format("ERROR=%s",string.gsub(error_msg,".*:[%d]+: ",""))
      end
  end
      
  function main()
      result = driver({3, 6, 16, 16, 19, 37, 47, 49, 74, 77, 86, 96}, 6, 6, 34)
      print(string.format("TEST-0...%s",result))
  
      result = driver({-6, -70, -26, 78, 98, -72, 48, -94, -38, 52, -50, 58, 84, 16, -74, 32, -44, -50, 68, -48, 28, 94, -26, -96, -42, 96, -24, 42, -70, 10, -16, -32, 98, 38, -2, 26, -26, -78, 44, -72, -56, -22}, 31, 32, 1272)
      print(string.format("TEST-1...%s",result))
  
      result = driver({0, 0, 0, 0, 0, 0, 0, 1, 1, 1}, 7, 8, 0)
      print(string.format("TEST-2...%s",result))
  
      result = driver({22, 12, 58, 70}, 3, 2, 46)
      print(string.format("TEST-3...%s",result))
  
      result = driver({-96, -96, -94, -92, -90, -88, -88, -84, -78, -76, -72, -72, -68, -62, -54, -52, -52, -36, -34, -32, -26, -20, -6, -4, -4, 4, 8, 10, 14, 16, 32, 32, 32, 34, 42, 46, 50, 60, 62, 64, 64, 72, 74, 76, 76, 78, 90, 92, 96}, 30, 36, 112)
      print(string.format("TEST-4...%s",result))
  
      result = driver({1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0}, 14, 13, 3)
      print(string.format("TEST-5...%s",result))
  
      result = driver({2, 4, 7, 11, 20, 24, 25, 27, 29, 33, 33, 36, 36, 41, 44, 45, 47, 54, 65, 66, 67, 75, 78, 82, 85, 90}, 15, 22, 42)
      print(string.format("TEST-6...%s",result))
  
      result = driver({56, 2, -10, -44, 68, 10, -32, -2, -68, 12, -34, -36, 0, 40, -16, -36, 92, 8, -40, -10, 46, 98, 76, -2, 98, -20, 6, 68, 32, -26, -12, 70, 16, -34, -50, -76, -34, -18, 0, -44, -76, 58}, 24, 35, 564)
      print(string.format("TEST-7...%s",result))
  
      result = driver({0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1}, 10, 8, 1)
      print(string.format("TEST-8...%s",result))
  
      result = driver({78, 39, 2, 76, 20, 21, 3, 21, 32, 80, 28, 89, 51, 2, 88, 19, 99, 71, 68, 38, 8, 76, 48, 81, 90, 71, 31}, 14, 24, 213)
      print(string.format("TEST-9...%s",result))
  
  end
  
  main()
source_py: def maximum_profit_by_buying_and_selling_a_share_at_most_k_times_1(price, n, k):
      profit = [[0 for i in range((n + 1))] for j in range((k + 1))]
      for i in range(1, (k + 1)):
          prevDiff = float('-inf')
          for j in range(1, n):
              prevDiff = max(prevDiff, (profit[(i - 1)][(j - 1)] - price[(j - 1)]))
              profit[i][j] = max(profit[i][(j - 1)], (price[j] + prevDiff))
      return profit[k][(n - 1)]
source_cpp: int maximum_profit_by_buying_and_selling_a_share_at_most_k_times_1(vector<int> price, int n, int k) {
    int profit [ k + 1 ] [ n + 1 ];
    for ( int i = 0;
    i <= k;
    i ++ ) profit [ i ] [ 0 ] = 0;
    for ( int j = 0;
    j <= n;
    j ++ ) profit [ 0 ] [ j ] = 0;
    for ( int i = 1;
    i <= k;
    i ++ ) {
      int prevDiff = INT_MIN;
      for ( int j = 1;
      j < n;
      j ++ ) {
        prevDiff = max ( prevDiff, profit [ i - 1 ] [ j - 1 ] - price [ j - 1 ] );
        profit [ i ] [ j ] = max ( profit [ i ] [ j - 1 ], price [ j ] + prevDiff );
      }
    }
    return profit [ k ] [ n - 1 ];
  }