买卖股票的最佳时机含手续费

题目链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/
题目描述:
买卖股票的最佳时机含手续费
相关题:买卖股票的最佳时机||
题解:
买卖股票的最佳时机含手续费
参考链接:买卖股票的最佳时机含手续费题解

class Solution {
public:
    int maxProfit(vector<int>& prices, int fee) {
        int result = 0;
        int inHand = prices[0];
        for(int i = 1; i < prices.size(); i++)
        {
            if(inHand > prices[i])
                inHand = prices[i];
            else if(inHand < prices[i] - fee)
            {
                result += prices[i] - fee - inHand;
                inHand = prices[i] - fee;
            }

        }
        return result;
    }
};

上一篇:leetcode 714买卖股票的最佳时机含手续费 贪心算法


下一篇:<> 第四版Exercise Section 8.4.1 练习题