Stock Profit With At Most k Trades
Updated
Problem statement
You are given an integer k and an array prices, where prices[i] is the price of a stock on day i.
A trade means buying one share on some day and selling it on a later day. You may make at most k trades, and you can hold at most one share at a time, so a share must be sold before the next one is bought (selling and buying again on the same day is allowed).
Return the largest total profit you can make. If no trade earns money, return 0.
Function
maxProfitWithKTrades(k: int, prices: int[]) → intExamples
Example 1
k = 2prices = [3,8,2,9,4,10]return = 13Two trades: buy at 3 and sell at 8 (profit 5), then buy at 2 and sell at 10 (profit 8), for 13. Buying at 2, selling at 9, then buying at 4 and selling at 10 also gives 7 + 6 = 13. Taking all three rises would give 18, but that needs three trades.
Example 2
k = 3prices = [9,7,4,1]return = 0The price only falls, so every trade would lose money. Making no trade gives 0.
Example 3
k = 1prices = [2,6,1,7,3,9]return = 8With one trade, buy at the lowest price before the highest later price: buy at 1, sell at 9, profit 8.
Constraints
1 <= k <= 1001 <= prices.length <= 10000 <= prices[i] <= 10^4
More Adobe Phone Screen questions
- Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
- All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
- Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
- Binary Tree Level-Order Traversal by LevelsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Count Occurrences in a Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Count Pairs with Absolute Difference KPHONE SCREEN · Seen Jul 2026
- Count Subsets with Sum KPHONE SCREEN · Seen Jul 2026
- Numbers with Exactly Three DivisorsPHONE SCREEN · Seen Jul 2026
- Sum of Even Nodes on a Path in a Numbered Binary TreePHONE SCREEN · Seen Jul 2026
- Largest Value in Every Sliding WindowPHONE SCREEN · Seen Jul 2026
Similar questions at other companies
- Split Array to Minimize the Largest SumNation with NaMo · PHONE SCREEN
- Trapping Rain WaterOracle · PHONE SCREENOracle interview questions
- Minimum Length After Removing Arithmetic TriplesMedia.net · PHONE SCREEN
- Select Transactions with Child-Pays-for-Parent PackagesCoinbase · PHONE SCREENCoinbase interview questions
- Longest Consistent Activity StreakAtlassian · PHONE SCREENAtlassian interview questions
- Longest Increasing Path in a MatrixNew Relic · PHONE SCREEN · ONSITE INTERVIEW
- Longest Increasing Subsequence With Bounded Adjacent DifferenceGoogle · PHONE SCREENGoogle interview questions
- Longest Increasing Subsequence With Bounded Adjacent DifferenceAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Maximum Profit with at Most K TransactionsTekion · PHONE SCREENTekion interview questions
- Count Ordered Combination Sums with Negative ValuesPinterest · PHONE SCREENPinterest interview questions
Interview reports that mention this question
Adobe interview experiences
- Adobe Web, QA, DevOps, ML Roles: 13 Experiences (Part 3 of 3)
- Adobe SWE Interviews, Level Unknown: 25 Experiences (Part 2 of 3)
- Adobe Interviews, Level Unknown: 38 Experiences (Part 1 of 3)
- Adobe New Grad MTS Interview Questions: 7 Experiences
- Adobe ML/Research Intern Interviews: 12 Experiences (Part 4 of 4)