How do you find the maximum profit from a single buy and sell, and why is the greedy approach correct?
Scan the prices once, keeping two values: the lowest price seen so far and the best profit found so far. At each price, consider selling today and buying at that lowest price. If the difference beats the best profit, update it. Then update the lowest price if today's price is lower.
The greedy argument is that the cheapest buying point must be earlier than the selling point, so while scanning forward you always know the optimal buy price for today. Any pair of days can be expressed as a selling day and the cheapest day before it, so by tracking the running minimum you implicitly consider the best partner for every possible selling day in one pass.
The order of operations matters in the code. You must compute the profit before you update the lowest price, otherwise a day could be treated as both the buy and the sell, which for a single-element array would produce a profit from buying and selling the same day.
The trade-off to name is that this handles exactly one transaction. If the interviewer allows multiple buys and sells, the algorithm changes to adding every positive difference between consecutive days, which is also a single pass. Stating which variant you solved, and how the others would change, is what separates a memorised answer from understanding.
Complexity-wise, the single-transaction version is O of n time and O of 1 space, and it is strictly better than the quadratic approach that tries every pair of days. The only reason you would ever choose the quadratic version is if you needed the actual buy and sell dates, which the greedy scan can also provide with two extra variables.