/* HackerRank: Prepare > Algorithms > Dynamic Programming > Stock Maximize Your algorithms have become so good at predicting the market that you now know what the share price of Wooden Orange Toothpicks Inc. (WOT) will be for the next number of days. Each day, you can either buy one share of WOT, sell any number of shares of WOT that you own, or not make any transaction at all. What is the maximum profit you can obtain with an optimum trading strategy? Example price = [1,2] Buy one share day one, and sell it day two for a profit of 1. Return 1. prices = [2,1] No profit can be made so you do not buy or sell stock those days. Return 0. Function Description Complete the stockmax function in the editor below. stockmax has the following parameter(s): - prices: an array of integers that represent predicted daily stock prices Returns - int: the maximum profit achievable Input Format The first line contains the number of test cases t. Each of the next pairs of lines contain: - The first line contains an integer , the number of predicted prices for WOT. - The next line contains n space-separated integers , each a predicted stock price for day . Constraints 1 <= t <= 10 1 <= n <= 50,000 1 <= prices[i] <= 100,000 Output Format Output t lines, each containing the maximum profit which can be obtained for the corresponding test case. Sample Input STDIN Function ----- -------- 3 q = 3 3 prices[] size n = 3 5 3 2 prices = [5, 3, 2] 3 prices[] size n = 3 1 2 100 prices = [1, 2, 100] 4 prices[] size n = 4 1 3 1 2 prices =[1, 3, 1, 2] Sample Output 0 197 3 Explanation For the first case, there is no profit because the share price never rises, return 0. For the second case, buy one share on the first two days and sell both of them on the third day for a profit of buy 1@1, buy 1@2, sell 2@100 = 200 - 2 - 1 = 197. For the third case, buy one share on day 1, sell one on day 2, buy one share on day 3, and sell one share on day 4. The overall profit is buy 1&1, sell 1@3, buy 1@1, sell 1@2 = 3 - 1 + 2 - 1 = 3. Possibly use recursion to explore all possible combinations? options: buy (b), sell (s), no transaction (n) prices = [1,3,1,2] Efficient Way: For share bought on day i, sell it on the highest price after day i. Traverse bacwards - start from last day and keep track of max price seen so far from right */ #include #include using namespace std; /* * Complete the 'stockmax' function below. * * The function is expected to return a LONG_INTEGER. * The function accepts INTEGER_ARRAY prices as parameter. */ long stockmax(vector prices) { for (const auto& i: prices) { cout << format("{} ", i); } cout << endl; auto print_dbg = [&prices](int i, long long profit, int sell_price) { cout << format(" index={}: profit = profit + (sell_price - prices[{}]) = {} + ({} - {}) = {}\n", i, i, profit, sell_price, prices[i], profit + (sell_price - prices[i]) ); }; long long profit = 0; int sell_price = 0; // future maximum selling price for (int i = (int)prices.size() - 1; i >= 0; --i) { // If current price > sell_price → update sell_price (this becomes a new selling opportunity) if (prices[i] > sell_price) { sell_price = prices[i]; cout << format(" index={}: sell_price: {} profit: {}\n", i, sell_price, profit); } else // you would buy here and sell later at sell_price → add (sell_price - prices[i]) to profit { print_dbg(i, profit, sell_price); profit += (long long)sell_price - prices[i]; } } cout << " profit = " << profit << endl; return profit; } long stockmax_bruteforce(vector prices) { for (const auto& i: prices) { cout << format("{} ", i); } cout << endl; long profit = 0; int numOfDays = (int)prices.size(); for(int i = 0; i < numOfDays; i++) { int maxprice = 0; for(int j = i; j < numOfDays; j++) { if(prices[j] > maxprice) { maxprice = prices[j]; } } cout << format(" index={}: profit = profit + maxprice - prices[{}] = {} + {} - {} = {}\n", i, i, profit, maxprice, prices[i],profit+(maxprice-prices[i])); profit = profit + (maxprice - prices[i]); } cout << " profit = " << profit << endl; return profit; } int main(void) { vector, int>> testCases = { // input output { {5,3,2}, 0 }, { {1,2,100}, 197 }, { {1,3,1,2}, 3 }, { {1,2,3,4}, 6 }, // profit = 3x4 - 1 + 2 + 3 = 6 { {1,2,1,2}, 2 }, }; cout << format("{:-<10} brute force O(n^2) {:-<10}\n", '-', '-'); for (const auto& tc: testCases) { vector input = get<0>(tc); [[maybe_unused]] long result = stockmax_bruteforce(input); long expected = get<1>(tc); EXPECT_EQ(result, expected); } cout << format("{:-<10} O(n) {:-<10}\n", '-', '-'); for (const auto& tc: testCases) { vector input = get<0>(tc); [[maybe_unused]] long result = stockmax(input); long expected = get<1>(tc); EXPECT_EQ(result, expected); } return 0; }