0%

LeetCode 714. Best Time to Buy and Sell Stock with Transaction Fee

题目

原题在此

解析

其实这里有官方的全套解析. 我开始觉得我记录这个东西是无用功了.

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
int maxProfit(vector<int>& prices, int fee) {
int res = 0, min = prices[0];
for(int i = 0; i < prices.size(); ++i) {
if(prices[i] < min) min = prices[i];
if(prices[i] > min + fee) {
res += prices[i] - min - fee;
min = prices[i] - fee;
}
}
return res;
}
};