class Solution {
    public int maxProfit(int[] prices, int fee) {
        // 买第一天股票所需要的全部费用(买入)
        int buy = prices[0] + fee; 
        // 利润总和
        int sum = 0;
        for(int p:prices){
            // 如果买后些天的股票所需的全部费用比第一天的少,就买后边这天的(买入)
            if(p + fee < buy){
                buy = p + fee;
            // 如果股票的价格比上回花的钱都多(卖出)
            }else if(p > buy){
                // 计算利润
                sum += p-buy;
                // 更新上一次买股票的钱数
                buy = p;
            }
        }
        return sum;
    }
}






![[ 数据结构 ] 查找算法--------线性、二分、插值、斐波那契查找](https://img-blog.csdnimg.cn/img_convert/10bb5ebed35220bdf74e2cbb24f7205e.png)













