Below is the implementation of the stock trader with cooldown:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109/** * Find the maximum profit with cooldown. * State Machine approach. * * @param {number[]} prices - Array of stock prices * @return {number} - Maximum profit */function maxProfit(prices) { if (prices.length <= 1) { return 0; } let hold = -prices[0]; // Maximum profit with a stock let sold = 0; // Maximum profit after selling let rest = 0; // Maximum profit at rest for (let i = 1; i < prices.length; i++) { // We need to use temporary variables to avoid using updated values in the same iteration const prevHold = hold; // Continue holding or buy hold = Math.max(hold, rest - prices[i]); // Sell the stock sold = prevHold + prices[i]; // Continue resting or come from sold rest = Math.max(rest, sold); } return Math.max(sold, rest);} /** * Find the maximum profit with cooldown. * Dynamic Programming approach. * * @param {number[]} prices - Array of stock prices * @return {number} - Maximum profit */function maxProfitDP(prices) { if (prices.length <= 1) { return 0; } const n = prices.length; const buy = new Array(n).fill(0); const sell = new Array(n).fill(0); const cooldown = new Array(n).fill(0); buy[0] = -prices[0]; for (let i = 1; i < n; i++) { // Buy: continue from previous buy state or buy after cooldown buy[i] = Math.max(buy[i - 1], cooldown[i - 1] - prices[i]); // Sell: continue from previous sell state or sell the stock we bought sell[i] = Math.max(sell[i - 1], buy[i - 1] + prices[i]); // Cooldown: continue from previous cooldown state or enter cooldown after selling cooldown[i] = Math.max(cooldown[i - 1], sell[i - 1]); } return Math.max(sell[n - 1], cooldown[n - 1]);} /** * Find the maximum profit with cooldown. * Optimized Dynamic Programming approach. * * @param {number[]} prices - Array of stock prices * @return {number} - Maximum profit */function maxProfitOptimized(prices) { if (prices.length <= 1) { return 0; } let buy = -prices[0]; let sell = 0; let cooldown = 0; for (let i = 1; i < prices.length; i++) { // We need to use temporary variables to avoid using updated values in the same iteration const prevBuy = buy; const prevSell = sell; // Buy: continue from previous buy state or buy after cooldown buy = Math.max(buy, cooldown - prices[i]); // Sell: continue from previous sell state or sell the stock we bought sell = Math.max(sell, prevBuy + prices[i]); // Cooldown: continue from previous cooldown state or enter cooldown after selling cooldown = Math.max(cooldown, prevSell); } return Math.max(sell, cooldown);} // Test casesconsole.log(maxProfit([1, 2, 3, 0, 2])); // 3console.log(maxProfit([1])); // 0 console.log(maxProfitDP([1, 2, 3, 0, 2])); // 3console.log(maxProfitDP([1])); // 0 console.log(maxProfitOptimized([1, 2, 3, 0, 2])); // 3console.log(maxProfitOptimized([1])); // 0
Let's break down the implementation:
Implement the stock trader with cooldown solution in different programming languages.
Below is the implementation of the stock trader with cooldown in different programming languages. Select a language tab to view the corresponding code.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109/** * Find the maximum profit with cooldown. * State Machine approach. * * @param {number[]} prices - Array of stock prices * @return {number} - Maximum profit */function maxProfit(prices) { if (prices.length <= 1) { return 0; } let hold = -prices[0]; // Maximum profit with a stock let sold = 0; // Maximum profit after selling let rest = 0; // Maximum profit at rest for (let i = 1; i < prices.length; i++) { // We need to use temporary variables to avoid using updated values in the same iteration const prevHold = hold; // Continue holding or buy hold = Math.max(hold, rest - prices[i]); // Sell the stock sold = prevHold + prices[i]; // Continue resting or come from sold rest = Math.max(rest, sold); } return Math.max(sold, rest);} /** * Find the maximum profit with cooldown. * Dynamic Programming approach. * * @param {number[]} prices - Array of stock prices * @return {number} - Maximum profit */function maxProfitDP(prices) { if (prices.length <= 1) { return 0; } const n = prices.length; const buy = new Array(n).fill(0); const sell = new Array(n).fill(0); const cooldown = new Array(n).fill(0); buy[0] = -prices[0]; for (let i = 1; i < n; i++) { // Buy: continue from previous buy state or buy after cooldown buy[i] = Math.max(buy[i - 1], cooldown[i - 1] - prices[i]); // Sell: continue from previous sell state or sell the stock we bought sell[i] = Math.max(sell[i - 1], buy[i - 1] + prices[i]); // Cooldown: continue from previous cooldown state or enter cooldown after selling cooldown[i] = Math.max(cooldown[i - 1], sell[i - 1]); } return Math.max(sell[n - 1], cooldown[n - 1]);} /** * Find the maximum profit with cooldown. * Optimized Dynamic Programming approach. * * @param {number[]} prices - Array of stock prices * @return {number} - Maximum profit */function maxProfitOptimized(prices) { if (prices.length <= 1) { return 0; } let buy = -prices[0]; let sell = 0; let cooldown = 0; for (let i = 1; i < prices.length; i++) { // We need to use temporary variables to avoid using updated values in the same iteration const prevBuy = buy; const prevSell = sell; // Buy: continue from previous buy state or buy after cooldown buy = Math.max(buy, cooldown - prices[i]); // Sell: continue from previous sell state or sell the stock we bought sell = Math.max(sell, prevBuy + prices[i]); // Cooldown: continue from previous cooldown state or enter cooldown after selling cooldown = Math.max(cooldown, prevSell); } return Math.max(sell, cooldown);} // Test casesconsole.log(maxProfit([1, 2, 3, 0, 2])); // 3console.log(maxProfit([1])); // 0 console.log(maxProfitDP([1, 2, 3, 0, 2])); // 3console.log(maxProfitDP([1])); // 0 console.log(maxProfitOptimized([1, 2, 3, 0, 2])); // 3console.log(maxProfitOptimized([1])); // 0
First, understand that we need to find the maximum profit from buying and selling stocks with a cooldown period of one day after selling.
Define state variables to track the maximum profit in different states: holding a stock, just sold a stock, and resting (not holding and not in cooldown).
Iterate through the array of prices, updating the state variables based on the possible actions: buy, sell, or do nothing.
Ensure that after selling a stock, we cannot buy on the next day (cooldown period).
Return the maximum profit, which will be the maximum of the sold and rest states.
Modify the code to implement an alternative approach and test it with the same examples.
Implement a function that solves the stock trader with cooldown problem using a different approach than shown above.
Handle the case where the input array is empty (return 0).
Handle the case where the input array has only one element (return 0).
Handle the case where prices are strictly decreasing (return 0).
Below is the implementation of the stock trader with cooldown:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109/** * Find the maximum profit with cooldown. * State Machine approach. * * @param {number[]} prices - Array of stock prices * @return {number} - Maximum profit */function maxProfit(prices) { if (prices.length <= 1) { return 0; } let hold = -prices[0]; // Maximum profit with a stock let sold = 0; // Maximum profit after selling let rest = 0; // Maximum profit at rest for (let i = 1; i < prices.length; i++) { // We need to use temporary variables to avoid using updated values in the same iteration const prevHold = hold; // Continue holding or buy hold = Math.max(hold, rest - prices[i]); // Sell the stock sold = prevHold + prices[i]; // Continue resting or come from sold rest = Math.max(rest, sold); } return Math.max(sold, rest);} /** * Find the maximum profit with cooldown. * Dynamic Programming approach. * * @param {number[]} prices - Array of stock prices * @return {number} - Maximum profit */function maxProfitDP(prices) { if (prices.length <= 1) { return 0; } const n = prices.length; const buy = new Array(n).fill(0); const sell = new Array(n).fill(0); const cooldown = new Array(n).fill(0); buy[0] = -prices[0]; for (let i = 1; i < n; i++) { // Buy: continue from previous buy state or buy after cooldown buy[i] = Math.max(buy[i - 1], cooldown[i - 1] - prices[i]); // Sell: continue from previous sell state or sell the stock we bought sell[i] = Math.max(sell[i - 1], buy[i - 1] + prices[i]); // Cooldown: continue from previous cooldown state or enter cooldown after selling cooldown[i] = Math.max(cooldown[i - 1], sell[i - 1]); } return Math.max(sell[n - 1], cooldown[n - 1]);} /** * Find the maximum profit with cooldown. * Optimized Dynamic Programming approach. * * @param {number[]} prices - Array of stock prices * @return {number} - Maximum profit */function maxProfitOptimized(prices) { if (prices.length <= 1) { return 0; } let buy = -prices[0]; let sell = 0; let cooldown = 0; for (let i = 1; i < prices.length; i++) { // We need to use temporary variables to avoid using updated values in the same iteration const prevBuy = buy; const prevSell = sell; // Buy: continue from previous buy state or buy after cooldown buy = Math.max(buy, cooldown - prices[i]); // Sell: continue from previous sell state or sell the stock we bought sell = Math.max(sell, prevBuy + prices[i]); // Cooldown: continue from previous cooldown state or enter cooldown after selling cooldown = Math.max(cooldown, prevSell); } return Math.max(sell, cooldown);} // Test casesconsole.log(maxProfit([1, 2, 3, 0, 2])); // 3console.log(maxProfit([1])); // 0 console.log(maxProfitDP([1, 2, 3, 0, 2])); // 3console.log(maxProfitDP([1])); // 0 console.log(maxProfitOptimized([1, 2, 3, 0, 2])); // 3console.log(maxProfitOptimized([1])); // 0
Let's break down the implementation:
Implement the stock trader with cooldown solution in different programming languages.
Below is the implementation of the stock trader with cooldown in different programming languages. Select a language tab to view the corresponding code.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109/** * Find the maximum profit with cooldown. * State Machine approach. * * @param {number[]} prices - Array of stock prices * @return {number} - Maximum profit */function maxProfit(prices) { if (prices.length <= 1) { return 0; } let hold = -prices[0]; // Maximum profit with a stock let sold = 0; // Maximum profit after selling let rest = 0; // Maximum profit at rest for (let i = 1; i < prices.length; i++) { // We need to use temporary variables to avoid using updated values in the same iteration const prevHold = hold; // Continue holding or buy hold = Math.max(hold, rest - prices[i]); // Sell the stock sold = prevHold + prices[i]; // Continue resting or come from sold rest = Math.max(rest, sold); } return Math.max(sold, rest);} /** * Find the maximum profit with cooldown. * Dynamic Programming approach. * * @param {number[]} prices - Array of stock prices * @return {number} - Maximum profit */function maxProfitDP(prices) { if (prices.length <= 1) { return 0; } const n = prices.length; const buy = new Array(n).fill(0); const sell = new Array(n).fill(0); const cooldown = new Array(n).fill(0); buy[0] = -prices[0]; for (let i = 1; i < n; i++) { // Buy: continue from previous buy state or buy after cooldown buy[i] = Math.max(buy[i - 1], cooldown[i - 1] - prices[i]); // Sell: continue from previous sell state or sell the stock we bought sell[i] = Math.max(sell[i - 1], buy[i - 1] + prices[i]); // Cooldown: continue from previous cooldown state or enter cooldown after selling cooldown[i] = Math.max(cooldown[i - 1], sell[i - 1]); } return Math.max(sell[n - 1], cooldown[n - 1]);} /** * Find the maximum profit with cooldown. * Optimized Dynamic Programming approach. * * @param {number[]} prices - Array of stock prices * @return {number} - Maximum profit */function maxProfitOptimized(prices) { if (prices.length <= 1) { return 0; } let buy = -prices[0]; let sell = 0; let cooldown = 0; for (let i = 1; i < prices.length; i++) { // We need to use temporary variables to avoid using updated values in the same iteration const prevBuy = buy; const prevSell = sell; // Buy: continue from previous buy state or buy after cooldown buy = Math.max(buy, cooldown - prices[i]); // Sell: continue from previous sell state or sell the stock we bought sell = Math.max(sell, prevBuy + prices[i]); // Cooldown: continue from previous cooldown state or enter cooldown after selling cooldown = Math.max(cooldown, prevSell); } return Math.max(sell, cooldown);} // Test casesconsole.log(maxProfit([1, 2, 3, 0, 2])); // 3console.log(maxProfit([1])); // 0 console.log(maxProfitDP([1, 2, 3, 0, 2])); // 3console.log(maxProfitDP([1])); // 0 console.log(maxProfitOptimized([1, 2, 3, 0, 2])); // 3console.log(maxProfitOptimized([1])); // 0
First, understand that we need to find the maximum profit from buying and selling stocks with a cooldown period of one day after selling.
Define state variables to track the maximum profit in different states: holding a stock, just sold a stock, and resting (not holding and not in cooldown).
Iterate through the array of prices, updating the state variables based on the possible actions: buy, sell, or do nothing.
Ensure that after selling a stock, we cannot buy on the next day (cooldown period).
Return the maximum profit, which will be the maximum of the sold and rest states.
Modify the code to implement an alternative approach and test it with the same examples.
Implement a function that solves the stock trader with cooldown problem using a different approach than shown above.
Handle the case where the input array is empty (return 0).
Handle the case where the input array has only one element (return 0).
Handle the case where prices are strictly decreasing (return 0).