Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: optimal bst #303

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions C++/Dynamic Programming/Optimal Binary Search Tree gfg.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
*-----------------------------------------------------------*
| |
| |
| AUTHOR: Himanshu Aswal |
| ( website: himanshu010.github.io/Portfolio_website ) |
| |
| |
*-----------------------------------------------------------*
*/
#include<bits/stdc++.h>
#define moduli 998244353
#define int long long int
#define ld long double
#define F first
#define S second
#define P pair<int,int>
#define pb push_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vb vector<bool>
#define um unordered_map
using namespace std;

void findMinConfig(vi keys, vi value, int n) {
vector<vector<P>>dp(n, vector<P>(n));
for (int i = 0; i < n; ++i)
{
dp[i][i] = {value[i], i};
}
int cur = 1;
while (cur <= n) {
int start = 0;
int end = start + cur;

while (end < n) {
int total = 0;
int mn = INT_MAX;
int root = start;
for (int i = start; i <= end; ++i)
{
total += value[i];
if (i == start) {
mn = min(mn, dp[i + 1][end].F);
if (mn == dp[i + 1][end].F) {
root = i;
}

}
else if (i == end) {
mn = min(mn, dp[start][i - 1].F);
if (mn == dp[start][end - 1].F) {
root = i;
}
}
else {
mn = min(mn, dp[start][i - 1].F + dp[i + 1][end].F);
if (mn == dp[start][i - 1].F + dp[i + 1][end].F) {
root = i;
}
}
}
dp[start][end].F = total + mn;
dp[start][end].S = root;
start = start + 1;
end = end + 1;
}
cur++;

}

for (auto x : dp) {
for (auto y : x) {
cout << y.F << ',' << y.S << ' ';
}
cout << endl;
}

cout << dp[0][n - 1].F << endl;
}

void solve(int tc) {
int i, j, k, n, m, ans = 0, cnt = 0, sum = 0;
cin >> n;
vector<int> keys(n), value(n);
for (int i = 0; i < n; ++i)
{
cin >> keys[i];
}
for (int i = 0; i < n; ++i)
{
cin >> value[i];
}

findMinConfig(keys, value, n);

}
int32_t main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base:: sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int tc = 1;
// int t;cin>>t;while(t--)
{
solve(tc);
tc++;
}
}