Maximum Stock Profit with a Transaction Limit
Updated
Problem statement
You are given chronological daily prices for one stock in prices, and a nonnegative integer k. Return the greatest total profit from at most k completed transactions.
Each transaction buys exactly one share on one day and sells that share on a strictly later day. Hold at most one share at a time. After selling, you may buy again on a later day. Perform at most one action on any day, and finish without holding a share.
The profit of a transaction is the sale price minus the purchase price. Add the profits of all completed transactions. You may make no transactions, which gives a profit of zero. There is no purchase-budget limit, transaction fee, short selling or additional waiting period.
Empty prices, a single price or k=0 give zero. Do not change the input array. Keep all working state local to each call.
Function
maxProfit(prices: int[], k: int) → intExamples
Example 1
prices = [3,3,5,0,0,3,1,4]k = 2return = 6One optimal choice buys at price 0 and sells at 3, then buys at 1 and sells at 4. The two gains are 3+3=6. Using at most two transactions cannot give a larger total.
Example 2
prices = [3,2,6,5,0,3]k = 1return = 4Only one transaction is allowed. Buying at 2 and selling at 6 gives profit 4, the greatest single-transaction gain.
Example 3
prices = [3,2,6,5,0,3]k = 2return = 7With two transactions allowed, take the gain 6-2=4, then the later gain 3-0=3. Their total is 7.
Constraints
0 <= prices.length <= 40.- Every price is an integer between
0and1000000, inclusive. kis an integer with0 <= k <= 40.
More Akuna Capital Onsite Interview questions
- Match Orders by Price and Arrival TimeOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Positive Nth Root with Gradient DescentPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Count Set Bits in an Integer ArrayONSITE INTERVIEW · Seen Jan 2026
- Remove Duplicates While Preserving OrderPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2025
- First and Last Position in a Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2022
- Maximum Observation in a Time WindowPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2021
- Stream Median, Mean and MaximumPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2021
- Group AnagramsONSITE INTERVIEW · Seen Aug 2021
- Rotate a Square MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Jan 2020
- Trace of a Square MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen May 2019
Similar questions at other companies
- Dungeon GameAmazon · ONSITE INTERVIEW
- Russian Doll EnvelopesGoogle · ONSITE INTERVIEWGoogle interview questions
- Global Charger AssignmentSpotify · ONSITE INTERVIEW
- Minimum Taps to Water a GardenThe D.E. Shaw Group · ONSITE INTERVIEWThe D.E. Shaw Group interview questions
- Longest Increasing Path in a MatrixNew Relic · PHONE SCREEN · ONSITE INTERVIEW
- Longest Increasing Path in a MatrixVisa · ONSITE INTERVIEWVisa interview questions
- Trapping Rain WaterMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Word Break IIBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Russian Doll EnvelopesInMobi · ONSITE INTERVIEWInMobi interview questions
- Maximum Weighted End-Pick ScoreRippling · ONSITE INTERVIEWRippling interview questions
Akuna Capital interview experiences
- Akuna Capital Quant Trader and QR Interviews: 5 Experiences
- Akuna Capital JQR CodePair Rounds: 8 Experiences (Part 6 of 6)
- Akuna Capital QR Probability Phones: 14 Experiences (Part 5 of 6)
- Akuna Capital QR-ML Interviews: 11 Experiences (Part 4 of 6)
- Akuna Capital QR Regression Rounds: 11 Experiences (Part 3 of 6)
- Akuna Capital QR Coding Interviews: 17 Experiences (Part 2 of 6)