Submissions disabled: This deployment is running in read-only mode for safety. Code execution is not available here.
← Back to all problems
Easy

Best Time to Buy and Sell Stock

Updated Jan 31, 2026

Problem

You are given an array prices where prices[i] is the price of a stock on the i-th day.

You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.

Return the maximum profit you can achieve. If you cannot achieve any profit, return 0.

Constraints

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

Examples

Example 1

Input: prices = [7, 1, 5, 3, 6, 4]
Output: 5

Example 2

Input: prices = [7, 6, 4, 3, 1]
Output: 0

Example 3

Input: prices = [2, 4, 1]
Output: 2

Function Signature

def maxProfit(self, prices: list[int]) -> int

How to Submit

Implement a Solution class with a maxProfit method.

Your method will be called with the input parameters and should return the answer.