继承leetcode123以及leetcode309的思路,,但应该也可以写成leetcode 152. 乘积最大子序列的形式
class Solution { public: int maxProfit(vector<int>& prices, int fee) { int len=prices.size(); if(len<=1) return 0; vector<int> buy(len,-prices[0]-fee); vector<int> sell(len,0); int res=0; for(int i=1;i<len;i++){ int p=prices[i]; buy[i]=max(buy[i-1],sell[i-1]-p-fee); sell[i]=max(sell[i-1],buy[i]+p); res=max(sell[i],res); } return res; } };