Skip to the code
cf.
+25 XP

#554 Trades With a Platform Fee

medium

A trading app charges a flat fee for every completed trade (one buy plus one sell). Given daily prices, you may make as many trades as you like, holding at most one share at a time.

Write profitAfterFees(prices, fee) that returns the largest total profit after fees.

Example 1

Input: prices = [2,4,1,7,5,10], fee = 2
Output: 7

Buy at 1 and sell at 10: 9 - 2 = 7. Two trades (1 to 7 and 5 to 10) also give 4 + 3 = 7.

Example 2

Input: prices = [3,3,3], fee = 1
Output: 0

No trade makes money.

Constraints

  • 1 <= prices.length <= 50000
  • 1 <= prices[i] <= 50000
  • 0 <= fee <= 50000
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).