Solve the classic Best Time to Buy and Sell Stock problem with various constraints, such as allowing multiple transactions or incorporating transaction fees.
Implement max_profit(prices, max_transactions=None, fee=0). Return the maximum profit, where each transaction consists of one buy followed by one sell, transactions cannot overlap, and the fee is charged on each sale. If max_transactions is None, any number of transactions is allowed.
Input: a list of integer prices, an optional nonnegative transaction limit, and a nonnegative fee. Output: one integer maximum profit.
def max_profit(prices, max_transactions=None, fee=0):