714. Best Time to Buy and Sell Stock with Transaction Fee
Description
You are given an array prices where prices[i] is the price of a given stock on the ith day, and an integer fee representing a transaction fee.
Find the maximum profit you can achieve. You may complete as many transactions as you like, but you need to pay the transaction fee for each transaction.
Note:
- You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
- The transaction fee is only charged once for each stock purchase and sale.
Example 1:
Input: prices = [1,3,2,8,4,9], fee = 2 Output: 8 Explanation: The maximum profit can be achieved by: - Buying at prices[0] = 1 - Selling at prices[3] = 8 - Buying at prices[4] = 4 - Selling at prices[5] = 9 The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8.
Example 2:
Input: prices = [1,3,7,5,10,3], fee = 3 Output: 6
Constraints:
1 <= prices.length <= 5 * 1041 <= prices[i] < 5 * 1040 <= fee < 5 * 104
Solutions
Solution 1: Memoization
Thinking
We may trade many times and pay \(fee\) on each completed sale. \(n \le 5\times 10^4\), so an unmemoized search over days repeats states.
A day has two modes: flat or holding. From flat we buy or skip; from holding we sell (minus \(fee\)) or hold. The optimum from day \(i\) in mode \(j\) depends only on those two successors.
Memoize \(dfs(i,j)\); past the last day the profit is \(0\). The answer is \(dfs(0,0)\). There are \(O(n)\) states.
We design a function \(dfs(i, j)\), which represents the maximum profit that can be obtained starting from day \(i\) with state \(j\). Here, \(j\) can take the values \(0\) and \(1\), representing not holding and holding a stock, respectively. The answer is \(dfs(0, 0)\).
The execution logic of the function \(dfs(i, j)\) is as follows:
If \(i \geq n\), there are no more stocks to trade, so we return \(0\).
Otherwise, we can choose not to trade, in which case \(dfs(i, j) = dfs(i + 1, j)\). We can also choose to trade stocks. If \(j \gt 0\), it means that we currently hold a stock and can sell it. In this case, \(dfs(i, j) = prices[i] + dfs(i + 1, 0) - fee\). If \(j = 0\), it means that we currently do not hold a stock and can buy one. In this case, \(dfs(i, j) = -prices[i] + dfs(i + 1, 1)\). We take the maximum value as the return value of the function \(dfs(i, j)\).
The answer is \(dfs(0, 0)\).
To avoid redundant calculations, we use memoization to record the return value of \(dfs(i, j)\) in an array \(f\). If \(f[i][j]\) is not equal to \(-1\), it means that we have already calculated it, so we can directly return \(f[i][j]\).
The time complexity is \(O(n)\), and the space complexity is \(O(n)\). Here, \(n\) is the length of the array \(prices\).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
Solution 2: Dynamic Programming
Thinking
Solution 1 is already linear, but the recursion still uses an \(O(n)\) stack and table. The same transitions can be written forward by day.
Let \(f[i][0/1]\) be the best profit after day \(i\) flat or holding. Each row comes from the previous one; the answer is \(f[n-1][0]\).
We define \(f[i][j]\) as the maximum profit that can be obtained up to day \(i\) with state \(j\). Here, \(j\) can take the values \(0\) and \(1\), representing not holding and holding a stock, respectively. We initialize \(f[0][0] = 0\) and \(f[0][1] = -prices[0]\).
When \(i \geq 1\), if we do not hold a stock at the current day, then \(f[i][0]\) can be obtained by transitioning from \(f[i - 1][0]\) and \(f[i - 1][1] + prices[i] - fee\), i.e., \(f[i][0] = \max(f[i - 1][0], f[i - 1][1] + prices[i] - fee)\). If we hold a stock at the current day, then \(f[i][1]\) can be obtained by transitioning from \(f[i - 1][1]\) and \(f[i - 1][0] - prices[i]\), i.e., \(f[i][1] = \max(f[i - 1][1], f[i - 1][0] - prices[i])\). The final answer is \(f[n - 1][0]\).
The time complexity is \(O(n)\), and the space complexity is \(O(n)\). Here, \(n\) is the length of the array \(prices\).
1 2 3 4 5 6 7 8 9 | |
1 2 3 4 5 6 7 8 9 10 11 12 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
1 2 3 4 5 6 7 8 9 10 | |
1 2 3 4 5 6 7 8 9 10 | |
Solution 3: Dynamic Programming (Space Optimization)
Thinking
Day \(i\) in Solution 2 reads only day \(i-1\), so the full table is unnecessary.
Roll two scalars \(f_0,f_1\). Parallel assignment keeps the previous pair while both updates run, so the new holding state still sees the old flat profit. Space is \(O(1)\).
The transition only needs the previous day, so two variables are enough and the space complexity is \(O(1)\).
1 2 3 4 5 6 | |
1 2 3 4 5 6 7 8 9 10 11 | |
1 2 3 4 5 6 7 8 9 10 11 12 | |
1 2 3 4 5 6 7 | |
1 2 3 4 5 6 7 8 | |