> 文档中心 > 714. 买卖股票的最佳时机含手续费

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


思路

和之前可以多次购买股票的思路一样,就是在买入股票的时候加上手续费就好了。

LeetCode122. 买卖股票的最佳时机 II_想进阿里的小菜鸡的博客-CSDN博客

代码

class Solution {    public int maxProfit(int[] prices, int fee) { int dp[][] = new int[prices.length][2]; dp[0][0]=-prices[0]-fee; dp[0][1] = 0; for(int i =1;i<prices.length;i++){     dp[i][0]=Math.max(dp[i-1][1]-prices[i]-fee,dp[i-1][0]);     dp[i][1]=Math.max(dp[i-1][1],dp[i-1][0]+prices[i]); } return Math.max(dp[prices.length-1][0],dp[prices.length-1][1]);    }}