Stock Time Machine
You've gained access to a dataset, tomorrowStocks
, which contains the stock prices from your favorite business on the NASDAQ. This dataset is a container indexed by minutes past opening. Each index contains the price of the stock at that time.
// Assume the stock market opens at 9:30AM EDT
// tomorrowStocks[] contains the prices of your target stock.
// If the stock is $22 @ 10:30AM EDT
tomorrowStocks[60] == 22
Output
Your task is to determine the best possible outcome of 1 purchase
and 1 sale
of 1 stock
from the given dataset.
Gotchas
- You must buy and sell exactly 1 stock.
- You may not buy and sell in the same time slot.
- You must buy before you sell.
Test Data
[1,2,3,4,5] # 4
[1,99,2,105] # 104
[99,1,99,100] # 99
[99,1,1,2,1,3] # 2
[5,4,3,3,1] # 0
[5,4,3,1] # -1
[5,2,1] # -1
[5,4,1] # -1
[55,45,20,1] # -10
[5,1] # -4
[10,7,5,1] # -2
[7] # Invalid input -- assume size >= 2
This is a code-golf; submit the shortest answer in your favorite language!
[5,4,3,1]
you can either but for5
and sell for4
or buy for4
and sell for3
to get the optimal result of-1
. \$\endgroup\$