Problem: Best Time to Buy and Sell Stock
Explore how to determine the best time to buy and sell stock by analyzing an array of prices. Learn to implement an efficient single-pass solution that tracks the minimum price and maximum profit. Understand how this greedy approach provides an optimal answer with linear time and constant space complexity.
We'll cover the following...
Statement
You are given an array prices where prices[i] represents the price of a stock on the
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
Note: You must buy the stock before you sell it, meaning the buy day must have a smaller index than the sell day.
Constraints: