714. 买卖股票的最佳时机含【手续费】

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


链接

题目描述

在这里插入图片描述

代码

在每次买入的时候计算手续费即可

class Solution {
    public int maxProfit(int[] prices, int fee) {
        if(prices == null || prices.length == 0){
            return 0;
        }
        int n = prices.length;
        int[][] dp = new int[n][2];
        for(int i = 0; i < n ;i++){
            if(i == 0){
                dp[i][0] = 0;
                dp[i][1] = -prices[i]-fee;
                continue;
            }
        
            dp[i][0] = Math.max(dp[i-1][0],dp[i-1][1]+prices[i]);
            dp[i][1] = Math.max(dp[i-1][1],dp[i-1][0]-prices[i]-fee);
        }
        return dp[n-1][0];
    }
}
发布了55 篇原创文章 · 获赞 1 · 访问量 861

猜你喜欢

转载自blog.csdn.net/weixin_42469108/article/details/105114045
今日推荐