title: Maximum Profit By Buying And Selling A Share At Most K Times 1
signature: function maximumProfitByBuyingAndSellingAShareAtMostKTimes1(price: Array<number>, n: number, k: number): number {
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 ];
  }
question_info.test_list: [{"idx": 0, "outputs": 34, "inputs": {"price": [3, 6, 16, 16, 19, 37, 47, 49, 74, 77, 86, 96], "n": 6, "k": 6}}, {"idx": 1, "outputs": 1272, "inputs": {"price": [-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], "n": 31, "k": 32}}, {"idx": 2, "outputs": 0, "inputs": {"price": [0, 0, 0, 0, 0, 0, 0, 1, 1, 1], "n": 7, "k": 8}}, {"idx": 3, "outputs": 46, "inputs": {"price": [22, 12, 58, 70], "n": 3, "k": 2}}, {"idx": 4, "outputs": 112, "inputs": {"price": [-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], "n": 30, "k": 36}}, {"idx": 5, "outputs": 3, "inputs": {"price": [1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0], "n": 14, "k": 13}}, {"idx": 6, "outputs": 42, "inputs": {"price": [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], "n": 15, "k": 22}}, {"idx": 7, "outputs": 564, "inputs": {"price": [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], "n": 24, "k": 35}}, {"idx": 8, "outputs": 1, "inputs": {"price": [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1], "n": 10, "k": 8}}, {"idx": 9, "outputs": 213, "inputs": {"price": [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], "n": 14, "k": 24}}]
question_info.entry_cls_name: Solution
question_info.test_code: 
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  function convertToString(value: any): string{
      return JSON.stringify(value, function (key, val) {
          if (val instanceof Set) {
              return Array.from(val);
          } return val;});
  }
  
  function validateSolution(actual: number, expected: number): boolean {
      return convertToString(actual) === convertToString(expected);
  }
  
  function driver(price: Array<number>, n: number, k: number, expected:number): string {
      try{
          if (validateSolution(PLACEHOLDER_FN_NAME(price, n, k),expected)){
              return "PASSED";
          }
          return "FAILED";
      } catch (error) {
        return error.name;
      }
      
  }
      
  function main(){
    let result = '';
    result = driver([3, 6, 16, 16, 19, 37, 47, 49, 74, 77, 86, 96], 6, 6, 34);
    console.log(`TEST-0...${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);
    console.log(`TEST-1...${result}`);
  
    result = driver([0, 0, 0, 0, 0, 0, 0, 1, 1, 1], 7, 8, 0);
    console.log(`TEST-2...${result}`);
  
    result = driver([22, 12, 58, 70], 3, 2, 46);
    console.log(`TEST-3...${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);
    console.log(`TEST-4...${result}`);
  
    result = driver([1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0], 14, 13, 3);
    console.log(`TEST-5...${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);
    console.log(`TEST-6...${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);
    console.log(`TEST-7...${result}`);
  
    result = driver([0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1], 10, 8, 1);
    console.log(`TEST-8...${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);
    console.log(`TEST-9...${result}`);
  
  };
  
  main();
question_info.extension: ts
question_info.entry_fn_name: maximumProfitByBuyingAndSellingAShareAtMostKTimes1
question_info.test_case_ids: 0 1 2 3 4 5 6 7 8 9
question_info.commands: tsc --target es2020 --lib es5,dom,es2015,es2020 __FILENAME__
question_info.commands: node __FILENAME__.js