From c15a1132b0d59524c078394d51d7847174a42eb5 Mon Sep 17 00:00:00 2001 From: glowfi <86939591+glowfi@users.noreply.github.com> Date: Sun, 25 Feb 2024 22:40:26 +0530 Subject: [PATCH] added some dp programs --- ...Length_of_Shortest_Common_Supersequence.py | 118 ++++++++++++++ .../33_Print_Shortest_Common_Supersequence.py | 49 ++++++ .../34_Longest_Palindromic_Substring.py | 33 ++++ .../35_Distinct_Subsequences.py | 119 +++++++++++++++ .../36_Wildcard_Matching.py | 115 ++++++++++++++ .../37_Regular_Expression.py | 17 +++ .../9_DynamicProgramming/38_Edit_Distance.py | 17 +++ ..._to_Buy_and_Sell_Stock_1_Transaction.py.py | 58 +++++++ ...and_Sell_Stock II_Unlimited_Transaction.py | 123 +++++++++++++++ ...o_Buy_and_Sell_Stock_III_2_Transactions.py | 138 +++++++++++++++++ ...to_Buy_and_Sell_Stock_IV_k_Transactions.py | 34 +++++ ...ime_To_Buy_and_Sell_Stock_with_Cooldown.py | 94 ++++++++++++ ...Buy_and_Sell_Stock_with_Transaction_Fee.py | 31 ++++ .../45_Maximum Alternating Subsequence Sum.py | 113 ++++++++++++++ .../46_Longest_Increasing_Subsequence.py | 102 +++++++++++++ .../47_Maximum_Length_of_Pair_Chain.py | 62 ++++++++ .../48_Longest_String_Chain.py | 41 +++++ .../49_Longest_Increasing_Path_in_a_Matrix.py | 44 ++++++ README.md | 20 ++- data.json | 144 ++++++++++++++++++ sheet.csv | 18 +++ 21 files changed, 1489 insertions(+), 1 deletion(-) create mode 100644 Programs/9_DynamicProgramming/32_Length_of_Shortest_Common_Supersequence.py create mode 100644 Programs/9_DynamicProgramming/33_Print_Shortest_Common_Supersequence.py create mode 100644 Programs/9_DynamicProgramming/34_Longest_Palindromic_Substring.py create mode 100644 Programs/9_DynamicProgramming/35_Distinct_Subsequences.py create mode 100644 Programs/9_DynamicProgramming/36_Wildcard_Matching.py create mode 100644 Programs/9_DynamicProgramming/37_Regular_Expression.py create mode 100644 Programs/9_DynamicProgramming/38_Edit_Distance.py create mode 100644 Programs/9_DynamicProgramming/39_Best_Time_to_Buy_and_Sell_Stock_1_Transaction.py.py create mode 100644 Programs/9_DynamicProgramming/40_Best_Time_to_Buy_and_Sell_Stock II_Unlimited_Transaction.py create mode 100644 Programs/9_DynamicProgramming/41_Best_Time_to_Buy_and_Sell_Stock_III_2_Transactions.py create mode 100644 Programs/9_DynamicProgramming/42_Best_Time_to_Buy_and_Sell_Stock_IV_k_Transactions.py create mode 100644 Programs/9_DynamicProgramming/43_Best_Time_To_Buy_and_Sell_Stock_with_Cooldown.py create mode 100644 Programs/9_DynamicProgramming/44_Best_Time_to_Buy_and_Sell_Stock_with_Transaction_Fee.py create mode 100644 Programs/9_DynamicProgramming/45_Maximum Alternating Subsequence Sum.py create mode 100644 Programs/9_DynamicProgramming/46_Longest_Increasing_Subsequence.py create mode 100644 Programs/9_DynamicProgramming/47_Maximum_Length_of_Pair_Chain.py create mode 100644 Programs/9_DynamicProgramming/48_Longest_String_Chain.py create mode 100644 Programs/9_DynamicProgramming/49_Longest_Increasing_Path_in_a_Matrix.py diff --git a/Programs/9_DynamicProgramming/32_Length_of_Shortest_Common_Supersequence.py b/Programs/9_DynamicProgramming/32_Length_of_Shortest_Common_Supersequence.py new file mode 100644 index 0000000..ba8b2e4 --- /dev/null +++ b/Programs/9_DynamicProgramming/32_Length_of_Shortest_Common_Supersequence.py @@ -0,0 +1,118 @@ +# https://www.geeksforgeeks.org/problems/shortest-common-supersequence0322/1 , Medium + + +# Recursion +# T.C. - O(ind1*ind2*2^n) +# S.C - O(min(ind1,ind2)) + + +class Solution: + def solve(self, i1, i2, X, Y): + if i1 == 0 or i2 == 0: + return 0 + + match, notMatch = 0, 0 + + if X[i1 - 1] == Y[i2 - 1]: + match = 1 + self.solve(i1 - 1, i2 - 1, X, Y) + + else: + notMatch = max(self.solve(i1 - 1, i2, X, Y), self.solve(i1, i2 - 1, X, Y)) + + return max(match, notMatch) + + def shortestCommonSupersequence(self, X, Y, m, n): + lcs_length = self.solve(m, n, X, Y) + + return (m + n) - lcs_length + + +# Memoization +# T.C. - O(ind1*ind2*n) +# S.C - O(min(ind1,ind2))+O(ind1*ind2) + + +class Solution: + def solve(self, i1, i2, X, Y, dp): + if (i1, i2) in dp: + return dp[(i1, i2)] + + if i1 == 0 or i2 == 0: + return 0 + + match, notMatch = 0, 0 + + if X[i1 - 1] == Y[i2 - 1]: + match = 1 + self.solve(i1 - 1, i2 - 1, X, Y, dp) + + else: + notMatch = max( + self.solve(i1 - 1, i2, X, Y, dp), self.solve(i1, i2 - 1, X, Y, dp) + ) + + dp[(i1, i2)] = max(match, notMatch) + + return dp[(i1, i2)] + + def shortestCommonSupersequence(self, X, Y, m, n): + lcs_length = self.solve(m, n, X, Y, {}) + + return (m + n) - lcs_length + + +# Tabulation +# T.C. - O(ind1*ind2) +# S.C - O(ind1*ind2) + + +class Solution: + def shortestCommonSupersequence(self, X, Y, m, n): + dp = [[0 for _ in range(n + 1)] for _ in range(m + 1)] + + for i1 in range(m + 1): + for i2 in range(n + 1): + if i1 == 0 or i2 == 0: + dp[i1][i2] = 0 + else: + match, notMatch = 0, 0 + + if X[i1 - 1] == Y[i2 - 1]: + match = 1 + dp[i1 - 1][i2 - 1] + else: + notMatch = max(dp[i1 - 1][i2], dp[i1][i2 - 1]) + + dp[i1][i2] = max(match, notMatch) + + lcs_length = dp[m][n] + + return (m + n) - lcs_length + + +# Space Optimized +# T.C. - O(ind1*ind2) +# S.C - O(ind2) + + +class Solution: + def shortestCommonSupersequence(self, X, Y, m, n): + dp = [0 for _ in range(n + 1)] + + for i1 in range(m + 1): + tmp = [0 for _ in range(n + 1)] + for i2 in range(n + 1): + if i1 == 0 or i2 == 0: + tmp[i2] = 0 + else: + match, notMatch = 0, 0 + + if X[i1 - 1] == Y[i2 - 1]: + match = 1 + dp[i2 - 1] + else: + notMatch = max(dp[i2], tmp[i2 - 1]) + + tmp[i2] = max(match, notMatch) + dp = tmp + + lcs_length = dp[n] + + return (m + n) - lcs_length diff --git a/Programs/9_DynamicProgramming/33_Print_Shortest_Common_Supersequence.py b/Programs/9_DynamicProgramming/33_Print_Shortest_Common_Supersequence.py new file mode 100644 index 0000000..bcee8cc --- /dev/null +++ b/Programs/9_DynamicProgramming/33_Print_Shortest_Common_Supersequence.py @@ -0,0 +1,49 @@ +# https://leetcode.com/problems/shortest-common-supersequence/ , Hard + + +# Tabulation +# T.C. - O(m*m)+O(n*m) +# S.C - O(n*m)+O(k) + + +class Solution: + def shortestCommonSupersequence(self, str1: str, str2: str) -> str: + dp = [[0 for _ in range(len(str2) + 1)] for _ in range(len(str1) + 1)] + + for i1 in range(len(str1) + 1): + for i2 in range(len(str2) + 1): + if i1 == 0 or i2 == 0: + dp[i1][i2] = 0 + else: + match, notMatch = 0, 0 + if str1[i1 - 1] == str2[i2 - 1]: + match = 1 + dp[i1 - 1][i2 - 1] + else: + notMatch = max(dp[i1 - 1][i2], dp[i1][i2 - 1]) + + dp[i1][i2] = max(match, notMatch) + + i1, i2 = len(str1), len(str2) + st = "" + + while i1 > 0 and i2 > 0: + if str1[i1 - 1] == str2[i2 - 1]: + st += str1[i1 - 1] + i1 -= 1 + i2 -= 1 + elif dp[i1 - 1][i2] > dp[i1][i2 - 1]: + st += str1[i1 - 1] + i1 -= 1 + else: + st += str2[i2 - 1] + i2 -= 1 + + while i1 > 0: + st += str1[i1 - 1] + i1 -= 1 + + while i2 > 0: + st += str2[i2 - 1] + i2 -= 1 + + return st[::-1] diff --git a/Programs/9_DynamicProgramming/34_Longest_Palindromic_Substring.py b/Programs/9_DynamicProgramming/34_Longest_Palindromic_Substring.py new file mode 100644 index 0000000..13fde5f --- /dev/null +++ b/Programs/9_DynamicProgramming/34_Longest_Palindromic_Substring.py @@ -0,0 +1,33 @@ +# https://leetcode.com/problems/longest-palindromic-substring/ , Medium + + +# Tabulation +# T.C. - O(n^2) +# S.C - O(n^2) + + +class Solution: + def longestPalindrome(self, s: str) -> str: + dp = [[0 for _ in range(len(s))] for _ in range(len(s))] + + res = "" + maxLen = 0 + + for diff in range(len(s)): + for i in range(len(s)): + j = i + diff + if j < len(s): + if i == j: + dp[i][j] = 1 + elif diff == 1: + dp[i][j] = 2 if s[i] == s[j] else 0 + else: + if s[i] == s[j] and dp[i + 1][j - 1]: + dp[i][j] = dp[i + 1][j - 1] + 2 + + if dp[i][j]: + if dp[i][j] > maxLen: + maxLen = dp[i][j] + res = s[i : i + diff + 1] + + return res diff --git a/Programs/9_DynamicProgramming/35_Distinct_Subsequences.py b/Programs/9_DynamicProgramming/35_Distinct_Subsequences.py new file mode 100644 index 0000000..eac45af --- /dev/null +++ b/Programs/9_DynamicProgramming/35_Distinct_Subsequences.py @@ -0,0 +1,119 @@ +# https://leetcode.com/problems/distinct-subsequences/ , Hard + + +# Recursion +# T.C. - O(2^(n*m)) +# S.C - O(n+m) + + +class Solution: + def solve(self, i: int, j: int, s: str, t: str) -> int: + # Base Case + if j == 0: + return 1 + + if i == 0: + return 0 + + # Try out all ways + match, notMatch = 0, 0 + + # Match + if s[i - 1] == t[j - 1]: + match = self.solve(i - 1, j - 1, s, t) + self.solve(i - 1, j, s, t) + return match + + else: + # notMatch + notMatch = self.solve(i - 1, j, s, t) + return notMatch + + def numDistinct(self, s: str, t: str) -> int: + return self.solve(len(s), len(t), s, t) + + +# Memoization +# T.C. - O(N*M) +# S.C - O(n+m)+O(n*m) + + +class Solution: + def solve(self, i: int, j: int, s: str, t: str, dp: dict) -> int: + if (i, j) in dp: + return dp[(i, j)] + + # Base Case + if j == 0: + return 1 + + if i == 0: + return 0 + + # Try out all ways + match, notMatch = 0, 0 + + # Match + if s[i - 1] == t[j - 1]: + match = self.solve(i - 1, j - 1, s, t, dp) + self.solve(i - 1, j, s, t, dp) + dp[(i, j)] = match + return dp[(i, j)] + + else: + # notMatch + notMatch = self.solve(i - 1, j, s, t, dp) + dp[(i, j)] = notMatch + return dp[(i, j)] + + def numDistinct(self, s: str, t: str) -> int: + return self.solve(len(s), len(t), s, t, {}) + + +# Tabulation +# T.C. - O(N*M) +# S.C - O(n*m) + + +class Solution: + def numDistinct(self, s: str, t: str) -> int: + dp = [[0 for _ in range(len(t) + 1)] for _ in range(len(s) + 1)] + + for i in range(len(s) + 1): + for j in range(len(t) + 1): + if j == 0: + dp[i][j] = 1 + elif i == 0: + dp[i][j] = 0 + else: + if s[i - 1] == t[j - 1]: + dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j] + else: + dp[i][j] = dp[i - 1][j] + + return dp[len(s)][len(t)] + + +# Space Optimized +# T.C. - O(N*M) +# S.C - O(m)+O(n) + + +class Solution: + def numDistinct(self, s: str, t: str) -> int: + dp = [0 for _ in range(len(t) + 1)] + + for i in range(len(s) + 1): + tmp = [0 for _ in range(len(t) + 1)] + for j in range(len(t) + 1): + if j == 0: + tmp[j] = 1 + elif i == 0: + tmp[j] = 0 + else: + if s[i - 1] == t[j - 1]: + tmp[j] = dp[j - 1] + dp[j] + else: + tmp[j] = dp[j] + + dp = tmp + + return dp[len(t)] diff --git a/Programs/9_DynamicProgramming/36_Wildcard_Matching.py b/Programs/9_DynamicProgramming/36_Wildcard_Matching.py new file mode 100644 index 0000000..f15bce7 --- /dev/null +++ b/Programs/9_DynamicProgramming/36_Wildcard_Matching.py @@ -0,0 +1,115 @@ +# https://leetcode.com/problems/wildcard-matching/ , Hard + +# Recursion +# T.C. - 2^O(N*M) +# S.C - O(N*M)+O(N+M) + + +class Solution: + def solve(self, i, j, s, p): + # Base Case + if i == 0 and j == 0: + return True + + elif i > 0 and j == 0: + return False + + elif i == 0 and j > 0: + for i in range(j): + if p[i] != "*": + return False + return True + + # Try out all possible ways + if s[i - 1] == p[j - 1] or p[j - 1] == "?": + return self.solve(i - 1, j - 1, s, p) + + elif p[j - 1] == "*": + return self.solve(i, j - 1, s, p) or self.solve(i - 1, j, s, p) + + else: + return False + + def isMatch(self, s: str, p: str) -> bool: + return self.solve(len(s), len(p), s, p) + + +# Memoization +# T.C. - O(N*M) +# S.C - O(N*M)+O(N+M) + + +class Solution: + def solve(self, i, j, s, p, dp): + if (i, j) in dp: + return dp[(i, j)] + # Base Case + if i == 0 and j == 0: + return True + + elif i > 0 and j == 0: + return False + + elif i == 0 and j > 0: + for i in range(j): + if p[i] != "*": + return False + return True + + # Try out all possible ways + if s[i - 1] == p[j - 1] or p[j - 1] == "?": + dp[(i, j)] = self.solve(i - 1, j - 1, s, p, dp) + return dp[(i, j)] + + elif p[j - 1] == "*": + dp[(i, j)] = self.solve(i, j - 1, s, p, dp) or self.solve( + i - 1, j, s, p, dp + ) + return dp[(i, j)] + + else: + dp[(i, j)] = False + return dp[(i, j)] + + def isMatch(self, s: str, p: str) -> bool: + return self.solve(len(s), len(p), s, p, {}) + + +# Tabulation +# T.C. - O(N*M) +# S.C - O(N*M) + + +class Solution: + def isMatch(self, s: str, p: str) -> bool: + dp = [[False for _ in range(len(p) + 1)] for _ in range(len(s) + 1)] + + dp[0][0] = True + + # Fill first column with zero + for row in range(len(s) + 1): + dp[row][0] = False + + # Fill first row with zero + for col in range(len(p) + 1): + flag = False + for k in range(col): + if p[k] != "*": + flag = not flag + dp[0][col] = False + break + if not flag: + dp[0][col] = True + + for i in range(1, len(s) + 1): + for j in range(1, len(p) + 1): + if s[i - 1] == p[j - 1] or p[j - 1] == "?": + dp[i][j] = dp[i - 1][j - 1] + + elif p[j - 1] == "*": + dp[i][j] = dp[i][j - 1] or dp[i - 1][j] + + else: + dp[i][j] = False + + return dp[len(s)][len(p)] diff --git a/Programs/9_DynamicProgramming/37_Regular_Expression.py b/Programs/9_DynamicProgramming/37_Regular_Expression.py new file mode 100644 index 0000000..75a7038 --- /dev/null +++ b/Programs/9_DynamicProgramming/37_Regular_Expression.py @@ -0,0 +1,17 @@ +# https://leetcode.com/problems/regular-expression-matching/ , Hard + +# Recursion +# T.C. - +# S.C - + +# Memoization +# T.C. - +# S.C - + +# Tabulation +# T.C. - +# S.C - + +# Space Optimized +# T.C. - +# S.C - diff --git a/Programs/9_DynamicProgramming/38_Edit_Distance.py b/Programs/9_DynamicProgramming/38_Edit_Distance.py new file mode 100644 index 0000000..039b795 --- /dev/null +++ b/Programs/9_DynamicProgramming/38_Edit_Distance.py @@ -0,0 +1,17 @@ +# https://leetcode.com/problems/edit-distance/ , Medium + +# Recursion +# T.C. - +# S.C - + +# Memoization +# T.C. - +# S.C - + +# Tabulation +# T.C. - +# S.C - + +# Space Optimized +# T.C. - +# S.C - diff --git a/Programs/9_DynamicProgramming/39_Best_Time_to_Buy_and_Sell_Stock_1_Transaction.py.py b/Programs/9_DynamicProgramming/39_Best_Time_to_Buy_and_Sell_Stock_1_Transaction.py.py new file mode 100644 index 0000000..5e90f76 --- /dev/null +++ b/Programs/9_DynamicProgramming/39_Best_Time_to_Buy_and_Sell_Stock_1_Transaction.py.py @@ -0,0 +1,58 @@ +# https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ , Easy + + +# Optimal +# T.C. - O(n) +# S.C - O(1) + + +class Solution: + def maxProfit(self, prices: List[int]) -> int: + l, r = 0, 1 + mxProfit = 0 + + while r < len(prices): + # isProfitable + if prices[l] < prices[r]: + profit = prices[r] - prices[l] + mxProfit = max(mxProfit, profit) + else: + l = r + r += 1 + + return mxProfit + + +# Space Optimized +# T.C. - O(n*2*k) +# S.C - O(k*2)+O(k*2) +# k=1 + + +class Solution: + def maxProfit(self, prices: list[int]) -> int: + k = 1 + dp = [[num for num in range(k + 1)] for _ in range(0, 2)] + tmp = [[num for num in range(k + 1)] for _ in range(0, 2)] + + for idx in range(len(prices), -1, -1): + for canBuy in range(0, 2): + for k in range(k + 1): + if k == 0: + tmp[canBuy][k] = 0 + elif idx == len(prices): + tmp[canBuy][k] = 0 + else: + profit = 0 + if canBuy: + buy = -prices[idx] + dp[0][k] + notBuy = 0 + dp[1][k] + profit = max(buy, notBuy) + else: + sell = prices[idx] + dp[1][k - 1] + notSell = 0 + dp[0][k] + profit = max(sell, notSell) + tmp[canBuy][k] = profit + dp = tmp + + return dp[1][k] diff --git a/Programs/9_DynamicProgramming/40_Best_Time_to_Buy_and_Sell_Stock II_Unlimited_Transaction.py b/Programs/9_DynamicProgramming/40_Best_Time_to_Buy_and_Sell_Stock II_Unlimited_Transaction.py new file mode 100644 index 0000000..262ada3 --- /dev/null +++ b/Programs/9_DynamicProgramming/40_Best_Time_to_Buy_and_Sell_Stock II_Unlimited_Transaction.py @@ -0,0 +1,123 @@ +# https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/ , Medium + +# Recursion +# T.C. - O(2^n) +# S.C - O(n) + + +class Solution: + def solve(self, idx, canBuy, prices): + # Base Case + if idx == len(prices): + return 0 + + profit = 0 + + # All Ways + if canBuy: + buy = -prices[idx] + self.solve(idx + 1, 0, prices) + notBuy = self.solve(idx + 1, 1, prices) + profit = max(buy, notBuy) + else: + sell = self.solve(idx + 1, 1, prices) + prices[idx] + notSell = self.solve(idx + 1, 0, prices) + profit = max(sell, notSell) + + return profit + + def maxProfit(self, prices: list[int]) -> int: + return self.solve(0, 1, prices) + + +# Memoization +# T.C. - O(n) +# S.C - O(n)+O(n^2) + + +class Solution: + def solve(self, idx, canBuy, prices, dp): + # Base Case + if (idx, canBuy) in dp: + return dp[(idx, canBuy)] + + if idx == len(prices): + return 0 + + profit = 0 + + # All Ways + if canBuy: + buy = -prices[idx] + self.solve(idx + 1, 0, prices, dp) + notBuy = self.solve(idx + 1, 1, prices, dp) + profit = max(buy, notBuy) + else: + sell = self.solve(idx + 1, 1, prices, dp) + prices[idx] + notSell = self.solve(idx + 1, 0, prices, dp) + profit = max(sell, notSell) + + dp[(idx, canBuy)] = profit + + return dp[(idx, canBuy)] + + def maxProfit(self, prices: list[int]) -> int: + return self.solve(0, 1, prices, {}) + + +# Tabulation +# T.C. - O(n^2) +# S.C - O(n^2) + + +class Solution: + def maxProfit(self, prices: list[int]) -> int: + dp = [[j for j in range(2)] for _ in range(len(prices) + 1)] + + for idx in range(len(prices), -1, -1): + for canBuy in range(1, -1, -1): + if idx == len(prices): + dp[idx][canBuy] = 0 + else: + profit = 0 + if canBuy: + buy = -prices[idx] + dp[idx + 1][0] + notBuy = dp[idx + 1][1] + profit = max(buy, notBuy) + else: + sell = dp[idx + 1][1] + prices[idx] + notSell = dp[idx + 1][0] + profit = max(sell, notSell) + + dp[idx][canBuy] = profit + + return dp[0][1] + + +# Space Optimized +# T.C. - O(n^2) +# S.C - O(n) + + +class Solution: + def maxProfit(self, prices: list[int]) -> int: + dp = [j for j in range(2)] + + for idx in range(len(prices), -1, -1): + tmp = [j for j in range(2)] + for canBuy in range(1, -1, -1): + if idx == len(prices): + tmp[canBuy] = 0 + else: + profit = 0 + if canBuy: + buy = -prices[idx] + dp[0] + notBuy = dp[1] + profit = max(buy, notBuy) + else: + sell = dp[1] + prices[idx] + notSell = dp[0] + profit = max(sell, notSell) + + tmp[canBuy] = profit + dp = tmp + + return dp[1] diff --git a/Programs/9_DynamicProgramming/41_Best_Time_to_Buy_and_Sell_Stock_III_2_Transactions.py b/Programs/9_DynamicProgramming/41_Best_Time_to_Buy_and_Sell_Stock_III_2_Transactions.py new file mode 100644 index 0000000..4ee3cac --- /dev/null +++ b/Programs/9_DynamicProgramming/41_Best_Time_to_Buy_and_Sell_Stock_III_2_Transactions.py @@ -0,0 +1,138 @@ +# https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ , Hard + +# Recursion +# T.C. - O(2^n) +# S.C - O(n) + + +class Solution: + def solve(self, idx, canBuy, k, prices): + # Base Case + if k == 0: + return 0 + + if idx == len(prices): + return 0 + + # All ways + profit = 0 + + if canBuy: + buy = -prices[idx] + self.solve(idx + 1, 0, k, prices) + notBuy = 0 + self.solve(idx + 1, 1, k, prices) + profit = max(buy, notBuy) + else: + sell = prices[idx] + self.solve(idx + 1, 1, k - 1, prices) + notSell = 0 + self.solve(idx + 1, 0, k, prices) + profit = max(sell, notSell) + + return profit + + def maxProfit(self, prices: list[int]) -> int: + return self.solve(0, 1, 2, prices) + + +# Memoization +# T.C. - O(n) +# S.C - O(n)+O(n^2) + + +class Solution: + def solve(self, idx, canBuy, k, prices, dp): + # Base Case + if (idx, canBuy, k) in dp: + return dp[(idx, canBuy, k)] + + if k == 0: + return 0 + + if idx == len(prices): + return 0 + + # All ways + profit = 0 + + if canBuy: + buy = -prices[idx] + self.solve(idx + 1, 0, k, prices, dp) + notBuy = 0 + self.solve(idx + 1, 1, k, prices, dp) + profit = max(buy, notBuy) + else: + sell = prices[idx] + self.solve(idx + 1, 1, k - 1, prices, dp) + notSell = 0 + self.solve(idx + 1, 0, k, prices, dp) + profit = max(sell, notSell) + + dp[(idx, canBuy, k)] = profit + + return dp[(idx, canBuy, k)] + + def maxProfit(self, prices: list[int]) -> int: + return self.solve(0, 1, 2, prices, {}) + + +# Tabulation +# T.C. - O(n*2*2) +# S.C - O(n*2*2) + + +class Solution: + def maxProfit(self, prices: list[int]) -> int: + k = 2 + dp = [ + [[num for num in range(k + 1)] for _ in range(0, 2)] + for _ in range(len(prices) + 1) + ] + + for idx in range(len(prices), -1, -1): + for canBuy in range(0, 2): + for k in range(k + 1): + if k == 0: + dp[idx][canBuy][k] = 0 + elif idx == len(prices): + dp[idx][canBuy][k] = 0 + else: + profit = 0 + if canBuy: + buy = -prices[idx] + dp[idx + 1][0][k] + notBuy = 0 + dp[idx + 1][1][k] + profit = max(buy, notBuy) + else: + sell = prices[idx] + dp[idx + 1][1][k - 1] + notSell = 0 + dp[idx + 1][0][k] + profit = max(sell, notSell) + dp[idx][canBuy][k] = profit + + return dp[0][1][2] + + +# Space Optimized +# T.C. - O(n*2*2) +# S.C - O(k*2)+O(k*2) + + +class Solution: + def maxProfit(self, prices: list[int]) -> int: + k = 2 + dp = [[num for num in range(k + 1)] for _ in range(0, 2)] + tmp = [[num for num in range(k + 1)] for _ in range(0, 2)] + + for idx in range(len(prices), -1, -1): + for canBuy in range(0, 2): + for k in range(k + 1): + if k == 0: + tmp[canBuy][k] = 0 + elif idx == len(prices): + tmp[canBuy][k] = 0 + else: + profit = 0 + if canBuy: + buy = -prices[idx] + dp[0][k] + notBuy = 0 + dp[1][k] + profit = max(buy, notBuy) + else: + sell = prices[idx] + dp[1][k - 1] + notSell = 0 + dp[0][k] + profit = max(sell, notSell) + tmp[canBuy][k] = profit + dp = tmp + + return dp[1][2] diff --git a/Programs/9_DynamicProgramming/42_Best_Time_to_Buy_and_Sell_Stock_IV_k_Transactions.py b/Programs/9_DynamicProgramming/42_Best_Time_to_Buy_and_Sell_Stock_IV_k_Transactions.py new file mode 100644 index 0000000..8e88caf --- /dev/null +++ b/Programs/9_DynamicProgramming/42_Best_Time_to_Buy_and_Sell_Stock_IV_k_Transactions.py @@ -0,0 +1,34 @@ +# https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ , Hard + + +# Space Optimized +# T.C. - O(n*2*k) +# S.C - O(k*2)+O(k*2) + + +class Solution: + def maxProfit(self, k: int, prices: list[int]) -> int: + dp = [[num for num in range(k + 1)] for _ in range(0, 2)] + tmp = [[num for num in range(k + 1)] for _ in range(0, 2)] + + for idx in range(len(prices), -1, -1): + for canBuy in range(0, 2): + for k in range(k + 1): + if k == 0: + tmp[canBuy][k] = 0 + elif idx == len(prices): + tmp[canBuy][k] = 0 + else: + profit = 0 + if canBuy: + buy = -prices[idx] + dp[0][k] + notBuy = 0 + dp[1][k] + profit = max(buy, notBuy) + else: + sell = prices[idx] + dp[1][k - 1] + notSell = 0 + dp[0][k] + profit = max(sell, notSell) + tmp[canBuy][k] = profit + dp = tmp + + return dp[1][k] diff --git a/Programs/9_DynamicProgramming/43_Best_Time_To_Buy_and_Sell_Stock_with_Cooldown.py b/Programs/9_DynamicProgramming/43_Best_Time_To_Buy_and_Sell_Stock_with_Cooldown.py new file mode 100644 index 0000000..026f379 --- /dev/null +++ b/Programs/9_DynamicProgramming/43_Best_Time_To_Buy_and_Sell_Stock_with_Cooldown.py @@ -0,0 +1,94 @@ +# https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/ , Mediun + +# Recursion +# T.C. - O(2^n) +# S.C - O(n) + + +class Solution: + def solve(self, idx, canBuy, prices): + # Base Case + if idx >= len(prices): + return 0 + + profit = 0 + + # All Ways + if canBuy: + buy = -prices[idx] + self.solve(idx + 1, 0, prices) + notBuy = self.solve(idx + 1, 1, prices) + profit = max(buy, notBuy) + else: + sell = self.solve(idx + 2, 1, prices) + prices[idx] + notSell = self.solve(idx + 1, 0, prices) + profit = max(sell, notSell) + + return profit + + def maxProfit(self, prices: list[int]) -> int: + return self.solve(0, 1, prices) + + +# Memoization +# T.C. - O(n) +# S.C - O(n)+O(n^2) + + +class Solution: + def solve(self, idx, canBuy, prices, dp): + # Base Case + if (idx, canBuy) in dp: + return dp[(idx, canBuy)] + + if idx >= len(prices): + return 0 + + profit = 0 + + # All Ways + if canBuy: + buy = -prices[idx] + self.solve(idx + 1, 0, prices, dp) + notBuy = self.solve(idx + 1, 1, prices, dp) + profit = max(buy, notBuy) + else: + sell = self.solve(idx + 2, 1, prices, dp) + prices[idx] + notSell = self.solve(idx + 1, 0, prices, dp) + profit = max(sell, notSell) + + dp[(idx, canBuy)] = profit + + return dp[(idx, canBuy)] + + def maxProfit(self, prices: list[int]) -> int: + return self.solve(0, 1, prices, {}) + + +# Tabulation +# T.C. - O(n*2) +# S.C - O(n^2) + + +class Solution: + def maxProfit(self, prices: list[int]) -> int: + dp = [[j for j in range(2)] for _ in range(len(prices) + 1)] + + for idx in range(len(prices), -1, -1): + for canBuy in range(2): + if idx == len(prices): + dp[idx][canBuy] = 0 + else: + profit = 0 + if canBuy: + buy = -prices[idx] + dp[idx + 1][0] + notBuy = dp[idx + 1][1] + profit = max(buy, notBuy) + else: + sell = ( + 0 if idx + 2 >= len(prices) else (dp[idx + 2][1]) + ) + prices[idx] + notSell = dp[idx + 1][0] + profit = max(sell, notSell) + + dp[idx][canBuy] = profit + + return dp[0][1] diff --git a/Programs/9_DynamicProgramming/44_Best_Time_to_Buy_and_Sell_Stock_with_Transaction_Fee.py b/Programs/9_DynamicProgramming/44_Best_Time_to_Buy_and_Sell_Stock_with_Transaction_Fee.py new file mode 100644 index 0000000..a9d0c9f --- /dev/null +++ b/Programs/9_DynamicProgramming/44_Best_Time_to_Buy_and_Sell_Stock_with_Transaction_Fee.py @@ -0,0 +1,31 @@ +# https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/ , Medium + +# Space Optimized +# T.C. - O(n*2) +# S.C - O(2) + + +class Solution: + def maxProfit(self, prices: list[int], fee: int) -> int: + dp = [j for j in range(2)] + + for idx in range(len(prices), -1, -1): + tmp = [j for j in range(2)] + for canBuy in range(1, -1, -1): + if idx == len(prices): + tmp[canBuy] = 0 + else: + profit = 0 + if canBuy: + buy = -prices[idx] + dp[0] + notBuy = dp[1] + profit = max(buy, notBuy) + else: + sell = dp[1] + prices[idx] - fee + notSell = dp[0] + profit = max(sell, notSell) + + tmp[canBuy] = profit + dp = tmp + + return dp[1] diff --git a/Programs/9_DynamicProgramming/45_Maximum Alternating Subsequence Sum.py b/Programs/9_DynamicProgramming/45_Maximum Alternating Subsequence Sum.py new file mode 100644 index 0000000..d9bcc09 --- /dev/null +++ b/Programs/9_DynamicProgramming/45_Maximum Alternating Subsequence Sum.py @@ -0,0 +1,113 @@ +# https://leetcode.com/problems/maximum-alternating-subsequence-sum/ , Medium + +# Recursion +# T.C. - O(2^n) +# S.C - O(n) + + +class Solution: + def solve(self, idx, flag, nums): + if idx == len(nums): + return 0 + + # Take + take = (nums[idx] * flag) + self.solve(idx + 1, flag * -1, nums) + + # Not Take + notTake = 0 + self.solve(idx + 1, flag, nums) + + return max(take, notTake) + + def maxAlternatingSum(self, nums: list[int]) -> int: + return self.solve(0, 1, nums) + + +# Memoization +# T.C. - O(n) +# S.C - O(n)+O(n*2) + + +class Solution: + def solve(self, idx, flag, nums, dp): + if (idx, flag) in dp: + return dp[(idx, flag)] + if idx == len(nums): + return 0 + + # Take + take = (nums[idx] * flag) + self.solve(idx + 1, flag * -1, nums, dp) + + # Not Take + notTake = 0 + self.solve(idx + 1, flag, nums, dp) + + dp[(idx, flag)] = max(take, notTake) + + return dp[(idx, flag)] + + def maxAlternatingSum(self, nums: list[int]) -> int: + return self.solve(0, 1, nums, {}) + + +# Tabulation +# T.C. - O(n) +# S.C - O(n*2) + + +class Solution: + def maxAlternatingSum(self, nums: list[int]) -> int: + dp = {} + + for i in range(len(nums) + 1): + for j in range(-1, 2): + if j == 0: + continue + else: + dp[(i, j)] = 0 + + for idx in range(len(nums), -1, -1): + for flag in range(-1, 2): + if flag == 0: + continue + elif idx == len(nums): + dp[(idx, flag)] = 0 + else: + take = (nums[idx] * flag) + dp[(idx + 1, flag * -1)] + notTake = 0 + dp[(idx + 1, flag)] + + dp[(idx, flag)] = max(take, notTake) + + return dp[(0, 1)] + + +# Space Optimized +# T.C. - O(n) +# S.C - O(2) + + +class Solution: + def maxAlternatingSum(self, nums: list[int]) -> int: + dp = {} + curr = {} + + for j in range(-1, 2): + if j == 0: + continue + else: + dp[j] = 0 + curr[j] = 0 + + for idx in range(len(nums), -1, -1): + for flag in range(-1, 2): + if flag == 0: + continue + elif idx == len(nums): + curr[flag] = 0 + else: + take = (nums[idx] * flag) + dp[flag * -1] + notTake = 0 + dp[flag] + + curr[flag] = max(take, notTake) + + dp = curr + + return dp[1] diff --git a/Programs/9_DynamicProgramming/46_Longest_Increasing_Subsequence.py b/Programs/9_DynamicProgramming/46_Longest_Increasing_Subsequence.py new file mode 100644 index 0000000..9a24ba3 --- /dev/null +++ b/Programs/9_DynamicProgramming/46_Longest_Increasing_Subsequence.py @@ -0,0 +1,102 @@ +# https://leetcode.com/problems/longest-increasing-subsequence/ , Medium + +# Recursion +# T.C. - O(2^n) +# S.C - O(n) + + +class Solution: + def solve(self, currIdx, prevIdx, nums): + if currIdx == len(nums): + return 0 + + take = 0 + + if prevIdx == -1 or nums[currIdx] > nums[prevIdx]: + take = 1 + self.solve(currIdx + 1, currIdx, nums) + notTake = 0 + self.solve(currIdx + 1, prevIdx, nums) + + return max(take, notTake) + + def lengthOfLIS(self, nums: List[int]) -> int: + return self.solve(0, -1, nums) + + +# Memoization +# T.C. - O(n) +# S.C - O(n)+O(n^2) + + +class Solution: + def solve(self, currIdx, prevIdx, nums, dp): + if currIdx == len(nums): + return 0 + + if dp[currIdx][prevIdx + 1] != -1: + return dp[currIdx][prevIdx + 1] + + take = 0 + + if prevIdx == -1 or nums[currIdx] > nums[prevIdx]: + take = 1 + self.solve(currIdx + 1, currIdx, nums, dp) + notTake = 0 + self.solve(currIdx + 1, prevIdx, nums, dp) + + dp[currIdx][prevIdx + 1] = max(take, notTake) + + return dp[currIdx][prevIdx + 1] + + def lengthOfLIS(self, nums: List[int]) -> int: + dp = [[-1 for _ in range(len(nums) + 1)] for _ in range(len(nums) + 1)] + return self.solve(0, -1, nums, dp) + + +# Tabulation +# T.C. - O(n^2) +# S.C - O(n^2) + + +class Solution: + def lengthOfLIS(self, nums: List[int]) -> int: + dp = [[0 for _ in range(len(nums) + 1)] for _ in range(len(nums) + 1)] + + for currIdx in range(len(nums), -1, -1): + for prevIdx in range(currIdx - 1, -2, -1): + if currIdx == len(nums): + dp[currIdx][prevIdx + 1] = 0 + else: + take = 0 + + if prevIdx == -1 or nums[currIdx] > nums[prevIdx]: + take = 1 + dp[currIdx + 1][currIdx + 1] + notTake = 0 + dp[currIdx + 1][prevIdx + 1] + + dp[currIdx][prevIdx + 1] = max(take, notTake) + + return dp[0][-1 + 1] + + +# Space Optimized +# T.C. - O(n^2) +# S.C - O(n) + + +class Solution: + def lengthOfLIS(self, nums: List[int]) -> int: + dp = [0 for _ in range(len(nums) + 1)] + tmp = [0 for _ in range(len(nums) + 1)] + + for currIdx in range(len(nums), -1, -1): + for prevIdx in range(currIdx - 1, -2, -1): + if currIdx == len(nums): + tmp[prevIdx + 1] = 0 + else: + take = 0 + + if prevIdx == -1 or nums[currIdx] > nums[prevIdx]: + take = 1 + dp[currIdx + 1] + notTake = 0 + dp[prevIdx + 1] + + tmp[prevIdx + 1] = max(take, notTake) + dp = tmp + + return dp[-1 + 1] diff --git a/Programs/9_DynamicProgramming/47_Maximum_Length_of_Pair_Chain.py b/Programs/9_DynamicProgramming/47_Maximum_Length_of_Pair_Chain.py new file mode 100644 index 0000000..df70646 --- /dev/null +++ b/Programs/9_DynamicProgramming/47_Maximum_Length_of_Pair_Chain.py @@ -0,0 +1,62 @@ +# https://leetcode.com/problems/maximum-length-of-pair-chain/ , Medium + +# Memoization +# T.C. - O(n)+O(nlog(n)) +# S.C - O(n^2)+O(n) + + +class Solution: + def solve(self, currIdx, prevIdx, pairs, dp): + if currIdx == len(pairs): + return 0 + + if dp[currIdx][prevIdx + 1] != -1: + return dp[currIdx][prevIdx + 1] + + take = 0 + + pair2 = pairs[currIdx] + pair1 = pairs[prevIdx] + + if prevIdx == -1 or pair2[0] > pair1[1]: + take = 1 + self.solve(currIdx + 1, currIdx, pairs, dp) + notTake = 0 + self.solve(currIdx + 1, prevIdx, pairs, dp) + + dp[currIdx][prevIdx + 1] = max(take, notTake) + + return dp[currIdx][prevIdx + 1] + + def findLongestChain(self, pairs: List[List[int]]) -> int: + pairs.sort() + dp = [[-1 for _ in range(len(pairs) + 1)] for _ in range(len(pairs) + 1)] + return self.solve(0, -1, pairs, dp) + + +# Space Optimized +# T.C. - O(n^2)+O(nlog(n)) +# S.C - O(n)+O(n) + + +class Solution: + def findLongestChain(self, pairs: List[List[int]]) -> int: + dp = [0 for _ in range(len(pairs) + 1)] + tmp = [0 for _ in range(len(pairs) + 1)] + + for currIdx in range(len(pairs), -1, -1): + for prevIdx in range(currIdx - 1, -2, -1): + if currIdx == len(pairs): + tmp[prevIdx + 1] = 0 + else: + pair2 = pairs[currIdx] + pair1 = pairs[prevIdx] + + take = 0 + + if prevIdx == -1 or pair2[0] > pair1[1]: + take = 1 + dp[currIdx + 1] + notTake = 0 + dp[prevIdx + 1] + + tmp[prevIdx + 1] = max(take, notTake) + dp = tmp + + return dp[-1 + 1] diff --git a/Programs/9_DynamicProgramming/48_Longest_String_Chain.py b/Programs/9_DynamicProgramming/48_Longest_String_Chain.py new file mode 100644 index 0000000..3446d8c --- /dev/null +++ b/Programs/9_DynamicProgramming/48_Longest_String_Chain.py @@ -0,0 +1,41 @@ +# https://leetcode.com/problems/longest-string-chain/ , Medium + + +# Memoization +# T.C. - O(nlog(n))+O(n*avg(words)) +# S.C - O(n^2)+O(n) + + +class Solution: + def check_predecessor(self, w1, w2): + if len(w2) != len(w1) + 1: + return False + i, j = 0, 0 + while j < len(w2): + if i < len(w1) and w1[i] == w2[j]: + i += 1 + j += 1 + else: + j += 1 + return i == len(w1) and j == len(w2) + + def solve(self, currIdx, prevIdx, words, dp): + if currIdx == len(words): + return 0 + + if dp[currIdx][prevIdx + 1] != -1: + return dp[currIdx][prevIdx + 1] + + take = 0 + if prevIdx == -1 or self.check_predecessor(words[prevIdx], words[currIdx]): + take = 1 + self.solve(currIdx + 1, currIdx, words, dp) + notTake = 0 + self.solve(currIdx + 1, prevIdx, words, dp) + + dp[currIdx][prevIdx + 1] = max(take, notTake) + + return dp[currIdx][prevIdx + 1] + + def longestStrChain(self, words: list[str]) -> int: + words.sort(key=len) + dp = [[-1 for _ in range(len(words) + 1)] for _ in range(len(words) + 1)] + return self.solve(0, -1, words, dp) diff --git a/Programs/9_DynamicProgramming/49_Longest_Increasing_Path_in_a_Matrix.py b/Programs/9_DynamicProgramming/49_Longest_Increasing_Path_in_a_Matrix.py new file mode 100644 index 0000000..c3638c6 --- /dev/null +++ b/Programs/9_DynamicProgramming/49_Longest_Increasing_Path_in_a_Matrix.py @@ -0,0 +1,44 @@ +# https://leetcode.com/problems/longest-increasing-path-in-a-matrix/ , Hard + + +# Memoization +# T.C. - O(4^n) +# S.C - O(n)+O(n^3) + + +class Solution: + def solve(self, i, j, matrix, preVal, dp): + if (i, j, preVal) in dp: + return dp[(i, j, preVal)] + + if i < 0 or j < 0 or i >= len(matrix) or j >= len(matrix[0]): + return 0 + + if matrix[i][j] <= preVal: + return 0 + + t, d, l, r = 0, 0, 0, 0 + + # Top + t = 1 + self.solve(i - 1, j, matrix, matrix[i][j], dp) + + # Down + d = 1 + self.solve(i + 1, j, matrix, matrix[i][j], dp) + + # Left + l = 1 + self.solve(i, j - 1, matrix, matrix[i][j], dp) + + # Right + r = 1 + self.solve(i, j + 1, matrix, matrix[i][j], dp) + + dp[(i, j, preVal)] = max(t, d, l, r) + + return dp[(i, j, preVal)] + + def longestIncreasingPath(self, matrix: list[list[int]]) -> int: + mx = 0 + for row in range(len(matrix)): + for col in range(len(matrix[0])): + getVal = self.solve(row, col, matrix, -1, {}) + mx = max(mx, getVal) + return mx diff --git a/README.md b/README.md index 5da072c..ea61ffa 100644 --- a/README.md +++ b/README.md @@ -374,4 +374,22 @@ | 361 | DynamicProgramming | Maximum Length of Repeated Subarray | https://leetcode.com/problems/maximum-length-of-repeated-subarray/ | Medium | https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/28_Maximum_Length_of_Repeated_Subarray.py | | 362 | DynamicProgramming | Longest Palindromic Subsequence | https://leetcode.com/problems/longest-palindromic-subsequence/ | Medium | https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/29_Longest_Palindromic_Subsequence.py | | 363 | DynamicProgramming | Minimum Insertions to make string palindrome | https://leetcode.com/problems/minimum-insertion-steps-to-make-a-string-palindrome/ | Hard | https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/30_Minimum_Insertions_to_make_string_palindrome.py | -| 364 | DynamicProgramming | Minimun Insertions Deletions to Convert String A to String B | https://www.geeksforgeeks.org/problems/minimum-number-of-deletions-and-insertions0209/1 | Medium | https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/31_Minimun_Insertions_Deletions_to_Convert_String_A_to_String_B.py | \ No newline at end of file +| 364 | DynamicProgramming | Minimun Insertions Deletions to Convert String A to String B | https://www.geeksforgeeks.org/problems/minimum-number-of-deletions-and-insertions0209/1 | Medium | https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/31_Minimun_Insertions_Deletions_to_Convert_String_A_to_String_B.py | +| 365 | DynamicProgramming | Length of Shortest Common Supersequence | https://www.geeksforgeeks.org/problems/shortest-common-supersequence0322/1 | Medium | https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/32_Length_of_Shortest_Common_Supersequence.py | +| 366 | DynamicProgramming | Print Shortest Common Supersequence | https://leetcode.com/problems/shortest-common-supersequence/ | Hard | https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/33_Print_Shortest_Common_Supersequence.py | +| 367 | DynamicProgramming | Longest Palindromic Substring | https://leetcode.com/problems/longest-palindromic-substring/ | Medium | https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/34_Longest_Palindromic_Substring.py | +| 368 | DynamicProgramming | Distinct Subsequences | https://leetcode.com/problems/distinct-subsequences/ | Hard | https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/35_Distinct_Subsequences.py | +| 369 | DynamicProgramming | Wildcard Matching | https://leetcode.com/problems/wildcard-matching/ | Hard | https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/36_Wildcard_Matching.py | +| 370 | DynamicProgramming | Regular Expression | https://leetcode.com/problems/regular-expression-matching/ | Hard | https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/37_Regular_Expression.py | +| 371 | DynamicProgramming | Edit Distance | https://leetcode.com/problems/edit-distance/ | Medium | https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/38_Edit_Distance.py | +| 372 | DynamicProgramming | Best Time to Buy and Sell Stock 1 Transaction | https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ | Easy | https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/39_Best_Time_to_Buy_and_Sell_Stock_1_Transaction.py.py | +| 373 | DynamicProgramming | Best Time to Buy and Sell Stock II Unlimited Transaction | https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/ | Medium | https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/40_Best_Time_to_Buy_and_Sell_Stock II_Unlimited_Transaction.py | +| 374 | DynamicProgramming | Best Time to Buy and Sell Stock III 2 Transactions | https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ | Hard | https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/41_Best_Time_to_Buy_and_Sell_Stock_III_2_Transactions.py | +| 375 | DynamicProgramming | Best Time to Buy and Sell Stock IV k Transactions | https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ | Hard | https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/42_Best_Time_to_Buy_and_Sell_Stock_IV_k_Transactions.py | +| 376 | DynamicProgramming | Best Time To Buy and Sell Stock with Cooldown | https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/ | Mediun | https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/43_Best_Time_To_Buy_and_Sell_Stock_with_Cooldown.py | +| 377 | DynamicProgramming | Best Time to Buy and Sell Stock with Transaction Fee | https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/ | Medium | https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/44_Best_Time_to_Buy_and_Sell_Stock_with_Transaction_Fee.py | +| 378 | DynamicProgramming | Maximum Alternating Subsequence Sum | https://leetcode.com/problems/maximum-alternating-subsequence-sum/ | Medium | https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/45_Maximum Alternating Subsequence Sum.py | +| 379 | DynamicProgramming | Longest Increasing Subsequence | https://leetcode.com/problems/longest-increasing-subsequence/ | Medium | https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/46_Longest_Increasing_Subsequence.py | +| 380 | DynamicProgramming | Maximum Length of Pair Chain | https://leetcode.com/problems/maximum-length-of-pair-chain/ | Medium | https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/47_Maximum_Length_of_Pair_Chain.py | +| 381 | DynamicProgramming | Longest String Chain | https://leetcode.com/problems/longest-string-chain/ | Medium | https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/48_Longest_String_Chain.py | +| 382 | DynamicProgramming | Longest Increasing Path in a Matrix | https://leetcode.com/problems/longest-increasing-path-in-a-matrix/ | Hard | https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/49_Longest_Increasing_Path_in_a_Matrix.py | \ No newline at end of file diff --git a/data.json b/data.json index 28eca35..c106c87 100644 --- a/data.json +++ b/data.json @@ -2935,6 +2935,150 @@ "https://www.geeksforgeeks.org/problems/minimum-number-of-deletions-and-insertions0209/1", " Medium", "https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/31_Minimun_Insertions_Deletions_to_Convert_String_A_to_String_B.py" + ], + [ + 32, + "DynamicProgramming", + "Length of Shortest Common Supersequence", + "https://www.geeksforgeeks.org/problems/shortest-common-supersequence0322/1", + " Medium", + "https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/32_Length_of_Shortest_Common_Supersequence.py" + ], + [ + 33, + "DynamicProgramming", + "Print Shortest Common Supersequence", + "https://leetcode.com/problems/shortest-common-supersequence/", + " Hard", + "https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/33_Print_Shortest_Common_Supersequence.py" + ], + [ + 34, + "DynamicProgramming", + "Longest Palindromic Substring", + "https://leetcode.com/problems/longest-palindromic-substring/", + " Medium", + "https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/34_Longest_Palindromic_Substring.py" + ], + [ + 35, + "DynamicProgramming", + "Distinct Subsequences", + "https://leetcode.com/problems/distinct-subsequences/", + " Hard", + "https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/35_Distinct_Subsequences.py" + ], + [ + 36, + "DynamicProgramming", + "Wildcard Matching", + "https://leetcode.com/problems/wildcard-matching/", + " Hard", + "https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/36_Wildcard_Matching.py" + ], + [ + 37, + "DynamicProgramming", + "Regular Expression", + "https://leetcode.com/problems/regular-expression-matching/", + " Hard", + "https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/37_Regular_Expression.py" + ], + [ + 38, + "DynamicProgramming", + "Edit Distance", + "https://leetcode.com/problems/edit-distance/", + " Medium", + "https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/38_Edit_Distance.py" + ], + [ + 39, + "DynamicProgramming", + "Best Time to Buy and Sell Stock 1 Transaction", + "https://leetcode.com/problems/best-time-to-buy-and-sell-stock/", + " Easy", + "https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/39_Best_Time_to_Buy_and_Sell_Stock_1_Transaction.py.py" + ], + [ + 40, + "DynamicProgramming", + "Best Time to Buy and Sell Stock II Unlimited Transaction", + "https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/", + " Medium", + "https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/40_Best_Time_to_Buy_and_Sell_Stock II_Unlimited_Transaction.py" + ], + [ + 41, + "DynamicProgramming", + "Best Time to Buy and Sell Stock III 2 Transactions", + "https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/", + " Hard", + "https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/41_Best_Time_to_Buy_and_Sell_Stock_III_2_Transactions.py" + ], + [ + 42, + "DynamicProgramming", + "Best Time to Buy and Sell Stock IV k Transactions", + "https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/", + " Hard", + "https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/42_Best_Time_to_Buy_and_Sell_Stock_IV_k_Transactions.py" + ], + [ + 43, + "DynamicProgramming", + "Best Time To Buy and Sell Stock with Cooldown", + "https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/", + " Mediun", + "https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/43_Best_Time_To_Buy_and_Sell_Stock_with_Cooldown.py" + ], + [ + 44, + "DynamicProgramming", + "Best Time to Buy and Sell Stock with Transaction Fee", + "https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/", + " Medium", + "https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/44_Best_Time_to_Buy_and_Sell_Stock_with_Transaction_Fee.py" + ], + [ + 45, + "DynamicProgramming", + "Maximum Alternating Subsequence Sum", + "https://leetcode.com/problems/maximum-alternating-subsequence-sum/", + " Medium", + "https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/45_Maximum Alternating Subsequence Sum.py" + ], + [ + 46, + "DynamicProgramming", + "Longest Increasing Subsequence", + "https://leetcode.com/problems/longest-increasing-subsequence/", + " Medium", + "https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/46_Longest_Increasing_Subsequence.py" + ], + [ + 47, + "DynamicProgramming", + "Maximum Length of Pair Chain", + "https://leetcode.com/problems/maximum-length-of-pair-chain/", + " Medium", + "https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/47_Maximum_Length_of_Pair_Chain.py" + ], + [ + 48, + "DynamicProgramming", + "Longest String Chain", + "https://leetcode.com/problems/longest-string-chain/", + " Medium", + "https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/48_Longest_String_Chain.py" + ], + [ + 49, + "DynamicProgramming", + "Longest Increasing Path in a Matrix", + "https://leetcode.com/problems/longest-increasing-path-in-a-matrix/", + " Hard", + "https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/49_Longest_Increasing_Path_in_a_Matrix.py" ] ] } \ No newline at end of file diff --git a/sheet.csv b/sheet.csv index bf6b0f1..54dd52b 100644 --- a/sheet.csv +++ b/sheet.csv @@ -364,3 +364,21 @@ 362,DynamicProgramming,Longest Palindromic Subsequence,https://leetcode.com/problems/longest-palindromic-subsequence/, Medium,https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/29_Longest_Palindromic_Subsequence.py 363,DynamicProgramming,Minimum Insertions to make string palindrome,https://leetcode.com/problems/minimum-insertion-steps-to-make-a-string-palindrome/, Hard,https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/30_Minimum_Insertions_to_make_string_palindrome.py 364,DynamicProgramming,Minimun Insertions Deletions to Convert String A to String B,https://www.geeksforgeeks.org/problems/minimum-number-of-deletions-and-insertions0209/1, Medium,https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/31_Minimun_Insertions_Deletions_to_Convert_String_A_to_String_B.py +365,DynamicProgramming,Length of Shortest Common Supersequence,https://www.geeksforgeeks.org/problems/shortest-common-supersequence0322/1, Medium,https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/32_Length_of_Shortest_Common_Supersequence.py +366,DynamicProgramming,Print Shortest Common Supersequence,https://leetcode.com/problems/shortest-common-supersequence/, Hard,https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/33_Print_Shortest_Common_Supersequence.py +367,DynamicProgramming,Longest Palindromic Substring,https://leetcode.com/problems/longest-palindromic-substring/, Medium,https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/34_Longest_Palindromic_Substring.py +368,DynamicProgramming,Distinct Subsequences,https://leetcode.com/problems/distinct-subsequences/, Hard,https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/35_Distinct_Subsequences.py +369,DynamicProgramming,Wildcard Matching,https://leetcode.com/problems/wildcard-matching/, Hard,https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/36_Wildcard_Matching.py +370,DynamicProgramming,Regular Expression,https://leetcode.com/problems/regular-expression-matching/, Hard,https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/37_Regular_Expression.py +371,DynamicProgramming,Edit Distance,https://leetcode.com/problems/edit-distance/, Medium,https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/38_Edit_Distance.py +372,DynamicProgramming,Best Time to Buy and Sell Stock 1 Transaction,https://leetcode.com/problems/best-time-to-buy-and-sell-stock/, Easy,https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/39_Best_Time_to_Buy_and_Sell_Stock_1_Transaction.py.py +373,DynamicProgramming,Best Time to Buy and Sell Stock II Unlimited Transaction,https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/, Medium,https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/40_Best_Time_to_Buy_and_Sell_Stock II_Unlimited_Transaction.py +374,DynamicProgramming,Best Time to Buy and Sell Stock III 2 Transactions,https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/, Hard,https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/41_Best_Time_to_Buy_and_Sell_Stock_III_2_Transactions.py +375,DynamicProgramming,Best Time to Buy and Sell Stock IV k Transactions,https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/, Hard,https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/42_Best_Time_to_Buy_and_Sell_Stock_IV_k_Transactions.py +376,DynamicProgramming,Best Time To Buy and Sell Stock with Cooldown,https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/, Mediun,https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/43_Best_Time_To_Buy_and_Sell_Stock_with_Cooldown.py +377,DynamicProgramming,Best Time to Buy and Sell Stock with Transaction Fee,https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/, Medium,https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/44_Best_Time_to_Buy_and_Sell_Stock_with_Transaction_Fee.py +378,DynamicProgramming,Maximum Alternating Subsequence Sum,https://leetcode.com/problems/maximum-alternating-subsequence-sum/, Medium,https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/45_Maximum Alternating Subsequence Sum.py +379,DynamicProgramming,Longest Increasing Subsequence,https://leetcode.com/problems/longest-increasing-subsequence/, Medium,https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/46_Longest_Increasing_Subsequence.py +380,DynamicProgramming,Maximum Length of Pair Chain,https://leetcode.com/problems/maximum-length-of-pair-chain/, Medium,https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/47_Maximum_Length_of_Pair_Chain.py +381,DynamicProgramming,Longest String Chain,https://leetcode.com/problems/longest-string-chain/, Medium,https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/48_Longest_String_Chain.py +382,DynamicProgramming,Longest Increasing Path in a Matrix,https://leetcode.com/problems/longest-increasing-path-in-a-matrix/, Hard,https://raw.githubusercontent.com/glowfi/DS/main/Programs/9_DynamicProgramming/49_Longest_Increasing_Path_in_a_Matrix.py