题目链接:https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/

cpp
class Solution {
public:
int maxProfit(vector<int>& prices, int fee)
{
/*时空复杂度O(n)*/
int n = prices.size();
// 1. 创建dp表
vector<int> f(n);
auto g = f;
// 2. 初始化
f[0] = -prices[0];
// 3. 填表
for (int i = 1; i < n; ++i)
{
f[i] = max(f[i - 1], g[i - 1] - prices[i]);
g[i] = max(g[i - 1], f[i - 1] + prices[i] - fee);
}
// 4. 返回值
return g[n - 1];
}
};