Sell, sell, sell!
Suppose we are given an array of
n integers which represent the value of some stock over time. Assuming you are allowed to buy the stock exactly once and sell the stock once, what is the maximum profit you can make? Can you write an algorithm that takes in an array of values and returns the maximum profit?
For example, if you are given the following array:
[2, 7, 1, 8, 2, 8, 14, 25, 14, 0, 4, 5]
The maximum profit you can make is 24 because you would buy the stock when its price is 1 and sell when it's 25. Note that we cannot make 25, because the stock is priced at 0 after it is priced at 25 (e.g you can't sell before you buy).
Subscribe to premium account to see the solution.Get premium now