Search⌘ K
AI Features

Problem: Best Time to Buy and Sell Stock

Explore how to determine the maximum profit from stock prices by choosing the best day to buy and sell using an efficient single-pass approach. Understand how to track minimum prices and update potential profits in an array to solve this classic problem with optimal time and space complexity.

Statement

You are given an array prices where prices[i] represents the price of a stock on the ithi^{th} day.

Your goal is to maximize your profit by selecting a single day to buy the stock and a different day in the future to sell it. The sell day must come strictly after the buy day.

Return the maximum profit achievable from this transaction. If no profit is possible, return 00.

Note: You must buy the stock before you sell it, meaning the buy day must have a smaller index than the sell day.

Constraints:

    ...