← problems
#121

Best Time to Buy and Sell Stock

EasySliding Window

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

You want to maximize your profit by choosing a single day to buy and a later day to sell. Return the maximum profit you can achieve. If no profit is possible, return 0.

Example

prices = [7, 1, 5, 3, 6, 4]
=> 5   // buy at 1 (day 1), sell at 6 (day 4)
Implement
function MaxProfit(prices: int[]) -> int

Found a bug? Or annoyed about something in the language? Tell us.

loading editor…