Skip to content
BASA DSA

Best Time to Buy And Sell Stock With Cooldown

Medium2-D Dynamic Programming

Trade one share at a time with unlimited transactions. After selling, the next day is a cooldown during which you cannot buy. Return the maximum total profit; ending with no share is allowed.

Example 1

Input
prices = [1,2,3,0,2]
Output
3

Explanation

Buy at 1, sell at 2, cool down, then buy at 0 and sell at 2 for total 3.

Example 2

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

Explanation

Falling prices make doing nothing optimal.

Example 3

Input
prices = [1,4]
Output
3

Explanation

Buy at 1 and sell at 4 for profit 3.

Constraints

  • 1 <= len(prices) <= 5000
  • Prices are 0..1000

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