Tap here to switch tabs
Problem
Submissions

Problem: Best Time to Buy and Sell Stock

easy
15 min
Explore how to maximize profit by selecting the best days to buy and sell stocks using greedy techniques. Understand the problem constraints and develop an optimal linear time and constant space solution. This lesson helps you apply practical coding patterns to solve common interview questions efficiently.

Statement

Given an array, prices, where prices[i] represent the price of a stock on the i-th day, maximize profit by selecting a single day to buy the stock and a different day in the future to sell it.

Return the maximum profit that can be achieved from this transaction. If no profit can be made, return 0.

Constraints:

  • We can’t sell before buying a stock, that is, the array index at which stock is bought will always be less than the index at which the stock is sold.

  • 11 \leq prices.length \leq 10310 ^ 3

  • 00 \leq prices[i] \leq 10510^5

Unlock AI-Powered LearningUpgrade to smarter learning with instant explanations of Ask Agent, Personalized Interview Prep, Real-World Projects, 3 AI Mock Interviews per month, and Personalized Paths
Tap here to switch tabs
Problem
Submissions

Problem: Best Time to Buy and Sell Stock

easy
15 min
Explore how to maximize profit by selecting the best days to buy and sell stocks using greedy techniques. Understand the problem constraints and develop an optimal linear time and constant space solution. This lesson helps you apply practical coding patterns to solve common interview questions efficiently.

Statement

Given an array, prices, where prices[i] represent the price of a stock on the i-th day, maximize profit by selecting a single day to buy the stock and a different day in the future to sell it.

Return the maximum profit that can be achieved from this transaction. If no profit can be made, return 0.

Constraints:

  • We can’t sell before buying a stock, that is, the array index at which stock is bought will always be less than the index at which the stock is sold.

  • 11 \leq prices.length \leq 10310 ^ 3

  • 00 \leq prices[i] \leq 10510^5

Unlock AI-Powered LearningUpgrade to smarter learning with instant explanations of Ask Agent, Personalized Interview Prep, Real-World Projects, 3 AI Mock Interviews per month, and Personalized Paths