← All problems

Best Time to Buy and Sell Stock

easy

You're given an array prices where prices[i] is a stock's price on day i. You may buy on one day and sell on a later day, at most once. Return the maximum profit you can achieve, or 0 if no profit is possible.

Example

Input: prices = [7, 1, 5, 3, 6, 4]
Output: 5        // buy at 1, sell at 6
Loading...