Problem
You get an array prices, where prices[i] is the price of a stock on day i. You also get an integer fee, which is the cost of one transaction.
A transaction is one buy and the sell after it. You can do as many transactions as you want. You pay fee one time for each transaction.
You can hold at most one share at a time, so you must sell the share before you buy again. Return the maximum profit.
Example 1
- Input:
- prices = [2,6,3,9], fee = 1
- Output:
- 8
- Explanation:
- Buy at 2 and sell at 6 for a profit of
6 - 2 - 1 = 3. Then buy at 3 and sell at 9 for9 - 3 - 1 = 5.
Example 2
- Input:
- prices = [1,4,2,5], fee = 3
- Output:
- 1
- Explanation:
- Two transactions give 0 each. One transaction from 1 to 5 gives
5 - 1 - 3 = 1.
Example 3
- Input:
- prices = [5,4,3,2], fee = 1
- Output:
- 0
- Explanation:
- The price only goes down, so the best plan is to do no transactions.
Constraints
- 1 <= prices.length <= 5 * 10^4
- 1 <= prices[i] < 5 * 10^4
- 0 <= fee < 5 * 10^4
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
- Best Time to Buy and Sell Stock
Coding · Array · Dynamic Programming
- Best Time to Buy and Sell Stock II
Coding · Array · Dynamic Programming · Greedy
- Best Time to Buy and Sell Stock III
Coding
- Best Time to Buy and Sell Stock IV
Coding
Sources · 1 reference
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 1 record
No company association
Leetcode 714. Best Time to Buy and Sell Stock with Transaction Fee
Reported date not specified · 0 source reports
Source snapshot: Nov 12, 2026
View source