Trader: best time to buy and sell
Do you feel like trading today ?
Imagine you are in a bank as a trader and your boss wants you to make the bank earn a lot of money. You are given an array of stock prices. The ith element of the array corresponds to the price of a share of the stock on day i.
You are allowed to do at most one transaction, i.e buy a share of a stock and sell it after.
The objective is to find a solution of time complexity O(n) and memory complexity O(1) that returns the maximum profit.
For example, let [100 5 7 34 100 2] be the prices in dollars of the stock for 6 days. If I buy a share of the stock on day 2 and I sell it on day 5, I realize the maximum profit of 95$. The function must return 95.
Do you understand the problem ? Perfect then try to find the solution and come back in a while ;)