Buy and Sell Stock: Optimal Time Complexity

Easy programming interview practice: Buy and Sell Stock: Optimal Time Complexity.

Given an array of stock prices where `prices[i]` is the price on day $i$, find the maximum profit from buying on one day and selling on a later day. You may complete at most one transaction (one buy and one sell). What is the optimal algorithm's time complexity?