Maximum Profit in Stock Trading with Transaction Fee
Problem
Given an array prices where prices[i] is the stock price on day i, and an integer fee charged once per completed transaction (a buy+sell pair), find the maximum profit achievable. You may complete as many transactions as you like but must sell your current holding before buying again.
Input / Output
- Input: prices (int array), fee (int).
- Output: maximum total profit.
Constraints
- 1 ≤ prices.length ≤ 5×10^4
- 1 ≤ prices[i] < 5×10^4
- 0 ≤ fee < 5×10^4
Example
- prices=[1,3,2,8,4,9], fee=2 -> 8 (buy@1 sell@8 profit 5, buy@4 sell@9 profit 3 → total 8)
added …