Track Stock State with a Fee
Track two states each day, holding a share or not, and charge the fee when you sell.
The problem
Implement max_profit_with_fee(prices, fee). prices[i] is the price of a stock on day i. You may buy and sell as many times as you like, but you can own at most one share at a time, so you must sell before you buy again. Every sale costs fee. Return the largest total profit you can end with. If prices is empty or no trade is worth it, return 0. For example, with prices [1, 3, 2, 8, 4, 9] and fee 2 the answer is 8: buy at 1 and sell at 8 (profit 5), then buy at 4 and sell at 9 (profit 3).
Starter code
def max_profit_with_fee(prices, fee):
passPublic examples
prices = [1, 3, 2, 8, 4, 9], fee = 2gives8prices = [5, 4, 3], fee = 1gives0
Solving it in the practice book needs an active paid plan.
Open it in the practice bookOne thought at a time.
