leetcode714+买卖股票带交易费,两个变量DP

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013554860/article/details/83746817

https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/

class Solution {
public:
    int maxProfit(vector<int>& prices, int fee) {
        int cash = 0, i=0;
        int hold = -prices[0];
        for(i=1; i<prices.size(); i++){
            cash = max(cash, hold+prices[i]-fee);
            hold = max(hold, cash-prices[i]);
        }
        return cash;
    }
};

猜你喜欢

转载自blog.csdn.net/u013554860/article/details/83746817