-
-
Notifications
You must be signed in to change notification settings - Fork 99
96.Unique Binary Search Trees #260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Reviewer's GuideThis PR introduces two LeetCode problem solutions: a two-pass greedy algorithm for the Candy problem (135) and a top-down memoized DP for counting unique Binary Search Trees (96). Class diagram for Solution class in Unique Binary Search Trees (96)classDiagram
class Solution {
+int dp[20]
+int numTrees(int n)
}
Class diagram for Solution class in Candy problem (135)classDiagram
class Solution {
+int candy(vector<int>& ratings)
}
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this 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 - here's some feedback:
- The dp array in the Unique BST solution is declared as size 20 but needs to support indices up to n=20, so increase it to at least dp[21] (or use a vector) to avoid out-of-bounds access.
- Use a 64-bit type (long long or uint64_t) for Catalan numbers when n can be 20, since Catalan(20) exceeds 32-bit integer range and will overflow.
- This PR currently includes solutions for both 'Candy' and 'Unique Binary Search Trees'—consider splitting them into separate PRs to keep each review focused.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The dp array in the Unique BST solution is declared as size 20 but needs to support indices up to n=20, so increase it to at least dp[21] (or use a vector) to avoid out-of-bounds access.
- Use a 64-bit type (long long or uint64_t) for Catalan numbers when n can be 20, since Catalan(20) exceeds 32-bit integer range and will overflow.
- This PR currently includes solutions for both 'Candy' and 'Unique Binary Search Trees'—consider splitting them into separate PRs to keep each review focused.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
kaif969
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SjxSubham delete the other two files. Please check.
🎉 Congrats on getting your PR merged in, @kaif969! 🙌🏼Thanks for your contribution every effort helps improve the project. Looking forward to seeing more from you! 🥳✨ |
PR Title Format: 96. Unique Binary Search Trees.cpp
Intuition
Choose any value i (1 ≤ i ≤ N) as root. Values
[1..i-1]must go to the left subtree (size i-1) and values[i+1..N]to the right subtree (size N-i). If G(n) is the number of unique BSTs with n nodes, then for a fixed root i there are G(i-1) * G(N-i) distinct BSTs (left and right subtrees combine independently). Summing over all possible roots yields the recurrence:G(N) = sum_{i=1..N} G(i-1) * G(N-i)
Base cases: G(0) = 1 (empty tree), G(1) = 1.
This is the Catalan-number recurrence. A DP solution computes
G[0..N]bottom-up (or uses memoized recursion) in O(N^2) time and O(N) space. Example: for N = 3,G(3) = G(0)G(2) + G(1)G(1) + G(2)G(0) = 2 + 1 + 2 = 5.
Approach
We solve this using the Catalan-number DP recurrence with two equivalent implementation styles: top-down memoized recursion or bottom-up iteration.
Initialization
n ≤ 20. Initializedp[0] = 1anddp[1] = 1.Recurrence
dp[n] = sum_{i=1..n} dp[i-1] * dp[n-i]Top-down (memoized recursion)
dp[n]is already computed, return it (avoids repeated work).dp[n]by looping i=1..n and accumulatingnumTrees(i-1) * numTrees(n-i)where recursive calls fill smaller dp entries.dp[n].Bottom-up (iterative DP)
2..n: for root=1..nodes: dp[nodes] += dp[root-1] * dp[nodes-root]Complexity
Code Solution (C++)
Related Issues
#259
By submitting this PR, I confirm that:
Summary by Sourcery
Introduce C++ DP-based solutions for the Candy and Unique Binary Search Trees problems on LeetCode.
New Features: