Problem · Array

Maximum Profit with at Most K Transactions

Learn this problem
HardTekion logoTekionFULLTIMEPHONE SCREEN

Problem statement

You are given the price of one batch of goods on consecutive days. You may complete at most k transactions. A transaction consists of buying once and selling later.

  • You must sell before buying again.
  • You may hold at most one batch at a time.
  • A completed buy-and-sell pair counts as one transaction.

Return the maximum profit possible.

Function

maxProfit(prices: int[], k: int) → int

Examples

Example 1

prices = [3,2,6,5,0,3]k = 2return = 7

Buy at 2 and sell at 6, then buy at 0 and sell at 3.

Constraints

  • 1 <= prices.length <= 1000
  • 0 <= k <= 100
  • 0 <= prices[i] <= 10^6

More Tekion problems

drafts saved locally
public int maxProfit(int[] prices, int k) {
    // Write your solution here.
}
prices[3,2,6,5,0,3]
k2
expected7
checking account