Below is the implementation of the stock trader with fees:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102/** * Find the maximum profit with transaction fees. * Dynamic Programming approach. * * @param {number[]} prices - Array of stock prices * @param {number} fee - Transaction fee * @return {number} - Maximum profit */function maxProfit(prices, fee) { if (prices.length <= 1) { return 0; } let cash = 0; // Maximum profit with no stock let hold = -prices[0]; // Maximum profit with a stock for (let i = 1; i < prices.length; i++) { // We need to use temporary variables to avoid using the updated cash value when calculating hold const prevCash = cash; // Sell or keep cash cash = Math.max(cash, hold + prices[i] - fee); // Buy or keep holding hold = Math.max(hold, prevCash - prices[i]); } return cash;} /** * Find the maximum profit with transaction fees. * State Machine approach. * * @param {number[]} prices - Array of stock prices * @param {number} fee - Transaction fee * @return {number} - Maximum profit */function maxProfitStateMachine(prices, fee) { if (prices.length <= 1) { return 0; } let s0 = 0; // Maximum profit with no stock let s1 = -prices[0]; // Maximum profit with a stock for (let i = 1; i < prices.length; i++) { // We need to use temporary variables to avoid using the updated s0 value when calculating s1 const prevS0 = s0; // State 0: No stock, either stay or sell s0 = Math.max(s0, s1 + prices[i] - fee); // State 1: Have stock, either stay or buy s1 = Math.max(s1, prevS0 - prices[i]); } return s0;} /** * Find the maximum profit with transaction fees. * Greedy approach. * * @param {number[]} prices - Array of stock prices * @param {number} fee - Transaction fee * @return {number} - Maximum profit */function maxProfitGreedy(prices, fee) { if (prices.length <= 1) { return 0; } let profit = 0; let buyPrice = prices[0]; for (let i = 1; i < prices.length; i++) { if (prices[i] < buyPrice) { // Found a lower buying price buyPrice = prices[i]; } else if (prices[i] > buyPrice + fee) { // We can make a profit after paying the fee profit += prices[i] - buyPrice - fee; // Adjust buying price for the next transaction // This is a key insight: we can effectively "hold" the stock by setting the buy price to the current price minus the fee buyPrice = prices[i] - fee; } } return profit;} // Test casesconsole.log(maxProfit([1, 3, 2, 8, 4, 9], 2)); // 8console.log(maxProfit([1, 3, 7, 5, 10, 3], 3)); // 6 console.log(maxProfitStateMachine([1, 3, 2, 8, 4, 9], 2)); // 8console.log(maxProfitStateMachine([1, 3, 7, 5, 10, 3], 3)); // 6 console.log(maxProfitGreedy([1, 3, 2, 8, 4, 9], 2)); // 8console.log(maxProfitGreedy([1, 3, 7, 5, 10, 3], 3)); // 6
Let's break down the implementation:
Implement the stock trader with fees solution in different programming languages.
Below is the implementation of the stock trader with fees in different programming languages. Select a language tab to view the corresponding code.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102/** * Find the maximum profit with transaction fees. * Dynamic Programming approach. * * @param {number[]} prices - Array of stock prices * @param {number} fee - Transaction fee * @return {number} - Maximum profit */function maxProfit(prices, fee) { if (prices.length <= 1) { return 0; } let cash = 0; // Maximum profit with no stock let hold = -prices[0]; // Maximum profit with a stock for (let i = 1; i < prices.length; i++) { // We need to use temporary variables to avoid using the updated cash value when calculating hold const prevCash = cash; // Sell or keep cash cash = Math.max(cash, hold + prices[i] - fee); // Buy or keep holding hold = Math.max(hold, prevCash - prices[i]); } return cash;} /** * Find the maximum profit with transaction fees. * State Machine approach. * * @param {number[]} prices - Array of stock prices * @param {number} fee - Transaction fee * @return {number} - Maximum profit */function maxProfitStateMachine(prices, fee) { if (prices.length <= 1) { return 0; } let s0 = 0; // Maximum profit with no stock let s1 = -prices[0]; // Maximum profit with a stock for (let i = 1; i < prices.length; i++) { // We need to use temporary variables to avoid using the updated s0 value when calculating s1 const prevS0 = s0; // State 0: No stock, either stay or sell s0 = Math.max(s0, s1 + prices[i] - fee); // State 1: Have stock, either stay or buy s1 = Math.max(s1, prevS0 - prices[i]); } return s0;} /** * Find the maximum profit with transaction fees. * Greedy approach. * * @param {number[]} prices - Array of stock prices * @param {number} fee - Transaction fee * @return {number} - Maximum profit */function maxProfitGreedy(prices, fee) { if (prices.length <= 1) { return 0; } let profit = 0; let buyPrice = prices[0]; for (let i = 1; i < prices.length; i++) { if (prices[i] < buyPrice) { // Found a lower buying price buyPrice = prices[i]; } else if (prices[i] > buyPrice + fee) { // We can make a profit after paying the fee profit += prices[i] - buyPrice - fee; // Adjust buying price for the next transaction // This is a key insight: we can effectively "hold" the stock by setting the buy price to the current price minus the fee buyPrice = prices[i] - fee; } } return profit;} // Test casesconsole.log(maxProfit([1, 3, 2, 8, 4, 9], 2)); // 8console.log(maxProfit([1, 3, 7, 5, 10, 3], 3)); // 6 console.log(maxProfitStateMachine([1, 3, 2, 8, 4, 9], 2)); // 8console.log(maxProfitStateMachine([1, 3, 7, 5, 10, 3], 3)); // 6 console.log(maxProfitGreedy([1, 3, 2, 8, 4, 9], 2)); // 8console.log(maxProfitGreedy([1, 3, 7, 5, 10, 3], 3)); // 6
First, understand that we need to find the maximum profit from buying and selling stocks with a transaction fee for each sell operation.
Define state variables to track the maximum profit in two states: holding a stock and not holding a stock.
Iterate through the array of prices, updating the state variables based on the possible actions: buy, sell, or do nothing.
When selling a stock, subtract the transaction fee from the profit.
Return the maximum profit, which will be in the state of not holding a stock (cash or s0).
Modify the code to implement an alternative approach and test it with the same examples.
Implement a function that solves the stock trader with fees 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 the transaction fee is higher than any potential profit (return 0).
Below is the implementation of the stock trader with fees:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102/** * Find the maximum profit with transaction fees. * Dynamic Programming approach. * * @param {number[]} prices - Array of stock prices * @param {number} fee - Transaction fee * @return {number} - Maximum profit */function maxProfit(prices, fee) { if (prices.length <= 1) { return 0; } let cash = 0; // Maximum profit with no stock let hold = -prices[0]; // Maximum profit with a stock for (let i = 1; i < prices.length; i++) { // We need to use temporary variables to avoid using the updated cash value when calculating hold const prevCash = cash; // Sell or keep cash cash = Math.max(cash, hold + prices[i] - fee); // Buy or keep holding hold = Math.max(hold, prevCash - prices[i]); } return cash;} /** * Find the maximum profit with transaction fees. * State Machine approach. * * @param {number[]} prices - Array of stock prices * @param {number} fee - Transaction fee * @return {number} - Maximum profit */function maxProfitStateMachine(prices, fee) { if (prices.length <= 1) { return 0; } let s0 = 0; // Maximum profit with no stock let s1 = -prices[0]; // Maximum profit with a stock for (let i = 1; i < prices.length; i++) { // We need to use temporary variables to avoid using the updated s0 value when calculating s1 const prevS0 = s0; // State 0: No stock, either stay or sell s0 = Math.max(s0, s1 + prices[i] - fee); // State 1: Have stock, either stay or buy s1 = Math.max(s1, prevS0 - prices[i]); } return s0;} /** * Find the maximum profit with transaction fees. * Greedy approach. * * @param {number[]} prices - Array of stock prices * @param {number} fee - Transaction fee * @return {number} - Maximum profit */function maxProfitGreedy(prices, fee) { if (prices.length <= 1) { return 0; } let profit = 0; let buyPrice = prices[0]; for (let i = 1; i < prices.length; i++) { if (prices[i] < buyPrice) { // Found a lower buying price buyPrice = prices[i]; } else if (prices[i] > buyPrice + fee) { // We can make a profit after paying the fee profit += prices[i] - buyPrice - fee; // Adjust buying price for the next transaction // This is a key insight: we can effectively "hold" the stock by setting the buy price to the current price minus the fee buyPrice = prices[i] - fee; } } return profit;} // Test casesconsole.log(maxProfit([1, 3, 2, 8, 4, 9], 2)); // 8console.log(maxProfit([1, 3, 7, 5, 10, 3], 3)); // 6 console.log(maxProfitStateMachine([1, 3, 2, 8, 4, 9], 2)); // 8console.log(maxProfitStateMachine([1, 3, 7, 5, 10, 3], 3)); // 6 console.log(maxProfitGreedy([1, 3, 2, 8, 4, 9], 2)); // 8console.log(maxProfitGreedy([1, 3, 7, 5, 10, 3], 3)); // 6
Let's break down the implementation:
Implement the stock trader with fees solution in different programming languages.
Below is the implementation of the stock trader with fees in different programming languages. Select a language tab to view the corresponding code.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102/** * Find the maximum profit with transaction fees. * Dynamic Programming approach. * * @param {number[]} prices - Array of stock prices * @param {number} fee - Transaction fee * @return {number} - Maximum profit */function maxProfit(prices, fee) { if (prices.length <= 1) { return 0; } let cash = 0; // Maximum profit with no stock let hold = -prices[0]; // Maximum profit with a stock for (let i = 1; i < prices.length; i++) { // We need to use temporary variables to avoid using the updated cash value when calculating hold const prevCash = cash; // Sell or keep cash cash = Math.max(cash, hold + prices[i] - fee); // Buy or keep holding hold = Math.max(hold, prevCash - prices[i]); } return cash;} /** * Find the maximum profit with transaction fees. * State Machine approach. * * @param {number[]} prices - Array of stock prices * @param {number} fee - Transaction fee * @return {number} - Maximum profit */function maxProfitStateMachine(prices, fee) { if (prices.length <= 1) { return 0; } let s0 = 0; // Maximum profit with no stock let s1 = -prices[0]; // Maximum profit with a stock for (let i = 1; i < prices.length; i++) { // We need to use temporary variables to avoid using the updated s0 value when calculating s1 const prevS0 = s0; // State 0: No stock, either stay or sell s0 = Math.max(s0, s1 + prices[i] - fee); // State 1: Have stock, either stay or buy s1 = Math.max(s1, prevS0 - prices[i]); } return s0;} /** * Find the maximum profit with transaction fees. * Greedy approach. * * @param {number[]} prices - Array of stock prices * @param {number} fee - Transaction fee * @return {number} - Maximum profit */function maxProfitGreedy(prices, fee) { if (prices.length <= 1) { return 0; } let profit = 0; let buyPrice = prices[0]; for (let i = 1; i < prices.length; i++) { if (prices[i] < buyPrice) { // Found a lower buying price buyPrice = prices[i]; } else if (prices[i] > buyPrice + fee) { // We can make a profit after paying the fee profit += prices[i] - buyPrice - fee; // Adjust buying price for the next transaction // This is a key insight: we can effectively "hold" the stock by setting the buy price to the current price minus the fee buyPrice = prices[i] - fee; } } return profit;} // Test casesconsole.log(maxProfit([1, 3, 2, 8, 4, 9], 2)); // 8console.log(maxProfit([1, 3, 7, 5, 10, 3], 3)); // 6 console.log(maxProfitStateMachine([1, 3, 2, 8, 4, 9], 2)); // 8console.log(maxProfitStateMachine([1, 3, 7, 5, 10, 3], 3)); // 6 console.log(maxProfitGreedy([1, 3, 2, 8, 4, 9], 2)); // 8console.log(maxProfitGreedy([1, 3, 7, 5, 10, 3], 3)); // 6
First, understand that we need to find the maximum profit from buying and selling stocks with a transaction fee for each sell operation.
Define state variables to track the maximum profit in two states: holding a stock and not holding a stock.
Iterate through the array of prices, updating the state variables based on the possible actions: buy, sell, or do nothing.
When selling a stock, subtract the transaction fee from the profit.
Return the maximum profit, which will be in the state of not holding a stock (cash or s0).
Modify the code to implement an alternative approach and test it with the same examples.
Implement a function that solves the stock trader with fees 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 the transaction fee is higher than any potential profit (return 0).