title: Maximum Profit By Buying And Selling A Share At Most K Times 1
signature: maximum_profit_by_buying_and_selling_a_share_at_most_k_times_1 <- function(price, n, k) {
test_code: 
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  validate_solution <- function(actual, expected) {
      toString(actual)==toString(expected)
  }
  
  driver <- function(price, n, k, expected){
      if (validate_solution(PLACEHOLDER_FN_NAME(price, n, k), expected)){
          "PASSED"
      } else{
          "FAILED"
      }
  }
  
  main <- function() {
      cat(sprintf("TEST-0...%s\n",driver(
          list(3L, 6L, 16L, 16L, 19L, 37L, 47L, 49L, 74L, 77L, 86L, 96L), 6L, 6L, 
          34L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(-6L, -70L, -26L, 78L, 98L, -72L, 48L, -94L, -38L, 52L, -50L, 58L, 84L, 16L, -74L, 32L, -44L, -50L, 68L, -48L, 28L, 94L, -26L, -96L, -42L, 96L, -24L, 42L, -70L, 10L, -16L, -32L, 98L, 38L, -2L, 26L, -26L, -78L, 44L, -72L, -56L, -22L), 31L, 32L, 
          1272L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L), 7L, 8L, 
          0L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(22L, 12L, 58L, 70L), 3L, 2L, 
          46L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-96L, -96L, -94L, -92L, -90L, -88L, -88L, -84L, -78L, -76L, -72L, -72L, -68L, -62L, -54L, -52L, -52L, -36L, -34L, -32L, -26L, -20L, -6L, -4L, -4L, 4L, 8L, 10L, 14L, 16L, 32L, 32L, 32L, 34L, 42L, 46L, 50L, 60L, 62L, 64L, 64L, 72L, 74L, 76L, 76L, 78L, 90L, 92L, 96L), 30L, 36L, 
          112L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(1L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 1L, 1L, 0L), 14L, 13L, 
          3L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(2L, 4L, 7L, 11L, 20L, 24L, 25L, 27L, 29L, 33L, 33L, 36L, 36L, 41L, 44L, 45L, 47L, 54L, 65L, 66L, 67L, 75L, 78L, 82L, 85L, 90L), 15L, 22L, 
          42L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(56L, 2L, -10L, -44L, 68L, 10L, -32L, -2L, -68L, 12L, -34L, -36L, 0L, 40L, -16L, -36L, 92L, 8L, -40L, -10L, 46L, 98L, 76L, -2L, 98L, -20L, 6L, 68L, 32L, -26L, -12L, 70L, 16L, -34L, -50L, -76L, -34L, -18L, 0L, -44L, -76L, 58L), 24L, 35L, 
          564L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 10L, 8L, 
          1L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(78L, 39L, 2L, 76L, 20L, 21L, 3L, 21L, 32L, 80L, 28L, 89L, 51L, 2L, 88L, 19L, 99L, 71L, 68L, 38L, 8L, 76L, 48L, 81L, 90L, 71L, 31L), 14L, 24L, 
          213L
      )))
  
  }
  
  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 ];
  }