title: Maximum Profit By Buying And Selling A Share At Most Twice
signature: maximum_profit_by_buying_and_selling_a_share_at_most_twice <- function(price, n) {
test_code: 
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  validate_solution <- function(actual, expected) {
      toString(actual)==toString(expected)
  }
  
  driver <- function(price, n, expected){
      if (validate_solution(PLACEHOLDER_FN_NAME(price, n), expected)){
          "PASSED"
      } else{
          "FAILED"
      }
  }
  
  main <- function() {
      cat(sprintf("TEST-0...%s\n",driver(
          list(9L, 10L, 10L, 12L, 17L, 18L, 23L, 32L, 41L, 44L, 47L, 50L, 59L, 69L, 69L, 75L, 82L, 84L, 87L, 89L, 97L, 99L), 20L, 
          80L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(6L, 6L, 60L, 40L, 32L, -70L, -92L, 88L, 10L, -8L, -54L, 4L, 16L, 8L, -44L, 80L, -70L, 36L, 36L, -74L, -94L, 18L, -64L, -66L, -46L, 0L, -54L, -84L, 16L, -88L, -34L, -24L, 92L, 84L, 62L), 34L, 
          366L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 13L, 
          1L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(2L, 67L, 50L, 8L, 20L, 42L, 37L, 69L, 86L, 74L, 85L, 96L, 78L, 89L, 91L), 8L, 
          126L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-68L, -52L, -14L, -2L, 18L, 22L, 30L, 34L, 64L, 64L, 70L), 9L, 
          132L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(1L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 1L), 21L, 
          2L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(4L, 17L, 19L, 28L, 29L, 30L, 30L, 30L, 35L, 36L, 36L, 38L, 40L, 40L, 42L, 43L, 45L, 51L, 55L, 57L, 58L, 59L, 64L, 65L, 66L, 82L, 84L, 85L, 87L, 91L, 92L, 94L, 98L, 98L), 25L, 
          62L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(52L, 88L, -40L, 60L, 30L, 8L, -96L, 66L, -96L, -28L, -56L, -14L, 76L, -92L, 56L, 58L, 64L, -60L, -90L, 26L, 64L, -2L, 54L, -24L, 54L, -46L, -44L), 14L, 
          334L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 22L, 
          0L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(82L, 14L, 51L, 12L, 5L, 15L, 50L, 88L, 91L, 82L, 16L, 98L, 23L, 58L, 86L, 91L, 30L, 81L, 7L, 73L, 67L, 47L, 10L, 50L, 43L, 31L, 19L, 2L, 22L), 18L, 
          168L
      )))
  
  }
  
  main()
source_py: def maximum_profit_by_buying_and_selling_a_share_at_most_twice(price, n):
      profit = ([0] * n)
      max_price = price[(n - 1)]
      for i in range((n - 2), 0, (- 1)):
          if (price[i] > max_price):
              max_price = price[i]
          profit[i] = max(profit[(i + 1)], (max_price - price[i]))
      min_price = price[0]
      for i in range(1, n):
          if (price[i] < min_price):
              min_price = price[i]
          profit[i] = max(profit[(i - 1)], (profit[i] + (price[i] - min_price)))
      result = profit[(n - 1)]
      return result
source_cpp: int maximum_profit_by_buying_and_selling_a_share_at_most_twice(vector<int> price, int n) {
    int * profit = new int [ n ];
    for ( int i = 0;
    i < n;
    i ++ ) profit [ i ] = 0;
    int max_price = price [ n - 1 ];
    for ( int i = n - 2;
    i >= 0;
    i -- ) {
      if ( price [ i ] > max_price ) max_price = price [ i ];
      profit [ i ] = max ( profit [ i + 1 ], max_price - price [ i ] );
    }
    int min_price = price [ 0 ];
    for ( int i = 1;
    i < n;
    i ++ ) {
      if ( price [ i ] < min_price ) min_price = price [ i ];
      profit [ i ] = max ( profit [ i - 1 ], profit [ i ] + ( price [ i ] - min_price ) );
    }
    int result = profit [ n - 1 ];
    delete [ ] profit;
    return result;
  }