Skip to content

Commit d0bda15

Browse files
Dynamic Programming - Uncommented Codes in Lect 27, Updated Codes in Lect 44, 51 and Updated Lect 1 Notes
1 parent 1144d34 commit d0bda15

5 files changed

Lines changed: 75 additions & 80 deletions

File tree

Dynamic Programming/Code/Part 5 - DP on Strings/Lect 27 - DP on Strings - Longest Common Substring/Method_1_Recursion.cpp

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,30 @@
1717
// Total Space Complexity : O(n+m)
1818
// Reason : Sum of Auxiliary Space Complexity O(n+m) and Non-Auxiliary Space Complexity O(1) (i.e O(n+m) = O(n+m) + O(1))
1919

20-
// #include <bits/stdc++.h>
21-
// using namespace std;
22-
23-
// int len = 0;
24-
25-
// int f(int i, int j, string s, string t, int& len) {
26-
// // Base Case
27-
// if(i < 0 or j < 0) return 0;
28-
29-
// // Recursive Case
30-
// if(s[i] == t[j]) {
31-
// int val = 1 + f(i-1, j-1, s, t, len);
32-
// len = max(len,s val);
33-
// return 1 + f(i-1, j-1, s, t, len);
34-
// }
35-
// return 0;
36-
// }
37-
38-
// int main()
39-
// {
40-
// string s = "abcdef", t = "abzdef";
41-
// int n = s.size(), m = t.size(), len = 0;
20+
#include <bits/stdc++.h>
21+
using namespace std;
22+
23+
int len = 0;
24+
25+
int f(int i, int j, string s, string t, int& len) {
26+
// Base Case
27+
if(i < 0 or j < 0) return 0;
28+
29+
// Recursive Case
30+
if(s[i] == t[j]) {
31+
int val = 1 + f(i-1, j-1, s, t, len);
32+
len = max(len, val);
33+
return 1 + f(i-1, j-1, s, t, len);
34+
}
35+
return 0;
36+
}
37+
38+
int main()
39+
{
40+
string s = "abcdef", t = "abzdef";
41+
int n = s.size(), m = t.size(), len = 0;
4242

43-
// f(n-1, m-1, s, t, len);
44-
// cout<<"Length of lcs is : "<<len;
45-
// return 0;
46-
// }
43+
f(n-1, m-1, s, t, len);
44+
cout<<"Length of lcs is : "<<len;
45+
return 0;
46+
}

Dynamic Programming/Code/Part 5 - DP on Strings/Lect 27 - DP on Strings - Longest Common Substring/Method_2_Memoization.cpp

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,33 @@
1818
// Total Space Complexity : O(n+m) + O(n*m)
1919
// Reason : Sum of Auxiliary Space Complexity O(n+m) and Non-Auxiliary Space Complexity O(n*m)
2020

21-
// #include <bits/stdc++.h>
22-
// using namespace std;
23-
24-
// int len = 0;
25-
26-
// int f(int i, int j, string s, string t, vector<vector<int>>& dp) {
27-
// // Base Case
28-
// if(i < 0 or j < 0) return 0;
29-
30-
// // Recursive Case
31-
// if(dp[i][j] != -1) return dp[i][j];
32-
// if(s[i] == t[j]) {
33-
// dp[i][j] = 1 + f(i-1, j-1, s, t, dp);
34-
// len = max(len, dp[i][j]);
35-
// return dp[i][j];
36-
// }
37-
// return dp[i][j] = 0;
38-
// }
39-
40-
// int main()
41-
// {
42-
// string s = "abcdef", t = "abzdef";
43-
// int n = s.size(), m = t.size();
44-
// vector<vector<int>> dp(n, vector<int> (m, -1));
21+
#include <bits/stdc++.h>
22+
using namespace std;
23+
24+
int len = 0;
25+
26+
int f(int i, int j, string s, string t, vector<vector<int>>& dp) {
27+
// Base Case
28+
if(i < 0 or j < 0) return 0;
29+
30+
// Recursive Case
31+
if(dp[i][j] != -1) return dp[i][j];
32+
if(s[i] == t[j]) {
33+
dp[i][j] = 1 + f(i-1, j-1, s, t, dp);
34+
len = max(len, dp[i][j]);
35+
return dp[i][j];
36+
}
37+
return dp[i][j] = 0;
38+
}
39+
40+
int main()
41+
{
42+
string s = "abcdef", t = "abzdef";
43+
int n = s.size(), m = t.size();
44+
vector<vector<int>> dp(n, vector<int> (m, -1));
4545

46-
// f(n-1, m-1, s, t, dp);
46+
f(n-1, m-1, s, t, dp);
4747

48-
// cout<<"Length of lcs is : "<<len;
49-
// return 0;
50-
// }
48+
cout<<"Length of lcs is : "<<len;
49+
return 0;
50+
}

Dynamic Programming/Code/Part 7 - DP on LIS/Lect 44 - DP on LIS - Longest Divisible Subset/Method_1_Weird_Algorithm_To_Trace_LIS.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,32 +40,32 @@ int main()
4040
vector<int> dp(n, 1);
4141
vector<int> hash(n, 0);
4242

43-
int maxLen = 1, lasti = 0;
44-
for (int i = 0; i < n; i++)
43+
int maxLen = 1, lastInd = 0;
44+
for (int ind = 0; ind < n; ind++)
4545
{
46-
hash[i] = i;
47-
for (int j = 0; j < i; j++)
46+
hash[ind] = ind;
47+
for (int j = 0; j < ind; j++)
4848
{
49-
if(arr[i] % arr[j] == 0) {
50-
if(1+dp[j] > dp[i]) {
51-
dp[i] = 1+dp[j];
52-
hash[i] = j;
49+
if(arr[ind] % arr[j] == 0) {
50+
if(1+dp[j] > dp[ind]) {
51+
dp[ind] = 1+dp[j];
52+
hash[ind] = j;
5353
}
5454
}
5555
}
5656

57-
if(dp[i] > maxLen) {
58-
maxLen = dp[i];
59-
lasti = i;
57+
if(dp[ind] > maxLen) {
58+
maxLen = dp[ind];
59+
lastInd = ind;
6060
}
6161
}
6262

6363
vector<int> lis(maxLen);
64-
lis[maxLen-1] = arr[lasti];
64+
lis[maxLen-1] = arr[lastInd];
6565
for (int i = 1; i < maxLen; i++)
6666
{
67-
lasti = hash[lasti];
68-
lis[maxLen-1-i] = arr[lasti];
67+
lastInd = hash[lastInd];
68+
lis[maxLen-1-i] = arr[lastInd];
6969
}
7070

7171
cout<<"Longest Divisible Subset : [ ";

Dynamic Programming/Code/Part 9 - DP on Rectangles/Lect 51 - DP on Rectangles - Maximum Rectangle Area with all 1's/Method_1_Tabulation.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,25 @@
1919
#include <bits/stdc++.h>
2020
using namespace std;
2121

22-
int largestRectangleArea(vector < int > & histo) {
23-
stack < int > st;
22+
int largestRectangleArea(vector<int>& histo) {
23+
stack<int> st;
2424
int maxA = 0;
2525
int n = histo.size();
2626
for (int i = 0; i <= n; i++) {
2727
while (!st.empty() && (i == n || histo[st.top()] >= histo[i])) {
2828
int height = histo[st.top()];
2929
st.pop();
3030
int width;
31-
if (st.empty())
32-
width = i;
33-
else
34-
width = i - st.top() - 1;
31+
if (st.empty()) width = i;
32+
else width = i - st.top() - 1;
3533
maxA = max(maxA, width * height);
3634
}
3735
st.push(i);
3836
}
3937
return maxA;
4038
}
4139

42-
int main()
43-
{
40+
int main() {
4441
vector<vector<int>> mat{
4542
{1, 0, 1, 1},
4643
{1, 0, 1, 1},
@@ -53,10 +50,8 @@ int main()
5350
vector<int> heights(m, 0);
5451

5552
int maxArea = 0;
56-
for (int i = 0; i < n; i++)
57-
{
58-
for (int j = 0; j < m; j++)
59-
{
53+
for (int i = 0; i < n; i++) {
54+
for (int j = 0; j < m; j++) {
6055
if(mat[i][j]) heights[j]++;
6156
else heights[j] = 0;
6257
}
Binary file not shown.

0 commit comments

Comments
 (0)