Last updated on January 21st, 2025 at 02:05 am
Here, we see the Best Time to Buy and Sell Stock with Cooldown LeetCode Solution. This Leetcode problem is solved using different approaches in many programming languages, such as C++, Java, JavaScript, Python, etc.
List of all LeetCode Solution
Topics
Companies
Level of Question
Medium
Best Time to Buy and Sell Stock with Cooldown LeetCode Solution
Table of Contents
1. 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
2. Coding Pattern Used in Solution
The coding pattern used in this code is Dynamic Programming (DP). Specifically, it is a State Machine DP approach. The code models the problem as a state machine with transitions between states (buy, sell, cooldown) to maximize profit while adhering to the cooldown constraint.
3. Code Implementation in Different Languages
3.1 Best Time to Buy and Sell Stock with Cooldown C++
class Solution { public: int maxProfit(vector<int> &prices) { int buy(INT_MIN), sell(0), prev_sell(0), prev_buy; for (int price : prices) { prev_buy = buy; buy = max(prev_sell - price, buy); prev_sell = sell; sell = max(prev_buy + price, sell); } return sell; } };
3.2 Best Time to Buy and Sell Stock with Cooldown Java
class Solution { public int maxProfit(int[] prices) { int sell = 0, prev_sell = 0, buy = Integer.MIN_VALUE, prev_buy; for (int price : prices) { prev_buy = buy; buy = Math.max(prev_sell - price, prev_buy); prev_sell = sell; sell = Math.max(prev_buy + price, prev_sell); } return sell; } }
3.3 Best Time to Buy and Sell Stock with Cooldown JavaScript
var maxProfit = function(prices) { let [coolDown, sell, hold] = [0, 0, Number.NEGATIVE_INFINITY]; for( let stockPrice_Day_i of prices){ let [prevCoolDown, prevSell, prevHold] = [coolDown, sell, hold]; coolDown = Math.max(prevCoolDown, prevSell); sell = prevHold + stockPrice_Day_i; hold = Math.max( prevHold, prevCoolDown - stockPrice_Day_i ); } return Math.max(sell, coolDown); };
3.4 Best Time to Buy and Sell Stock with Cooldown Python
class Solution(object): def maxProfit(self, prices): if len(prices) < 2: return 0 sell, buy, prev_sell, prev_buy = 0, -prices[0], 0, 0 for price in prices: prev_buy = buy buy = max(prev_sell - price, prev_buy) prev_sell = sell sell = max(prev_buy + price, prev_sell) return sell
4. Time and Space Complexity
Time Complexity | Space Complexity | |
C++ | O(n) | O(1) |
Java | O(n) | O(1) |
JavaScript | O(n) | O(1) |
Python | O(n) | O(1) |
- This is a State Machine DP problem where the states are
buy
,sell
, andcooldown
. - The algorithm is efficient with O(n) time complexity and O(1) space complexity.
- The solution is implemented in a similar way across all four languages, with minor syntax differences.