DP Problem Algortithms
DP Problem Algortithms
2. If i > j, return 0.
3. If i == j, return 1.
5. If str[i] != str[j],
1. Define a function scs(s1,s2, i, j) to find the shortest common supersequence in the substring str[1...i]
and substring[1…j];
2. If i ==0, return j.
3. If j== 0, return i.
5. s1[i-1] != s2[j-1]
2.Intialized IN[0]=1;
4.iterate IN array index 0 to n and find the maximum value and print the
value.
5.End of Algorithm.
the worst-case time complexity of the above solution is O(N(2)).
10. Longest Bitonic Subsequence
To find the longest increasing subsequence (LIS) in an array (nums) of size
n.
1.create an array IN[n];
2.Intialized IN[0]=1;
2. If i ==0, return j.
3. If j==0 , return i.
5. If str[i] != str[j],
3. If i ==0, return 0.
4. If j==0 , return 1.
5.if str[i-1]==str[j-1],
-Exclude the last character from both str and ptr counts(str,ptr,i-1,j-1)
6.if str[i-1]==str[j-1],
2. if target==0, return 1.
3. If target<0, return 0.
-result +=count(arr,target-arr[i]).
6. return result
2. if target==0, return 0.
6. return result
2. if n==0, return 0.
3. If n==1
- If last_digit==0, return 2.
4.if last_digit==0,
5.if last_digit==1,
- if j+1<n ,
-if i-1>0 ,
if mat[i][j]+1== mat[i-1][j], recur to top cell path(mat,i-1,j,n)+1.
-if j-1>0 ,
if mat[i][j]+1== mat[i][j-1], recur top left cell path(mat,i,j-1,n)+1.
Return maximum these four option.
8. End of algoritm
P-8: Print all shortest common super sequence (SCS)
Algorithm:
1. Create a 2D array ‘dp table’ to store the length of SCS for substrings of ‘X’ and ‘Y’.
2. Fill ‘dp table’ using dynamic programming to determine the length of the SCS for all substrings of ‘X’
and ‘Y’.
If the last characters of ‘X’ and ‘Y’ match, append that character to all SCS of the remaining substrings.
• If the top cell of ‘dp table’ has a smaller value than the left cell, append the last character of ‘X’ to all
SCS of the remaining substrings. (dp [i - 1] [j] < dp[i] [j - 1])
• If the left cell of ‘dp table’ has a smaller value than the top cell, append the last character of ‘Y’ to all
SCS of the remaining substrings. (dp [i - 1] [j] > dp[i] [j - 1])
• If both cells have the same value, append both characters to all SCS of the remaining substrings.
Time Complexity:
2. Get the number of rows ‘M’ by calling ‘mat.size()’, and the number of columns ‘N’ by calling
‘mat[0].size()’.
3. Create a 2D vector ‘dp’ of size ‘M’x’N’, initialized with all elements set to 0.
4. Initialize the first row of ‘dp’ with the corresponding elements from the first row of ‘mat’, and
do the same for the first column.
5. Iterate over the remaining positions of ‘dp’ (starting from index 1,1):
if (mat[i][j] == 1)
7. Now, start (0,0) index and calculate dp[i][j] and compare to ‘maxSize’ and update if
necessary.
8. Print the size of the largest square submatrix of 1's, which is ‘maxSize’.
9. Return ‘maxSize’.
Time Complexity:
Matrix[0][j]+=matrix[0][j-1].
Matrix[i][0]+=matrix[i-1][0].
4.iterate matrix elment skip first row and first collum matrix[1…i][1…j]