Skip to content
BASA DSA

Best Time to Buy And Sell Stock

EasySliding Window

Choose one day to buy and a strictly later day to sell. Return the largest possible profit, or zero if no profitable trade exists.

Example 1

Input
prices = [9,2,6,1,7]
Output
6

Explanation

Buy at 1 on day 3 and sell at 7 on day 4 for profit 6.

Example 2

Input
prices = [5,4,2]
Output
0

Explanation

Prices only fall, so skipping a trade gives profit zero.

Example 3

Input
prices = [3]
Output
0

Explanation

One day provides no strictly later selling day.

Constraints

  • 1 <= len(prices) <= 100000
  • Prices are 0..10000

Function contractmaxProfitArguments: json
Loading draft…
SolutionRuns on your device
Loading draft…