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?
Buy one share day one, and sell it day two for a profit of . Return .
No profit can be made so you do not buy or sell stock those days. Return .
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
int: the maximum profit achievable
The first line contains the number of test cases .
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 .
Output lines, each containing the maximum profit which can be obtained for the corresponding test case.
For the first case, there is no profit because the share price never rises, return .
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 .
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 .