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.
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.
Input: prices = [3,3,3], fee = 1 Output: 0
No trade makes money.
1 <= prices.length <= 10^51 <= prices[i] <= 500000 <= fee <= 50000Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #554 Trades With a Platform Fee. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.