LeetCode 714. Best Time to Buy and Sell Stock with Transaction Fee Posted on 2020-12-17 Edited on 2024-11-24 In LeetCode DPGreedy1234567891011class Solution {public: int maxProfit(vector<int>& prices, int fee) { int cash = 0, hold = -prices[0]; for (int i = 1; i < prices.size(); ++i) { cash = max(cash, hold + prices[i] - fee); hold = max(hold, cash - prices[i]); } return cash; }};12345678910111213141516class Solution {public: int maxProfit(vector<int>& prices, int fee) { int buy = prices[0] + fee; int profit = 0; for (int i = 1; i < prices.size(); ++i) { if (prices[i] + fee < buy) { buy = prices[i] + fee; } else if (prices[i] > buy) { profit += prices[i] - buy; buy = prices[i]; } } return profit; }};