Skip to content

Commit dd9381f

Browse files
committed
Fix boundary condition
1 parent 4d56693 commit dd9381f

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

OptimalBST.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ using namespace std;
44

55
int cost[N][N], obst[N][N], prefix[N];
66

7+
/*
8+
* function to find optimal binary search tree (with minimum cost)
9+
* time complexity : O(n^3)
10+
* space complexity : O(n^2)
11+
*/
712
void findOptimalBST(int keys[], int freq[], int n) {
813
int i,j,l,r;
914
/*precompute prefix sums*/
@@ -21,7 +26,7 @@ void findOptimalBST(int keys[], int freq[], int n) {
2126
obst[i][i] = i;
2227
}
2328
for(l=2; l<=n; l++) {
24-
for(i=0; i<=n-l+1; i++) {
29+
for(i=0; i<n-l+1; i++) {
2530
j = i+l-1;
2631
cost[i][j] = INT_MAX;
2732
/*Calculate cost for each element in [i,j] as the root and take the minimum*/
@@ -36,6 +41,11 @@ void findOptimalBST(int keys[], int freq[], int n) {
3641
}
3742
}
3843
}
44+
for(i=0;i<n;i++) {
45+
for(j=0;j<n;j++)
46+
cout<<cost[i][j]<<" ";
47+
cout<<endl;
48+
}
3949
}
4050

4151
int main() {

0 commit comments

Comments
 (0)