Skip to content

Commit

Permalink
add khushal87 dynamic-programming-code
Browse files Browse the repository at this point in the history
  • Loading branch information
khushal87 committed Feb 11, 2021
1 parent 57a7546 commit d925e51
Show file tree
Hide file tree
Showing 13 changed files with 476 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Dynamic Programming Tree/diameter_of_tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <bits/stdc++.h>
using namespace std;

struct Node
{
int data;
Node *left, *right;
};

int solve(Node *root, int &res)
{
if (root == nullptr)
return 0;
int l = solve(root->left, res);
int r = solve(root->right, res);
int temp = max(l, r) + 1;
int ans = max(temp, 1 + l + r);
res = max(ans, temp);
return temp;
}

int main()
{
int res = INT_MIN;
Node *root;
solve(root, res);
cout << res << endl;
}
32 changes: 32 additions & 0 deletions Dynamic Programming Tree/max_path_sum_from_leaf_to_leaf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <bits/stdc++.h>
using namespace std;

struct Node
{
int data;
Node *left, *right;
};

int solve(Node *root, int &res)
{
if (root == nullptr)
return 0;
int l = solve(root->left, res);
int r = solve(root->right, res);
int temp = max(l, r) + root->data;
if (root->left == nullptr && root->right == nullptr)
{
temp = max(temp, root->data);
}
int ans = max(temp, root->data + l + r);
res = max(ans, temp);
return temp;
}

int main()
{
int res = INT_MIN;
Node *root;
solve(root, res);
cout << res << endl;
}
28 changes: 28 additions & 0 deletions Dynamic Programming Tree/max_path_sum_from_node_to_node.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <bits/stdc++.h>
using namespace std;

struct Node
{
int data;
Node *left, *right;
};

int solve(Node *root, int &res)
{
if (root == nullptr)
return 0;
int l = solve(root->left, res);
int r = solve(root->right, res);
int temp = max(max(l, r) + root->data, root->data);
int ans = max(temp, root->data + l + r);
res = max(ans, temp);
return temp;
}

int main()
{
int res = INT_MIN;
Node *root;
solve(root, res);
cout << res << endl;
}
40 changes: 40 additions & 0 deletions Dynamic Programming/edit_distance.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <bits/stdc++.h>
using namespace std;

int minDistance(string x, string y)
{
int m = x.length();
int n = y.length();
int t[m + 1][n + 1];
t[0][0] = 0;
for (int i = 1; i <= m; i++)
{
t[i][0] = i;
}
for (int j = 1; j <= n; j++)
{
t[0][j] = j;
}
for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++)
{
if (x[i - 1] == y[j - 1])
{
t[i][j] = t[i - 1][j - 1];
}
else
{
t[i][j] = 1 + min({t[i - 1][j], t[i][j - 1], t[i - 1][j - 1]});
}
}
}
return t[m][n];
}

int main()
{
string x, y;
cin >> x >> y;
minDistance(x, y);
}
57 changes: 57 additions & 0 deletions Dynamic Programming/longest_commen_subsequence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <bits/stdc++.h>
using namespace std;

string findLCS(string x, string y, int m, int n)
{
int t[m + 1][n + 1];
for (int i = 0; i <= m; i++)
{
for (int j = 0; j <= n; j++)
{
if (i == 0 || j == 0)
{
t[i][j] = 0;
}
else if (x[i - 1] == y[j - 1])
{
t[i][j] = 1 + t[i - 1][j - 1];
}
else
{
t[i][j] = max(t[i - 1][j], t[i][j - 1]);
}
}
}
int i = m, j = n;
string s = "";
while (i > 0 && j > 0)
{
if (x[i - 1] == y[j - 1])
{
s += x[i - 1];
i--, j--;
}
else
{
if (t[i - 1][j] < t[i][j - 1])
{
j--;
}
else
{
i--;
}
}
}
reverse(s.begin(), s.end());
return s;

//for length return t[m][n];
}

int main()
{
string x, y;
cin >> x >> y;
cout << findLCS(x, y, x.length(), y.length()) << endl;
}
41 changes: 41 additions & 0 deletions Dynamic Programming/longest_commen_substring.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <bits/stdc++.h>
using namespace std;

int findLCS(string x, string y, int m, int n)
{
int t[m + 1][n + 1];
for (int i = 0; i <= m; i++)
{
for (int j = 0; j <= n; j++)
{
if (i == 0 || j == 0)
{
t[i][j] = 0;
}
else if (x[i - 1] == y[j - 1])
{
t[i][j] = 1 + t[i - 1][j - 1];
}
else
{
t[i][j] = 0;
}
}
}
int mx = INT_MIN;
for (int i = 0; i <= m; i++)
{
for (int j = 0; j <= n; j++)
{
mx = max(mx, t[i][j]);
}
}
return mx;
}

int main()
{
string x, y;
cin >> x >> y;
cout << findLCS(x, y, x.length(), y.length()) << endl;
}
38 changes: 38 additions & 0 deletions Dynamic Programming/longest_palindrome_subsequence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <bits/stdc++.h>
using namespace std;

int findLCS(string x, string y, int m, int n)
{
int t[m + 1][n + 1];
for (int i = 0; i <= m; i++)
{
for (int j = 0; j <= n; j++)
{
if (i == 0 || j == 0)
{
t[i][j] = 0;
}
else if (x[i - 1] == y[j - 1])
{
t[i][j] = 1 + t[i - 1][j - 1];
}
else
{
t[i][j] = max(t[i - 1][j], t[i][j - 1]);
}
}
}
return t[m][n];
}

int main()
{
string x;
cin >> x;
string y = "";
for (int i = x.length() - 1; i >= 0; i--)
{
y += x[i];
}
cout << findLCS(x, y, x.length(), y.length()) << endl;
}
33 changes: 33 additions & 0 deletions Dynamic Programming/longest_repeating_sequence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <bits/stdc++.h>
using namespace std;

int findLCS(string x, string y, int m, int n)
{
int t[m + 1][n + 1];
for (int i = 0; i <= m; i++)
{
for (int j = 0; j <= n; j++)
{
if (i == 0 || j == 0)
{
t[i][j] = 0;
}
else if (x[i - 1] == y[j - 1] && i != j)
{
t[i][j] = 1 + t[i - 1][j - 1];
}
else
{
t[i][j] = max(t[i - 1][j], t[i][j - 1]);
}
}
}
return t[m][n];
}

int main()
{
string x;
cin >> x;
cout << findLCS(x, x, x.length(), x.length()) << endl;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <bits/stdc++.h>
using namespace std;

int findLCS(string x, string y, int m, int n)
{
int t[m + 1][n + 1];
for (int i = 0; i <= m; i++)
{
for (int j = 0; j <= n; j++)
{
if (i == 0 || j == 0)
{
t[i][j] = 0;
}
else if (x[i - 1] == y[j - 1])
{
t[i][j] = 1 + t[i - 1][j - 1];
}
else
{
t[i][j] = max(t[i - 1][j], t[i][j - 1]);
}
}
}
return t[m][n];
}

int main()
{
string x, y;
cin >> x >> y;
int ans = findLCS(x, y, x.length(), y.length());
int del = x.length() - ans;
int ins = y.length() - ans;
cout << "Insertion - " << ins << " Deletion - " << del << endl;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <bits/stdc++.h>
using namespace std;

//insertion deletion same hi hogaaa
int findLCS(string x, string y, int m, int n)
{
int t[m + 1][n + 1];
for (int i = 0; i <= m; i++)
{
for (int j = 0; j <= n; j++)
{
if (i == 0 || j == 0)
{
t[i][j] = 0;
}
else if (x[i - 1] == y[j - 1])
{
t[i][j] = 1 + t[i - 1][j - 1];
}
else
{
t[i][j] = max(t[i - 1][j], t[i][j - 1]);
}
}
}
return t[m][n];
}

int main()
{
string x;
cin >> x;
string y = "";
for (int i = x.length() - 1; i >= 0; i--)
{
y += x[i];
}
cout << x.length() - findLCS(x, y, x.length(), y.length()) << endl;
}
1 change: 1 addition & 0 deletions Dynamic Programming/rod_cutting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ int main()
}
int lenRod;
cin >> lenRod;
map<int, int> mp;
cout << solve(p, len, n, lenRod) << endl;
}
Loading

0 comments on commit d925e51

Please sign in to comment.