Best Time to Buy and Sell Stock with Cooldown II Problem
Description
LeetCode Problem 4069.
You are given an integer array prices of length n, where prices[i] is the price of a stock on day i.
You are also given an integer cooldown and an integer array costs of length n. The value costs[k] is the total holding fee for a transaction in which the stock is held for exactly k days.
You may perform any number of transactions, including zero, subject to the following rules:
- You may hold at most one share at a time. You must sell your current share before buying another.
- If you sell on day
j, the earliest day you may buy again isj + cooldown + 1. - If you buy on day
iand sell on dayj, the holding duration isj - idays. You pay the feecosts[j - i]once for that transaction.
The profit from buying on day i and selling on day j is prices[j] - prices[i] - costs[j - i].
Return the maximum total profit you can achieve. If no profitable transactions are possible, return 0.
Example 1:
Input: prices = [1,5,3], cooldown = 1, costs = [2,0,1]
Output: 4
Explanation:
The optimal strategy is:
- Buy on day 0 at prices[0] = 1.
- Sell on day 1 at prices[1] = 5. The holding duration is 1 day, so the holding cost is costs[1] = 0.
- Therefore, the profit from this transaction is 5 - 1 - 0 = 4.
- After selling, cooldown = 1 prevents buying any stock on day 2, so no further transactions are possible.
- Maximum profit achievable is 4.
Example 2:
Input: prices = [1,2,5], cooldown = 0, costs = [0,2,1]
Output: 3
Explanation:
The optimal strategy is:
- Buy on day 0 at prices[0] = 1.
- Sell on day 2 at prices[2] = 5. The holding duration is 2 days, so the holding cost is costs[2] = 1.
- Therefore, the profit from this transaction is 5 - 1 - 1 = 3.
- No further transactions improve the profit. Maximum profit achievable is 3.
Example 3:
Input: prices = [4,1,7], cooldown = 2, costs = [3,0,1]
Output: 6
Explanation:
The optimal strategy is:
- Buy on day 1 at prices[1] = 1.
- Sell on day 2 at prices[2] = 7. The holding duration is 1 day, so the holding cost is costs[1] = 0.
- Profit from this transaction = 7 - 1 - 0 = 6.
- No further transactions are possible. Maximum profit achievable = 6.
Example 4:
Input: prices = [3,1,4], cooldown = 0, costs = [2,1,0]
Output: 2
Explanation:
The optimal strategy is:
- Buy on day 1 at prices[1] = 1.
- Sell on day 2 at prices[2] = 4. The holding duration is 1 day, so the holding cost is costs[1] = 1.
- Therefore, the profit from this transaction is 4 - 1 - 1 = 2.
- No further transactions improve the profit. Maximum profit achievable is 2.
Constraints:
1 <= n == prices.length <= 15001 <= prices[i] <= 10^50 <= cooldown <= n - 1costs.length == n0 <= costs[i] <= 10^5
Solution
Approach: Dynamic Programming over Sell Days
Define two arrays:
sold[j]: the best total profit when the last transaction is sold on dayj;best[d]: the best total profit of transactions that are finished early enough to allow buying on dayd, meaning the last sale was on dayd - cooldown - 1or earlier.
best never decreases with d, so best[d] = max(best[d-1], sold[d - cooldown - 1]).
For the last transaction bought on day i and sold on day j > i, the profit is best[i] + prices[j] - prices[i] - costs[j - i], so sold[j] is the maximum of this over all i < j. Buying and selling on the same day only costs money, so it is never useful. The answer is the largest sold[j], or 0 if there is none.
Complexity: O(n2) time and O(n) extra space.
class Solution:
def maxProfit(self, prices: List[int], cooldown: int, costs: List[int]) -> int:
n = len(prices)
# best[d]: most profit from finished trades that allow buying on day d (the last sale is on day d - cooldown - 1 or earlier)
best = [0] * (n + 1)
# sold[j]: most profit when the last trade is sold on day j
sold = [0] * n
ans = 0
for j in range(n):
if j - cooldown - 1 >= 0:
best[j] = max(best[j], sold[j - cooldown - 1])
if j > 0:
best[j] = max(best[j], best[j - 1])
sold[j] = max(
[best[i] + prices[j] - prices[i] - costs[j - i] for i in range(j)],
default=0,
)
ans = max(ans, sold[j])
return ans