Best Time to Buy and Sell Stock with Cooldown
Best Time to Buy and Sell Stock with Cooldown 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.
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
- Dynamic Programming (DP) Patterns pattern guide
- Stock problems subpattern
- The full DSA patterns sheet
- All coding practice problems
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
Opening the problem
Progress is saved as you study.