Best Time to Buy and Sell Stock with Transaction Fee
MediumDynamic Programming
Decode it first. Clues, constraints, pattern and plan before any code.prices[i] is a stock's price on day i, and every completed trade (a buy followed by a sell) costs a flat fee. You may make as many transactions as you like, but you can hold at most one share at a time. Return the maximum profit.
prices = [1, 3, 2, 8, 4, 9], fee = 2 → 8 (buy 1, sell 8, buy 4, sell 9)
prices = [1, 3, 7, 5, 10, 3], fee = 3 → 6Constraints
1 <= prices.length <= 5 * 10^41 <= prices[i] < 5 * 10^40 <= fee < 5 * 10^4