给定一个整数数组 prices,其中第 i 个元素代表了第 i 天的股票价格 ;非负整数 fee 代表了交易股票的手续费用。
你可以无限次地完成交易,但是你每笔交易都需要付手续费。如果你已经购买了一个股票,在卖出它之前你就不能再继续购买股票了。
返回获得利润的最大值。
注意:这里的一笔交易指买入持有并卖出股票的整个过程,每笔交易你只需要为支付一次手续费。
状态方程:
cash[i] :标识第i天没有持有股票情况下的最大获利
hold[i] :标识第i天持有股票情况下的最大获利
cash[i] = max(cash[i - 1], hold[i - 1] + prices[i] - fee)
hold[i] = max(hold[i - 1], cash[i - 1] - prices[i])
int maxProfit(vector<int>& prices, int fee) {
int cash = 0;
int hold = -prices[0];
for(int i = 1; i < prices.size(); i++) {
cash = max(cash, hold + prices[i] - fee);
hold = max(hold, cash - prices[i]);
}
return max(hold, cash);
}

284

被折叠的 条评论
为什么被折叠?



