leetcode 714. 买卖股票的最佳时机含手续费 medium
题目描述:
解题思路:
手续费,可以在买入扣,也可以在卖出的时候扣,只不过应该是第0天初始化,而不再是第-1天。否则可能值越界,或者有别的什么问题,没想好 oRZ
代码:
//
class Solution {
public:
int maxProfit(vector<int>& prices, int fee) {
if (prices.size() < 2)
return 0;
int dp_0 = 0, dp_1 = -prices[0] - fee; // 买入添加手续费
for (int i = 1; i < prices.size(); i++){
int tmp = dp_0;
dp_0 = max(dp_0, dp_1 + prices[i]);
dp_1 = max(dp_1, tmp - prices[i]-fee);
}
return dp_0;
}
};