FastPrepBest Stock Profit With a Sell Fee
Problems/Best Stock Profit With a Sell Fee

Best Stock Profit With a Sell Fee

Kaseya logoKaseya● MediumFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: The reported task identifies this algorithmic exercise. The executable interface, bounds, deterministic tie rules, and examples are the documented practice contract.

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
Saved
class Solution {
    public int maxProfitWithSellFee(int[] prices, int sellFee) {
        // Write your code here.
    }

}
Checking account…