Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

179. Best Time to Buy and Sell Stock with Cooldown

MediumDay 53 of 60Dynamic Programming

You are given prices, where prices[i] is the price of a stock on day i. You may complete as many buy and sell transactions as you like, but you can hold at most one share at a time, and after you sell you must wait one full day (a cooldown) before buying again. Return the maximum profit.

Example 1
Input: prices = [1,2,3,0,2]
Output: 3
Buy at 1, sell at 2, cool down, buy at 0, sell at 2: profit 1 + 2 = 3.
Example 2
Input: prices = [1]
Output: 0
Constraints
  • 1 <= prices.length <= 5000
  • 0 <= prices[i] <= 1000
Hints
Frequently asked at: Amazon, Google, Adobe
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min