Stock K Transactions Problem
Stock K Transactions Problem — ExecCode Hard DSA Practice
Solve the Stock K Transactions problem on ExecCode. Free online hard DSA practice in DP. Write and run code in Java, C++, Python — no signup required to run.
Problem description
You may complete at most k buy-sell transactions (a buy followed later by a sell). You cannot hold two positions. This is the k-transaction generalization used in Goldman/Amazon OA hard sets. When k is huge it reduces to summing every uphill. Return the maximum profit. If no profit is possible, 0. You are given: k — max transactions prices — daily prices Return integer profit. k = 0 means profit 0.
Examples
Input {'k': 2, 'prices': [3, 2, 6, 5, 0, 3]}; Output 7. Input {'k': 1, 'prices': [1, 2, 3, 4]}; Output 3. Input {'k': 2, 'prices': [7, 6, 4]}; Output 0
Constraints
You must sell before buying again Same-day buy and sell is allowed but useless
Practice Stock K Transactions free on ExecCode. Browse DSA problems, topic map, and placement guides.