1180. Buy and Sell with Transaction Fee

MediumDynamic ProgrammingStock DP

Given daily stock prices and a fixed transaction fee charged per completed transaction, you may complete unlimited transactions. Return the maximum profit. The input is JSON {prices, fee}.

Input: JSON {prices, fee}.

Output: Integer — the maximum profit.

Examples

Example 1
Input: {"prices":[1,3,2,8,4,9],"fee":2}
Output: 8
Explanation: Two transactions after fees.
Example 2
Input: {"prices":[1,3,7,5,10,3],"fee":3}
Output: 6
Explanation: Optimal after fees.
Example 3
Input: {"prices":[1],"fee":1}
Output: 0
Explanation: No transaction.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →