Trade Repeatedly with a Fee per Completed Transaction

Quick Overview

Trade Repeatedly with a Fee per Completed Transaction. Given daily prices and a fee, maximize total profit from any number of completed buy/sell transactions.

Trade Repeatedly with a Fee per Completed Transaction

Company: Infosys

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Given daily prices and a fee, maximize total profit from any number of completed buy/sell transactions. Hold at most one share at a time and sell before buying again. Charge fee once for each completed transaction. ```cpp class Solution { public: int maxProfit(vector<int>& prices, int fee); }; ``` ### Examples **Example 1:** ```text prices = [2,7,1,8], fee = 2 Output: 8 Explanation: (7-2-2)+(8-1-2)=8. ``` **Example 2:** ```text prices = [9,6,3], fee = 0 Output: 0 Explanation: Making no transactions is optimal. ``` ### Constraints - Prices cover 1 through 50000 days. - Each price is at least 1 and strictly below 50000. - The fee is at least 0 and strictly below 50000. ### Contract considerations Allow zero transactions and exclude simultaneous multiple holdings. ```hint Read the boundary condition What two states summarize every legal prefix strategy? ``` ### Boundary reminders Allow zero transactions and exclude simultaneous multiple holdings.

Overview: Trade Repeatedly with a Fee per Completed Transaction. Given daily prices and a fee, maximize total profit from any number of completed buy/sell transactions.

Infosys logo
Infosys
Sep 8, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Given daily prices and a fee, maximize total profit from any number of completed buy/sell transactions. Hold at most one share at a time and sell before buying again. Charge fee once for each completed transaction.

class Solution {
public:
    int maxProfit(vector<int>& prices, int fee);
};

Examples

Example 1:

prices = [2,7,1,8], fee = 2
Output: 8
Explanation: (7-2-2)+(8-1-2)=8.

Example 2:

prices = [9,6,3], fee = 0
Output: 0
Explanation: Making no transactions is optimal.

Constraints

  • Prices cover 1 through 50000 days.
  • Each price is at least 1 and strictly below 50000.
  • The fee is at least 0 and strictly below 50000.

Contract considerations

Allow zero transactions and exclude simultaneous multiple holdings.

Boundary reminders

Allow zero transactions and exclude simultaneous multiple holdings.