"If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit."
I misunderstood the above sentence as at most either buy or sell one share of stock on a single day. In that case, I buy at a local minimum price and sell it at a local maximum to reach a maximum total profit.
Please consider changing it to something like the following:
If you were only permitted to complete at most one buy and at most one sell during the whole period of days, design an algorithm such that your profit, i.e., sell price minus buy price, is maximized. Note that you can only not sell a stock before you buy one.
[1,2,3,4] ==> returns 3 (buy at 1 and sell at 4)
[4,3,2,1] ==> returns 0 (don't buy)
[4,10,25,2,10] ==> returns 21 (buy at 4 and sell at 25)
Same here, I don't know anything about stocks, nor I want to learn just to understand this problem, please be more verbose.
Another thing that might be worth doing is putting the whole problem into past tense (what was time when I could've achieved the most profit), or into magic future where you can predict stock prices.
Two others suggestions. First, the title of the question because the output is the max potential profit and not the 'best time'. Second, they should replace the phrase 'design an algorithm' with something like 'compute the optimal profit', since in a trading context an algorithm is usually understood to be an online algorithm (and this question is an offline question).
You are correct, the problem description is inaccurate. Your wording is freaking spot on. surprised leetcode hasnt fixed it yet.
Had the same confusion when I first encoutered BuyAndSell II, which I did before this one.
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.