Predict the output
What does this print?
prices = [7, 1, 5, 3, 6, 4]
low, best = prices[0], 0
for p in prices[1:]:
best = max(best, p - low)
low = min(low, p)
print(best, low)Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 5 1
Right: buy at 1 and sell at 6; the minimum stays 1.
The running minimum drops to 1 on day two and the best gap after it is 6 - 1.
Why not the others?
- B 6 1. 7 comes before 1, so 7 - 1 is a loss, not a profit of 6.
- C 4 1. Selling at 6 beats selling at 5.