A portfolio tool shows daily prices in prices. You may complete at most k trades, where a trade is one buy followed later by one sell, and you may hold at most one share at a time.
Write profitWithLimit(prices, k) that returns the largest total profit.
Input: prices = [4,3,7,6,1,4], k = 2 Output: 7
Buy at 3 and sell at 7, then buy at 1 and sell at 4: 4 + 3.
Input: prices = [4,3,7,6,1,4], k = 1 Output: 4
With one trade, buying at 3 and selling at 7 is best.
1 <= prices.length <= 10000 <= prices[i] <= 10000 <= k <= 100Run 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).
I'm here to help with #555 At Most K Trades. 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.