Why remembering the lowest price so far turns every pair into one pass.
About this comparison
Two lanes hold the same eight days of prices, 7, 1, 5, 3, 6, 8, 2 and 4, drawn as a line so a low and a high read at a glance. Both look for the most money one buy and one later sell can make. Each lane does one subtraction per beat, and its counter keeps the score.
Every pair buys on one day and sells on each later day, with two loops, and keeps the best profit it has seen. It cannot stop early: the best pair could be the last one it tries, so it always prices all of them, O(n²).
One pass walks the days once and remembers one thing, the lowest price so far. On each day it asks what selling today would make against that low, and keeps the best. A new low is never a sale, it just becomes the price to beat. One look per day, O(n), and O(1) memory.
Every pair forgets everything between pairs, so it buys at 7 and checks all seven later days before it ever tries buying at 1. One pass carries the low forward: once it has seen 1, every later day is priced against 1 without looking back.
Both end on the same trade: buy at 1, sell at 8, +7. One pass gets there in 8 looks, one per day. Every pair needs 28, and most of them lose money, like buying at 7 and selling at 1.
The gap grows with the number of days: n looks against n(n - 1) / 2 pairs.
Use the one pass for Best Time to Buy and Sell Stock as it is asked: one buy, one later sell. Every pair is the first draft that is easy to trust, and the one to test the fast answer against on small inputs.