...with Transaction Fee
Given an array prices where prices[i] is the price of a stock on day i and an integer fee, find the maximum profit. You may complete as many transactions as you like, but each completed transaction (a buy paired with a later sell) incurs the given transaction fee. You may not hold more than one share at a time.
Open official problem prompt ↗Maximize profit with unlimited trades when every completed trade costs a fixed fee.
Like a market stall that charges a flat commission each time you sell: you only bother flipping goods when the price jump comfortably exceeds the commission, otherwise you hold.
- Input
- prices = [1, 3, 2, 8, 4, 9], fee = 2
- Output
- 8
- Why
- Buy at 1 sell at 8 nets 7-2=5, buy at 4 sell at 9 nets 5-2=3; total 8.
1 <= prices.length <= 5 * 10^41 <= prices[i] < 5 * 10^40 <= fee < 5 * 10^4