πDay 16. Stock Buy and Sell β Max K Transactions Allowed π§
The problem can be found at the following link: Question Link
π‘ Problem Description:
In the stock market, a person can buy a stock and sell it on a future date. You are given an array prices[] representing stock prices on different days and a positive integer k.
Find out the maximum profit a person can make with at most k transactions.
A transaction consists of buying and subsequently selling a stock. A new transaction can only start after completing the previous one.
π Example Walkthrough:
Example 1:
Input:
prices = [10, 22, 5, 80]
k = 2Output:
87Explanation:
Buy at 10, Sell at 22 β Profit = 12
Buy at 5, Sell at 80 β Profit = 75 Total Profit = 12 + 75 = 87
Example 2:
Input:
Output:
Explanation:
Buy at 20, Sell at 580 β Profit = 560
Buy at 420, Sell at 900 β Profit = 480 Total Profit = 560 + 480 = 1040
Example 3:
Input:
Output:
Explanation:
The stock price is continuously decreasing, so there is no profit possible.
Constraints:
$(1 \leq \text{prices.size()} \leq 10^3)$
$(1 \leq k \leq 200)$
$(1 \leq \text{prices}[i] \leq 10^3)$
π― My Approach:
Optimized Dynamic Programming
If 2 * k >= n, it's optimal to perform all profitable transactions, similar to an unlimited transactions problem.
Otherwise, we use a 1D DP table (
dp[2*k+1]) to store the best profit for different transaction states:Odd indices β Buying states
Even indices β Selling states
Iterate through the stock prices and update
dp[]based on previous values:If buying, maximize profit by either holding or buying today.
If selling, maximize profit by either selling today or holding.
Algorithm Steps:
If
k == 0, return 0 (no transactions allowed).If 2 * k >= n, return the sum of all upward price differences.
Initialize
dp[2*k+1]with-βfor buy states and0for the rest.Iterate through prices and update
dp[j]for all transactions.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(K Γ N), since we iterate over
prices[]for each transaction.Expected Auxiliary Space Complexity: O(2K), since we only use a
dp[]array of size2K + 1.
π Solution Code
Code (C++)
β‘ Alternative Approaches
2οΈβ£ Dynamic Programming (O(KΓN) Time, O(KΓN) Space) β 2D DP
Algorithm Steps:
Use a 2D DP table, where
dp[i][j]represents the maximum profit at dayjwith at mostitransactions.Base Case:
dp[0][j] = 0(No transactions, no profit).dp[i][0] = 0(At day 0, profit is zero).
Recurrence Relation: $[ dp[i][j] = \max(dp[i][j-1], prices[j] + \max(dp[i-1][p] - prices[p]) \quad \text{for } 0 \leq p < j ]$
dp[i][j-1]: No transaction on dayj.prices[j] + max(dp[i-1][p] - prices[p]): Buy at somep, sell atj.
β
Time Complexity: O(K Γ N)
β
Space Complexity: O(K Γ N)
3οΈβ£ Recursive + Memoization (O(KΓN) Time, O(KΓN) Space)
Algorithm Steps:
Define a recursive function
solve(i, t, holding), where:iis the current index (day).tis the number of transactions left.holdingistrueif we own a stock.
Base Cases:
If
i == nort == 0, return0.
Recurrence Relation:
If
holding:Sell:
prices[i] + solve(i + 1, t - 1, false).Hold:
solve(i + 1, t, true).
If not holding:
Buy:
-prices[i] + solve(i + 1, t, true).Skip:
solve(i + 1, t, false).
Use Memoization (
dp[i][t][holding]) to store computed values.
β
Time Complexity: O(K Γ N)
β
Space Complexity: O(K Γ N) (recursion stack)
Comparison of Approaches
Approach
β±οΈ Time Complexity
ποΈ Space Complexity
β Pros
β οΈ Cons
1D DP (Space Optimized)
π‘ O(K Γ N)
π’ O(2K)
Best space-efficient solution
Harder to understand
2D DP (Tabulation)
π‘ O(K Γ N)
π΄ O(K Γ N)
Intuitive approach
High space usage
Recursive + Memoization
π‘ O(K Γ N)
π΄ O(K Γ N)
Natural recursion flow
Stack overhead
β Best Choice?
If optimizing space: Use 1D DP (Space-Optimized).
If space is not a concern: Use 2D DP (Tabulation) for easier understanding.
For recursion lovers: Use Recursive + Memoization.
Code (Java)
Code (Python)
π― Contribution and Support:
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Letβs make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated