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

122. 买卖股票的最佳时机 II状态定义一模一样。

转移方程如下:

\[f(i,0) = \max(f(i-1,0),f(i-1,1)+prices[i]-fee); \\ f(i,1) = \max(f(i-1,1),f(i-1,0)-prices[i]); \]

class Solution {
public:
    static const int N=5e4+10;
    int f[N][2];
    int maxProfit(vector<int>& prices, int fee) {
        int n=prices.size();
        
        f[0][0]=0,f[0][1]=-prices[0];
        for(int i=1;i<n;i++)
        {
            f[i][0]=max(f[i-1][0],f[i-1][1]+prices[i]-fee);
            f[i][1]=max(f[i-1][1],f[i-1][0]-prices[i]);
        }
        return f[n-1][0];
    }
};
posted @ 2021-03-23 13:01  Dazzling!  阅读(17)  评论(0编辑  收藏  举报