title: Maximum Profit By Buying And Selling A Share At Most K Times 1
signature: maximumProfitByBuyingAndSellingAShareAtMostKTimes1 :: [Integer] -> Integer -> Integer -> Integer
  maximumProfitByBuyingAndSellingAShareAtMostKTimes1 price n k = 
test_code: import Text.Printf
  import Control.Exception (catch, SomeException)
  import Data.Typeable
  import qualified Data.List as List
  import qualified Data.Map as Map
  import qualified Data.Set as Set
  
  -- SOLUTION CODE
  -- ============================================
  
  PLACEHOLDER_CODE_BODY
  
  -- TESTING CODE 
  -- ============================================
  validateSolution :: Integer -> Integer -> Bool
  validateSolution actual expected = do
      actual == expected
  
  
  driver :: [Integer] -> Integer -> Integer -> Integer -> String
  driver price n k expected = do
      if (validateSolution (PLACEHOLDER_FN_NAME price n k) expected)
          then "PASSED"
      else "FAILED"
  
  main = do
      printf "TEST-0...%s\n" (driver ([3, 6, 16, 16, 19, 37, 47, 49, 74, 77, 86, 96]) (6) (6) (34) )
  
      printf "TEST-1...%s\n" (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) )
  
      printf "TEST-2...%s\n" (driver ([0, 0, 0, 0, 0, 0, 0, 1, 1, 1]) (7) (8) (0) )
  
      printf "TEST-3...%s\n" (driver ([22, 12, 58, 70]) (3) (2) (46) )
  
      printf "TEST-4...%s\n" (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) )
  
      printf "TEST-5...%s\n" (driver ([1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0]) (14) (13) (3) )
  
      printf "TEST-6...%s\n" (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) )
  
      printf "TEST-7...%s\n" (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) )
  
      printf "TEST-8...%s\n" (driver ([0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1]) (10) (8) (1) )
  
      printf "TEST-9...%s\n" (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) )
  
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 ];
  }