【算法/贪心算法/股票问题】题解+详细备注(共2题)
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n = prices.size();
int result{};
for(int i{};i<n-1;++i){
if(prices[i+1] - prices[i] > 0){
result+=(prices[i+1]-prices[i]);
}
}
return result;
}
};
class Solution {
public:
int maxProfit(vector<int>& prices, int fee) {
int n = prices.size();
int result{};
int minPrices{prices[0]};
for(int i = 1;i<n;++i){
if(prices[i] < minPrices) minPrices = prices[i];
if(prices[i] > minPrices && prices[i] <= minPrices+ fee){
continue;
}
if(prices[i] - minPrices > fee){
result +=(prices[i] -minPrices - fee);
minPrices=prices[i] - fee;
}
}
return result;
}
};