Search⌘ K
AI Features

Problem: Best Time to Buy and Sell Stock

Explore how to maximize profit from stock prices by selecting one day to buy and a later day to sell. Understand a single-pass greedy algorithm that tracks the minimum price and calculates the maximum achievable profit efficiently using arrays.

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.

Retu ...