-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add khushal87 dynamic-programming-code
- Loading branch information
Showing
13 changed files
with
476 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
Dynamic Programming Tree/max_path_sum_from_leaf_to_leaf.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
Dynamic Programming Tree/max_path_sum_from_node_to_node.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
36 changes: 36 additions & 0 deletions
36
Dynamic Programming/min_insertion_and_deletion_to_convert_a_string.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
39 changes: 39 additions & 0 deletions
39
Dynamic Programming/min_insertion_and_deletion_to_make_in_palindrome.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,5 +37,6 @@ int main() | |
} | ||
int lenRod; | ||
cin >> lenRod; | ||
map<int, int> mp; | ||
cout << solve(p, len, n, lenRod) << endl; | ||
} |
Oops, something went wrong.