309. Best Time to Buy and Sell Stock with Cooldown
Best Time to Buy and Sell Stock with Cooldown (LeetCode 309) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Dynamic Programming (DP) Patterns pattern, in the Stock problems subpattern.
- NeetCode 150 · #113 of 150 · 2-D Dynamic Programming
- NeetCode 250 · #196 of 250 · 2-D Dynamic Programming
How to solve Best Time to Buy and Sell Stock with Cooldown
Best Time to Buy and Sell Stock with Cooldown is a Stock Trading DP problem, part of Dynamic Programming (DP) Patterns. Solve stock trading problems with k transactions, cooldown, and transaction fees using DP.
Work towards the solution in these steps:
- Consider using dynamic programming to handle the sequential decision-making involved in buying and selling stocks.
- Identify states that represent whether you currently hold a stock, are in cooldown, or are free to buy, and think about how these states transition each day.
- Formulate recurrence relations that capture the maximum profit achievable for each state on day i, incorporating the cooldown constraint after selling.
The reference solution runs in O(n) time and O(1) space.
The Best Time to Buy and Sell Stock with Cooldown solution and editorial walks through the full approach with reference code.
Problem statement
You are given an array prices where prices[i] is the price of a given stock on the ith day.
Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the following restrictions: After you sell your stock, you cannot buy stock on the next day (i.e., cooldown one day).
> Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
Example 1:
Input: prices = [1,2,3,0,2] Output: 3 Explanation: transactions = [buy, sell, cooldown, buy, sell]
Example 2:
Input: prices = [1] Output: 0
Constraints:
- 1 <= prices.length <= 5000 - 0 <= prices[i] <= 1000
Topics and companies
Topics: Dynamic Programming.
Reported in interviews at Amazon, Yahoo.
How to practise Best Time to Buy and Sell Stock with Cooldown on Thita.ai
Starter code is provided in C, C#, C++, dart, elixir, erlang, Go, Java, JavaScript, Kotlin, PHP, Python, python3, racket, Ruby, Rust, Scala, Swift and TypeScript. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(1) space.
Open Best Time to Buy and Sell Stock with Cooldown in the code editor, or read the Best Time to Buy and Sell Stock with Cooldown editorial for a worked solution with its approach and complexity analysis.
Where Best Time to Buy and Sell Stock with Cooldown sits in the DSA pattern sheet
Problems related to Best Time to Buy and Sell Stock with Cooldown
Other problems that use the same Stock problems and Dynamic Programming (DP) Patterns techniques:
- Best Time to Buy and Sell Stock — Easy, same subpattern
- Best Time to Buy and Sell Stock II — Medium, same subpattern
- Best Time to Buy and Sell Stock III — Hard, same subpattern
- Best Time to Buy and Sell Stock IV — Hard, same subpattern
- Burst Balloons — Hard, same pattern
- Climbing Stairs — Easy, same pattern
Best Time to Buy and Sell Stock with Cooldown FAQ
What is the LeetCode number of Best Time to Buy and Sell Stock with Cooldown?
Best Time to Buy and Sell Stock with Cooldown is LeetCode problem 309, rated Medium.
What pattern is used to solve Best Time to Buy and Sell Stock with Cooldown?
Best Time to Buy and Sell Stock with Cooldown is a Stock Trading DP problem. It belongs to the Dynamic Programming (DP) Patterns pattern, in the Stock problems subpattern.
What is the time complexity of Best Time to Buy and Sell Stock with Cooldown?
The reference solution runs in O(n) time and O(1) space.
Which companies ask Best Time to Buy and Sell Stock with Cooldown?
Best Time to Buy and Sell Stock with Cooldown has been reported in interviews at Amazon, Yahoo.