You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
Example 1
Input: prices = [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 1 (price = 1) and sell on day 4 (price = 6) for a profit of 6 - 1 = 5. Buying at 1 and selling at 7 is not allowed because you must buy before you sell.
Example 2
Input: prices = [7,6,4,3,1]
Output: 0
Explanation: Prices only go down, so every sale would lose money. The best choice is not to trade, giving a profit of 0.
Example 3
Input: prices = [2,4,1]
Output: 2
Explanation: Buy on day 0 (price = 2) and sell on day 1 (price = 4) for a profit of 2. The later price 1 comes too late to buy and resell.
At each day, the best profit if we *sell today* is `today - minSoFar`. Track `minSoFar` as we scan and maintain the global max of that difference.
Run checks all cases above. Submit evaluates all test cases.