You want to maximize your profit by choosing a single day to buy one stock and choosing a different future day to sell that stock.
Return the maximum profit you can achieve from this transaction. If no profit can be achieved, return 0. You must buy the stock before you sell it.
Example 1:
Input: prices = [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 when the price is 1 and sell on day 5 when the price is 6. Profit: 6 - 1 = 5. This becomes the maximum possible profit.
Example 2:
Input: prices = [7,6,4,3,1]
Output: 0
Explanation: The stock price keeps decreasing, so no profitable transaction is possible. Therefore, the maximum profit becomes: 0
Example 3:
Input: prices = [2,4,1]
Output: 2
Explanation: Buy at price 2 and sell at price 4. Profit: 4 - 2 = 2