From 8b415fb0a495ee4ae58ad9273c75db34bc45845a Mon Sep 17 00:00:00 2001 From: kaif969 Date: Wed, 29 Oct 2025 21:46:05 +0530 Subject: [PATCH 1/4] Implement numTrees function to calculate unique binary search trees --- 96. Unique Binary Search Trees.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 96. Unique Binary Search Trees.cpp diff --git a/96. Unique Binary Search Trees.cpp b/96. Unique Binary Search Trees.cpp new file mode 100644 index 0000000..7be39aa --- /dev/null +++ b/96. Unique Binary Search Trees.cpp @@ -0,0 +1,11 @@ +class Solution { +public: + int dp[20]{}; + int numTrees(int n) { + if(n <= 1) return 1; + if(dp[n]) return dp[n]; + for(int i = 1; i <= n; i++) + dp[n] += numTrees(i-1) * numTrees(n-i); + return dp[n]; + } +}; \ No newline at end of file From f13fdda56d3409a6e1940411a18e6ada3fa4f4f1 Mon Sep 17 00:00:00 2001 From: kaif969 Date: Thu, 30 Oct 2025 01:03:12 +0530 Subject: [PATCH 2/4] Implement dynamic programming solution for Best Time to Buy and Sell Stock IV --- 188. Best Time to Buy and Sell Stock IV.cpp | 27 +++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 188. Best Time to Buy and Sell Stock IV.cpp diff --git a/188. Best Time to Buy and Sell Stock IV.cpp b/188. Best Time to Buy and Sell Stock IV.cpp new file mode 100644 index 0000000..b516804 --- /dev/null +++ b/188. Best Time to Buy and Sell Stock IV.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + vector>> dp; + + int profit(vector& 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& prices) { + int n = prices.size(); + dp = vector>>(n, vector>(2, vector(k + 1, -1))); + return profit(prices, 0, 1, k); + } +}; \ No newline at end of file From 561ac97843f3a7822143bb88f9f394d353b05c02 Mon Sep 17 00:00:00 2001 From: Sk Kaif Uddin <116540325+kaif969@users.noreply.github.com> Date: Thu, 30 Oct 2025 14:00:48 +0530 Subject: [PATCH 3/4] Delete 135. Candy.cpp --- 135. Candy.cpp | 24 ------------------------ 1 file changed, 24 deletions(-) delete mode 100644 135. Candy.cpp diff --git a/135. Candy.cpp b/135. Candy.cpp deleted file mode 100644 index f276999..0000000 --- a/135. Candy.cpp +++ /dev/null @@ -1,24 +0,0 @@ -class Solution { -public: - int candy(vector& ratings) { - int n = ratings.size(); - vector count(n, 1); // Step 1: Initialize with 1 - - // Step 2: Left to Right - for (int i = 1; i < n; i++) { - if (ratings[i] > ratings[i - 1]) { - count[i] = count[i - 1] + 1; - } - } - - // Step 3: Right to Left - for (int i = n - 2; i >= 0; i--) { - if (ratings[i] > ratings[i + 1]) { - count[i] = max(count[i], count[i + 1] + 1); - } - } - - // Step 4: Total candies - return accumulate(count.begin(), count.end(), 0); - } -}; From 10d3957436f4f83ffb1de9ea98a548b2ef2cb7d3 Mon Sep 17 00:00:00 2001 From: Sk Kaif Uddin <116540325+kaif969@users.noreply.github.com> Date: Thu, 30 Oct 2025 14:01:26 +0530 Subject: [PATCH 4/4] Delete 188. Best Time to Buy and Sell Stock IV.cpp --- 188. Best Time to Buy and Sell Stock IV.cpp | 27 --------------------- 1 file changed, 27 deletions(-) delete mode 100644 188. Best Time to Buy and Sell Stock IV.cpp diff --git a/188. Best Time to Buy and Sell Stock IV.cpp b/188. Best Time to Buy and Sell Stock IV.cpp deleted file mode 100644 index b516804..0000000 --- a/188. Best Time to Buy and Sell Stock IV.cpp +++ /dev/null @@ -1,27 +0,0 @@ -class Solution { -public: - vector>> dp; - - int profit(vector& 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& prices) { - int n = prices.size(); - dp = vector>>(n, vector>(2, vector(k + 1, -1))); - return profit(prices, 0, 1, k); - } -}; \ No newline at end of file