Skip to main content

Best Time to Buy and Sell Stock

EasyArrays & Strings

Given an array prices where prices[i] is the price of a stock on day i, find the maximum profit from a single transaction (buy then sell). If no profit is possible, return 0.

Example:

Input: prices = [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1), sell on day 5 (price = 6), profit = 6-1 = 5

Constraints:

  • 1 <= prices.length <= 10^5
  • 0 <= prices[i] <= 10^4