bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 309. Best Time to Buy and Sell Stock with Cooldown

309. Best Time to Buy and Sell Stock with Cooldown

Leetcode Solutions

Dynamic Programming with State Machine

Algorithm

  1. Initialize held, sold, and reset arrays with a length of prices.length and set all values to 0.
  2. Set held[0] to -prices[0] to represent buying the stock on the first day.
  3. Iterate over the prices from day 1 to the last day:
    • Update sold[i] as the profit if selling the stock on day i.
    • Update held[i] as the maximum profit if holding the stock on day i.
    • Update reset[i] as the maximum profit if in cooldown on day i.
  4. Return the maximum of sold[n] and reset[n].
UML Thumbnail

Yet-Another Dynamic Programming Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...