Skip to content

Create Best Time to Buy and Sell Stock with Cooldown #1719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var maxProfit = function(prices) {
const memo = {}; // Initialize memoization object
return _maxProfit(prices, 0, false, memo); // Start the recursion with buy index 0 and no stock in hand
}

var _maxProfit = function(prices, day, hasStock, memo) {
if (day >= prices.length) return 0; // Base case: no more days left

const key = day + '_' + hasStock; // Using both day and stock status as the key for memoization
if (key in memo) return memo[key]; // Return memoized result if available

let maxProfit = 0;
// If we have the stock, we can either sell it or continue holding
if (hasStock) {
// Sell the stock and go to the next day with no stock
maxProfit = Math.max(prices[day] + _maxProfit(prices, day + 2, false, memo), _maxProfit(prices, day + 1, true, memo));
} else {
// If we don't have the stock, we can either buy it or skip the current day
maxProfit = Math.max(-prices[day] + _maxProfit(prices, day + 1, true, memo), _maxProfit(prices, day + 1, false, memo));
}

memo[key] = maxProfit; // Memoize the result
return maxProfit;
};
Loading