FastPrepBest Stock Profit With a Sell Fee

Best Stock Profit With a Sell Fee

Kaseya logoKaseya● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

You may buy and sell one share any number of times, but may hold at most one share. Every sale pays a fixed sellFee. Return the maximum achievable profit.

A sale may occur only after its corresponding purchase.

Function

maxProfitWithSellFee(prices: int[], sellFee: int) → int

Examples

Example 1

prices = [1,3,2,8,4,9]sellFee = 2return = 8

Case 1 exercises the documented deterministic contract.

Example 2

prices = [1,3,7,5,10,3]sellFee = 3return = 6

Case 2 exercises the documented deterministic contract.

Example 3

prices = [5]sellFee = 2return = 0

Case 3 exercises the documented deterministic contract.

Constraints

  • 1 <= prices.length <= 200000.
  • 0 <= prices[i], sellFee <= 10^6.
  • The answer fits a signed 32-bit integer.
See Kaseya hiring insights
public int maxProfitWithSellFee(int[] prices, int sellFee) {
    // Write your code here.
}
prices[1,3,2,8,4,9]
sellFee2
expected8
Checking account…