Below is the implementation of the stock trader with k transactions:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163/** * Find the maximum profit with at most k transactions. * Dynamic Programming with 2K States approach. * * @param {number} k - Maximum number of transactions * @param {number[]} prices - Array of stock prices * @return {number} - Maximum profit */function maxProfit(k, prices) { const n = prices.length; // Edge cases if (k === 0 || n === 0) { return 0; } // If k is large, use the greedy approach if (k >= Math.floor(n / 2)) { let maxProfit = 0; for (let i = 1; i < n; i++) { if (prices[i] > prices[i - 1]) { maxProfit += prices[i] - prices[i - 1]; } } return maxProfit; } // Initialize arrays const buy = new Array(k + 1).fill(-Infinity); const sell = new Array(k + 1).fill(0); // Set initial values for (let j = 1; j <= k; j++) { buy[j] = -prices[0]; } // Dynamic programming for (let i = 1; i < n; i++) { for (let j = 1; j <= k; j++) { // Update buy: either keep the previous state or buy the j-th stock buy[j] = Math.max(buy[j], sell[j - 1] - prices[i]); // Update sell: either keep the previous state or sell the j-th stock sell[j] = Math.max(sell[j], buy[j] + prices[i]); } } return sell[k];} /** * Find the maximum profit with at most k transactions. * Dynamic Programming with 2D Arrays approach. * * @param {number} k - Maximum number of transactions * @param {number[]} prices - Array of stock prices * @return {number} - Maximum profit */function maxProfitWith2DArrays(k, prices) { const n = prices.length; // Edge cases if (k === 0 || n === 0) { return 0; } // If k is large, use the greedy approach if (k >= Math.floor(n / 2)) { let maxProfit = 0; for (let i = 1; i < n; i++) { if (prices[i] > prices[i - 1]) { maxProfit += prices[i] - prices[i - 1]; } } return maxProfit; } // Initialize 2D arrays const buy = Array(n).fill().map(() => Array(k + 1).fill(-Infinity)); const sell = Array(n).fill().map(() => Array(k + 1).fill(0)); // Set initial values for (let j = 1; j <= k; j++) { buy[0][j] = -prices[0]; } // Dynamic programming for (let i = 1; i < n; i++) { for (let j = 1; j <= k; j++) { // Update buy: either keep the previous state or buy the j-th stock buy[i][j] = Math.max(buy[i - 1][j], sell[i - 1][j - 1] - prices[i]); // Update sell: either keep the previous state or sell the j-th stock sell[i][j] = Math.max(sell[i - 1][j], buy[i][j] + prices[i]); } } return sell[n - 1][k];} /** * Find the maximum profit with at most k transactions. * Optimized Dynamic Programming approach. * * @param {number} k - Maximum number of transactions * @param {number[]} prices - Array of stock prices * @return {number} - Maximum profit */function maxProfitOptimized(k, prices) { const n = prices.length; // Edge cases if (k === 0 || n === 0) { return 0; } // If k is large, use the greedy approach if (k >= Math.floor(n / 2)) { let maxProfit = 0; for (let i = 1; i < n; i++) { if (prices[i] > prices[i - 1]) { maxProfit += prices[i] - prices[i - 1]; } } return maxProfit; } // Initialize arrays const buy = new Array(k + 1).fill(-Infinity); const sell = new Array(k + 1).fill(0); // Set initial values for (let j = 1; j <= k; j++) { buy[j] = -prices[0]; } // Dynamic programming for (let i = 1; i < n; i++) { for (let j = 1; j <= k; j++) { // We need to use temporary variables to avoid using updated values in the same iteration const prevBuy = buy[j]; const prevSell = sell[j - 1]; // Update buy: either keep the previous state or buy the j-th stock buy[j] = Math.max(prevBuy, prevSell - prices[i]); // Update sell: either keep the previous state or sell the j-th stock sell[j] = Math.max(sell[j], prevBuy + prices[i]); } } return sell[k];} // Test casesconsole.log(maxProfit(2, [2, 4, 1])); // 2console.log(maxProfit(2, [3, 2, 6, 5, 0, 3])); // 7 console.log(maxProfitWith2DArrays(2, [2, 4, 1])); // 2console.log(maxProfitWith2DArrays(2, [3, 2, 6, 5, 0, 3])); // 7 console.log(maxProfitOptimized(2, [2, 4, 1])); // 2console.log(maxProfitOptimized(2, [3, 2, 6, 5, 0, 3])); // 7
Let's break down the implementation:
Implement the stock trader with k transactions solution in different programming languages.
Below is the implementation of the stock trader with k transactions in different programming languages. Select a language tab to view the corresponding code.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163/** * Find the maximum profit with at most k transactions. * Dynamic Programming with 2K States approach. * * @param {number} k - Maximum number of transactions * @param {number[]} prices - Array of stock prices * @return {number} - Maximum profit */function maxProfit(k, prices) { const n = prices.length; // Edge cases if (k === 0 || n === 0) { return 0; } // If k is large, use the greedy approach if (k >= Math.floor(n / 2)) { let maxProfit = 0; for (let i = 1; i < n; i++) { if (prices[i] > prices[i - 1]) { maxProfit += prices[i] - prices[i - 1]; } } return maxProfit; } // Initialize arrays const buy = new Array(k + 1).fill(-Infinity); const sell = new Array(k + 1).fill(0); // Set initial values for (let j = 1; j <= k; j++) { buy[j] = -prices[0]; } // Dynamic programming for (let i = 1; i < n; i++) { for (let j = 1; j <= k; j++) { // Update buy: either keep the previous state or buy the j-th stock buy[j] = Math.max(buy[j], sell[j - 1] - prices[i]); // Update sell: either keep the previous state or sell the j-th stock sell[j] = Math.max(sell[j], buy[j] + prices[i]); } } return sell[k];} /** * Find the maximum profit with at most k transactions. * Dynamic Programming with 2D Arrays approach. * * @param {number} k - Maximum number of transactions * @param {number[]} prices - Array of stock prices * @return {number} - Maximum profit */function maxProfitWith2DArrays(k, prices) { const n = prices.length; // Edge cases if (k === 0 || n === 0) { return 0; } // If k is large, use the greedy approach if (k >= Math.floor(n / 2)) { let maxProfit = 0; for (let i = 1; i < n; i++) { if (prices[i] > prices[i - 1]) { maxProfit += prices[i] - prices[i - 1]; } } return maxProfit; } // Initialize 2D arrays const buy = Array(n).fill().map(() => Array(k + 1).fill(-Infinity)); const sell = Array(n).fill().map(() => Array(k + 1).fill(0)); // Set initial values for (let j = 1; j <= k; j++) { buy[0][j] = -prices[0]; } // Dynamic programming for (let i = 1; i < n; i++) { for (let j = 1; j <= k; j++) { // Update buy: either keep the previous state or buy the j-th stock buy[i][j] = Math.max(buy[i - 1][j], sell[i - 1][j - 1] - prices[i]); // Update sell: either keep the previous state or sell the j-th stock sell[i][j] = Math.max(sell[i - 1][j], buy[i][j] + prices[i]); } } return sell[n - 1][k];} /** * Find the maximum profit with at most k transactions. * Optimized Dynamic Programming approach. * * @param {number} k - Maximum number of transactions * @param {number[]} prices - Array of stock prices * @return {number} - Maximum profit */function maxProfitOptimized(k, prices) { const n = prices.length; // Edge cases if (k === 0 || n === 0) { return 0; } // If k is large, use the greedy approach if (k >= Math.floor(n / 2)) { let maxProfit = 0; for (let i = 1; i < n; i++) { if (prices[i] > prices[i - 1]) { maxProfit += prices[i] - prices[i - 1]; } } return maxProfit; } // Initialize arrays const buy = new Array(k + 1).fill(-Infinity); const sell = new Array(k + 1).fill(0); // Set initial values for (let j = 1; j <= k; j++) { buy[j] = -prices[0]; } // Dynamic programming for (let i = 1; i < n; i++) { for (let j = 1; j <= k; j++) { // We need to use temporary variables to avoid using updated values in the same iteration const prevBuy = buy[j]; const prevSell = sell[j - 1]; // Update buy: either keep the previous state or buy the j-th stock buy[j] = Math.max(prevBuy, prevSell - prices[i]); // Update sell: either keep the previous state or sell the j-th stock sell[j] = Math.max(sell[j], prevBuy + prices[i]); } } return sell[k];} // Test casesconsole.log(maxProfit(2, [2, 4, 1])); // 2console.log(maxProfit(2, [3, 2, 6, 5, 0, 3])); // 7 console.log(maxProfitWith2DArrays(2, [2, 4, 1])); // 2console.log(maxProfitWith2DArrays(2, [3, 2, 6, 5, 0, 3])); // 7 console.log(maxProfitOptimized(2, [2, 4, 1])); // 2console.log(maxProfitOptimized(2, [3, 2, 6, 5, 0, 3])); // 7
First, understand that we need to find the maximum profit from at most k transactions, where we must sell before buying again.
Handle edge cases such as k = 0, empty prices array, and k >= n/2 (where we can make as many transactions as we want).
Define state variables to track the maximum profit after different states of transactions: buying and selling stocks for each transaction.
Iterate through the array of prices, updating the state variables based on the possible actions: buy, sell, or do nothing.
Return the maximum profit, which will be the maximum profit after selling the k-th stock.
Modify the code to implement an alternative approach and test it with the same examples.
Implement a function that solves the stock trader with k transactions problem using a different approach than shown above.
Handle the case where the input array is empty (return 0).
Handle the case where k is 0 (return 0).
Handle the case where k is large (k >= n/2), which reduces to the Best Time to Buy and Sell Stock II problem.
Handle the case where prices are strictly decreasing (return 0).
Below is the implementation of the stock trader with k transactions:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163/** * Find the maximum profit with at most k transactions. * Dynamic Programming with 2K States approach. * * @param {number} k - Maximum number of transactions * @param {number[]} prices - Array of stock prices * @return {number} - Maximum profit */function maxProfit(k, prices) { const n = prices.length; // Edge cases if (k === 0 || n === 0) { return 0; } // If k is large, use the greedy approach if (k >= Math.floor(n / 2)) { let maxProfit = 0; for (let i = 1; i < n; i++) { if (prices[i] > prices[i - 1]) { maxProfit += prices[i] - prices[i - 1]; } } return maxProfit; } // Initialize arrays const buy = new Array(k + 1).fill(-Infinity); const sell = new Array(k + 1).fill(0); // Set initial values for (let j = 1; j <= k; j++) { buy[j] = -prices[0]; } // Dynamic programming for (let i = 1; i < n; i++) { for (let j = 1; j <= k; j++) { // Update buy: either keep the previous state or buy the j-th stock buy[j] = Math.max(buy[j], sell[j - 1] - prices[i]); // Update sell: either keep the previous state or sell the j-th stock sell[j] = Math.max(sell[j], buy[j] + prices[i]); } } return sell[k];} /** * Find the maximum profit with at most k transactions. * Dynamic Programming with 2D Arrays approach. * * @param {number} k - Maximum number of transactions * @param {number[]} prices - Array of stock prices * @return {number} - Maximum profit */function maxProfitWith2DArrays(k, prices) { const n = prices.length; // Edge cases if (k === 0 || n === 0) { return 0; } // If k is large, use the greedy approach if (k >= Math.floor(n / 2)) { let maxProfit = 0; for (let i = 1; i < n; i++) { if (prices[i] > prices[i - 1]) { maxProfit += prices[i] - prices[i - 1]; } } return maxProfit; } // Initialize 2D arrays const buy = Array(n).fill().map(() => Array(k + 1).fill(-Infinity)); const sell = Array(n).fill().map(() => Array(k + 1).fill(0)); // Set initial values for (let j = 1; j <= k; j++) { buy[0][j] = -prices[0]; } // Dynamic programming for (let i = 1; i < n; i++) { for (let j = 1; j <= k; j++) { // Update buy: either keep the previous state or buy the j-th stock buy[i][j] = Math.max(buy[i - 1][j], sell[i - 1][j - 1] - prices[i]); // Update sell: either keep the previous state or sell the j-th stock sell[i][j] = Math.max(sell[i - 1][j], buy[i][j] + prices[i]); } } return sell[n - 1][k];} /** * Find the maximum profit with at most k transactions. * Optimized Dynamic Programming approach. * * @param {number} k - Maximum number of transactions * @param {number[]} prices - Array of stock prices * @return {number} - Maximum profit */function maxProfitOptimized(k, prices) { const n = prices.length; // Edge cases if (k === 0 || n === 0) { return 0; } // If k is large, use the greedy approach if (k >= Math.floor(n / 2)) { let maxProfit = 0; for (let i = 1; i < n; i++) { if (prices[i] > prices[i - 1]) { maxProfit += prices[i] - prices[i - 1]; } } return maxProfit; } // Initialize arrays const buy = new Array(k + 1).fill(-Infinity); const sell = new Array(k + 1).fill(0); // Set initial values for (let j = 1; j <= k; j++) { buy[j] = -prices[0]; } // Dynamic programming for (let i = 1; i < n; i++) { for (let j = 1; j <= k; j++) { // We need to use temporary variables to avoid using updated values in the same iteration const prevBuy = buy[j]; const prevSell = sell[j - 1]; // Update buy: either keep the previous state or buy the j-th stock buy[j] = Math.max(prevBuy, prevSell - prices[i]); // Update sell: either keep the previous state or sell the j-th stock sell[j] = Math.max(sell[j], prevBuy + prices[i]); } } return sell[k];} // Test casesconsole.log(maxProfit(2, [2, 4, 1])); // 2console.log(maxProfit(2, [3, 2, 6, 5, 0, 3])); // 7 console.log(maxProfitWith2DArrays(2, [2, 4, 1])); // 2console.log(maxProfitWith2DArrays(2, [3, 2, 6, 5, 0, 3])); // 7 console.log(maxProfitOptimized(2, [2, 4, 1])); // 2console.log(maxProfitOptimized(2, [3, 2, 6, 5, 0, 3])); // 7
Let's break down the implementation:
Implement the stock trader with k transactions solution in different programming languages.
Below is the implementation of the stock trader with k transactions in different programming languages. Select a language tab to view the corresponding code.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163/** * Find the maximum profit with at most k transactions. * Dynamic Programming with 2K States approach. * * @param {number} k - Maximum number of transactions * @param {number[]} prices - Array of stock prices * @return {number} - Maximum profit */function maxProfit(k, prices) { const n = prices.length; // Edge cases if (k === 0 || n === 0) { return 0; } // If k is large, use the greedy approach if (k >= Math.floor(n / 2)) { let maxProfit = 0; for (let i = 1; i < n; i++) { if (prices[i] > prices[i - 1]) { maxProfit += prices[i] - prices[i - 1]; } } return maxProfit; } // Initialize arrays const buy = new Array(k + 1).fill(-Infinity); const sell = new Array(k + 1).fill(0); // Set initial values for (let j = 1; j <= k; j++) { buy[j] = -prices[0]; } // Dynamic programming for (let i = 1; i < n; i++) { for (let j = 1; j <= k; j++) { // Update buy: either keep the previous state or buy the j-th stock buy[j] = Math.max(buy[j], sell[j - 1] - prices[i]); // Update sell: either keep the previous state or sell the j-th stock sell[j] = Math.max(sell[j], buy[j] + prices[i]); } } return sell[k];} /** * Find the maximum profit with at most k transactions. * Dynamic Programming with 2D Arrays approach. * * @param {number} k - Maximum number of transactions * @param {number[]} prices - Array of stock prices * @return {number} - Maximum profit */function maxProfitWith2DArrays(k, prices) { const n = prices.length; // Edge cases if (k === 0 || n === 0) { return 0; } // If k is large, use the greedy approach if (k >= Math.floor(n / 2)) { let maxProfit = 0; for (let i = 1; i < n; i++) { if (prices[i] > prices[i - 1]) { maxProfit += prices[i] - prices[i - 1]; } } return maxProfit; } // Initialize 2D arrays const buy = Array(n).fill().map(() => Array(k + 1).fill(-Infinity)); const sell = Array(n).fill().map(() => Array(k + 1).fill(0)); // Set initial values for (let j = 1; j <= k; j++) { buy[0][j] = -prices[0]; } // Dynamic programming for (let i = 1; i < n; i++) { for (let j = 1; j <= k; j++) { // Update buy: either keep the previous state or buy the j-th stock buy[i][j] = Math.max(buy[i - 1][j], sell[i - 1][j - 1] - prices[i]); // Update sell: either keep the previous state or sell the j-th stock sell[i][j] = Math.max(sell[i - 1][j], buy[i][j] + prices[i]); } } return sell[n - 1][k];} /** * Find the maximum profit with at most k transactions. * Optimized Dynamic Programming approach. * * @param {number} k - Maximum number of transactions * @param {number[]} prices - Array of stock prices * @return {number} - Maximum profit */function maxProfitOptimized(k, prices) { const n = prices.length; // Edge cases if (k === 0 || n === 0) { return 0; } // If k is large, use the greedy approach if (k >= Math.floor(n / 2)) { let maxProfit = 0; for (let i = 1; i < n; i++) { if (prices[i] > prices[i - 1]) { maxProfit += prices[i] - prices[i - 1]; } } return maxProfit; } // Initialize arrays const buy = new Array(k + 1).fill(-Infinity); const sell = new Array(k + 1).fill(0); // Set initial values for (let j = 1; j <= k; j++) { buy[j] = -prices[0]; } // Dynamic programming for (let i = 1; i < n; i++) { for (let j = 1; j <= k; j++) { // We need to use temporary variables to avoid using updated values in the same iteration const prevBuy = buy[j]; const prevSell = sell[j - 1]; // Update buy: either keep the previous state or buy the j-th stock buy[j] = Math.max(prevBuy, prevSell - prices[i]); // Update sell: either keep the previous state or sell the j-th stock sell[j] = Math.max(sell[j], prevBuy + prices[i]); } } return sell[k];} // Test casesconsole.log(maxProfit(2, [2, 4, 1])); // 2console.log(maxProfit(2, [3, 2, 6, 5, 0, 3])); // 7 console.log(maxProfitWith2DArrays(2, [2, 4, 1])); // 2console.log(maxProfitWith2DArrays(2, [3, 2, 6, 5, 0, 3])); // 7 console.log(maxProfitOptimized(2, [2, 4, 1])); // 2console.log(maxProfitOptimized(2, [3, 2, 6, 5, 0, 3])); // 7
First, understand that we need to find the maximum profit from at most k transactions, where we must sell before buying again.
Handle edge cases such as k = 0, empty prices array, and k >= n/2 (where we can make as many transactions as we want).
Define state variables to track the maximum profit after different states of transactions: buying and selling stocks for each transaction.
Iterate through the array of prices, updating the state variables based on the possible actions: buy, sell, or do nothing.
Return the maximum profit, which will be the maximum profit after selling the k-th stock.
Modify the code to implement an alternative approach and test it with the same examples.
Implement a function that solves the stock trader with k transactions problem using a different approach than shown above.
Handle the case where the input array is empty (return 0).
Handle the case where k is 0 (return 0).
Handle the case where k is large (k >= n/2), which reduces to the Best Time to Buy and Sell Stock II problem.
Handle the case where prices are strictly decreasing (return 0).