Stock Profit with a Transaction Limit
Updated
Problem statement
You are given an array prices, where prices[i] is the price of one share on day i, and a nonnegative integer k.
Return the largest total profit from at most k completed buy-and-sell transactions. Hold at most one share at a time. Buy before selling that share, and sell before starting another transaction. You may choose to make no trades.
Progressive versions
- With
k = 1, solve the single-transaction version in one pass. - When the transaction limit cannot restrict the answer, solve the unlimited-transactions version.
- Handle a general transaction limit and explain how to reduce the memory used by dynamic programming.
There are no transaction fees or cooldown days. A buy and its sale must occur on different days.
Function
maxStockProfit(prices: int[], k: int) → intExamples
Example 1
prices = [3,2,6,5,0,3]k = 2return = 7Buy at 2 and sell at 6 for a profit of 4. Then buy at 0 and sell at 3 for a profit of 3. The total profit is 7.
Example 2
prices = [7,1,5,3,6,4]k = 1return = 5Buy at 1 and sell at 6. Their difference is 5, which is the largest profit from one transaction.
Example 3
prices = [1,3,2,5]k = 2000return = 5Take the rise from 1 to 3 and the rise from 2 to 5. These two transactions earn 2 + 3 = 5.
Constraints
0 <= prices.length <= 2000.0 <= prices[i] <= 10^6.0 <= k <= 2000.
More Citadel Phone Screen questions
- Portfolio Tickers with Constant-Time Random PickPHONE SCREEN · Seen Nov 2026
- Square Root FunctionPHONE SCREEN · Seen Oct 2026
- Reachable Transfer PositionsPHONE SCREEN · Seen Oct 2026
- Spreadsheet Formula DependenciesPHONE SCREEN · Seen Oct 2026
- Merge Price-Delta FeedsPHONE SCREEN · Seen Sep 2026
- Reconcile Unmatched Trades with Timestamp TolerancePHONE SCREEN · Seen Sep 2026
- Top Ten Trades by Notional ValuePHONE SCREEN · Seen Sep 2026
- Exchange BBO and Market-Wide NBBOPHONE SCREEN · Seen Sep 2026
- Maximize Workday EarningsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Minimum Changes for Palindromic BlocksOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Sep 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