1. 程式人生 > >leetcode714+買賣股票帶交易費,兩個變數DP

leetcode714+買賣股票帶交易費,兩個變數DP

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;
    }
};