Skip to content

Conversation

@kaif969
Copy link
Contributor

@kaif969 kaif969 commented Oct 29, 2025

PR Title Format: 188. Best Time to Buy and Sell Stock IV.cpp

Intuition

We want to maximize profit by making at most k stock transactions. Each day, we can choose to buy, sell, or skip. Using recursion with memoization (3D DP), we track the day, buy/sell state, and remaining transactions.

Approach

Use a recursive function profit(prices, i, isBuy, k):

Base case: if we reach the end of prices or no transactions left, return 0.

  • If isBuy == 1: choose to buy today or skip.

  • If isBuy == 0: choose to sell today or skip.

Store results in a 3D DP array dp[i][isBuy][k] to avoid recomputation.

Finally, call profit(prices, 0, 1, k) starting from day 0 with buy allowed.

Code Solution (C++)

class Solution {
public:
    vector<vector<vector<int>>> dp;

    int profit(vector<int>& prices, int i, int isBuy, int k) {
        if (i == prices.size() || k == 0) return 0;

        if (dp[i][isBuy][k] != -1) return dp[i][isBuy][k];

        int a, b;
        if (isBuy) {
            a = profit(prices, i + 1, 1, k);
            b = profit(prices, i + 1, 0, k) - prices[i];
        } else {
            a = profit(prices, i + 1, 0, k);
            b = profit(prices, i + 1, 1, k - 1) + prices[i];
        }

        return dp[i][isBuy][k] = max(a, b);
    }

    int maxProfit(int k, vector<int>& prices) {
        int n = prices.size();
        dp = vector<vector<vector<int>>>(n, vector<vector<int>>(2, vector<int>(k + 1, -1)));
        return profit(prices, 0, 1, k);
    }
};
    

Related Issues

#261

By submitting this PR, I confirm that:

  • This is my original work not totally AI generated
  • I have tested the solution thoroughly on leetcode
  • I have maintained proper PR description format
  • This is a meaningful contribution, not spam

Summary by Sourcery

  • Adds a C++ solution for LeetCode 188 (Best Time to Buy and Sell Stock IV).
  • Uses recursive memoization with a 3D DP array dp[i][isBuy][k] and a helper profit(prices, i, isBuy, k).
  • Time complexity O(n * k), space O(n * k). Starts from profit(prices, 0, 1, k).
  • Suggestion: if k >= n/2, reduce to the unlimited-transactions case for O(n) solution.

Summary by Sourcery

New Features:

  • Introduces a C++ solution for LeetCode 188 (Best Time to Buy and Sell Stock IV) using recursion with memoization.

@sourcery-ai
Copy link

sourcery-ai bot commented Oct 29, 2025

Reviewer's Guide

This PR adds a new C++ solution for LeetCode 188 using a 3D memoized recursion that tracks the current day, buy/sell state, and remaining transactions to compute maximum profit.

Class diagram for the new Solution class (Best Time to Buy and Sell Stock IV)

classDiagram
class Solution {
  +vector<vector<vector<int>>> dp
  +int profit(vector<int>& prices, int i, int isBuy, int k)
  +int maxProfit(int k, vector<int>& prices)
}
Solution : profit(prices, i, isBuy, k) uses dp
Solution : maxProfit(k, prices) uses profit(prices, i, isBuy, k)
Loading

File-Level Changes

Change Details Files
Introduce recursive memoization with 3D DP to handle at most k stock transactions
  • Declare dp as a 3D vector initialized to -1 for memo storage
  • Implement profit(prices, i, isBuy, k) with base cases and buy/sell decision branches
  • Store and reuse computed results in dp[i][isBuy][k] to avoid recomputation
  • Implement maxProfit() to set up dp dimensions and invoke the recursion
188. Best Time to Buy and Sell Stock IV.cpp

Possibly linked issues

  • 242 : Valid Anagram #188: PR adds recursive DP for LeetCode 188, Best Time to Buy and Sell Stock IV, matching issue's problem.
  • 242 : Valid Anagram #188: The PR provides a C++ dynamic programming solution to find the maximum profit for at most k transactions, directly addressing issue 188.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@kaif969
Copy link
Contributor Author

kaif969 commented Oct 31, 2025

@SjxSubham Please 🙏 check this PR also.

@kaif969
Copy link
Contributor Author

kaif969 commented Nov 1, 2025

@SjxSubham, please merge this PR.

@SjxSubham
Copy link
Owner

@kaif969 U r only allowed to contribute upto two solutions only...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants