Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dd9381f

Browse files
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

Lines changed: 11 additions & 1 deletion
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)

AltStyle によって変換されたページ (->オリジナル) /