From 8abedd35ca8adc82da209002780e55923eb89838 Mon Sep 17 00:00:00 2001 From: himanshu010 Date: Tue, 15 Dec 2020 15:19:40 +0530 Subject: [PATCH] add: optimal bst --- .../Optimal Binary Search Tree gfg.cpp | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 C++/Dynamic Programming/Optimal Binary Search Tree gfg.cpp diff --git a/C++/Dynamic Programming/Optimal Binary Search Tree gfg.cpp b/C++/Dynamic Programming/Optimal Binary Search Tree gfg.cpp new file mode 100644 index 00000000..80508b74 --- /dev/null +++ b/C++/Dynamic Programming/Optimal Binary Search Tree gfg.cpp @@ -0,0 +1,113 @@ +/* +*-----------------------------------------------------------* +| | +| | +| AUTHOR: Himanshu Aswal | +| ( website: himanshu010.github.io/Portfolio_website ) | +| | +| | +*-----------------------------------------------------------* +*/ +#include +#define moduli 998244353 +#define int long long int +#define ld long double +#define F first +#define S second +#define P pair +#define pb push_back +#define vi vector +#define vvi vector> +#define vb vector +#define um unordered_map +using namespace std; + +void findMinConfig(vi keys, vi value, int n) { + vector>dp(n, vector

(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 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++; + } +} \ No newline at end of file