-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPossibleBST.java
More file actions
131 lines (106 loc) · 2.36 KB
/
PossibleBST.java
File metadata and controls
131 lines (106 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package com.program.dynamic.programming;
/**
* @author rrohit
*/
public class PossibleBST {
private static int count = 0;
public static int possibleBst(int n){
if (n==0 || n==1) {
return n;
}
int input[] = new int[n];
for (int i=0; i<n; i++){
input[i] = i+1;
}
return possibleBst(null, input);
}
private static int possibleBst(int output[], int [] input) {
int sum=0;
if (input.length == 0) {
return isBst(output) ? 1 : 0;
}
for (int i=0; i<input.length; i++) {
int [] next = null;
if (output != null) {
next = new int[output.length+1];
System.arraycopy(output, 0, next, 0, output.length);
}else {
next = new int[1];
}
next[next.length-1] = input[i];
int remain[] = new int [input.length-1];
System.arraycopy(input, 0, remain, 0, i);
System.arraycopy(input, i+1, remain, i, input.length - (i +1));
sum += possibleBst(next, remain);
}
return sum;
}
/**
* BST(i) = Left->BST(i-1)* Right->BST(n-i)
* BST(n) = BST(1) + BST(2) + BST(3)....+BST(n).
*
* BST(n) = Summation (i=1 to N) BST(i-1)*BST(n-i);
*
* Time Complexity is 2^n with lots of repetition. Lets solve this using DP
* @param n
* @return
*/
public static int bst(int n) {
int sum=0;
if(n==0 || n==1) {
count++;
return 1;
}
for (int i=1; i<=n; i++) {
count++;
sum += bst(i-1)*bst(n-i);
}
return sum;
}
/**
* Using table complexity will be reduced to linear
*
* Time = n*(n+1)/2 , which is O(n^2)
* Space = O(n)
* @param n
* @return
*/
public static Long bstDP(int n) {
if (n==0 || n==1){
return 1L;
}
Long table [] = new Long[n];
return bstDP(n, table);
}
public static Long bstDP(int n, Long [] table) {
Long sum=0L;
if (n==0 || n==1) {
table[0] = table[1] = 1L;
count++;
return 1L;
}
for (int i=1; i<= n; i++) {
count++;
if (table[i-1] == null ) {
table[i-1] = bstDP(i-1, table);
}
if (table[n-i] == null) {
table[n-i] = bstDP(n-i, table);
}
sum += table[i-1] * table[n-i];
}
return sum;
}
private static boolean isBst(int[] output) {
for(int i : output){
System.out.print(i);
}
System.out.println();
return true;
}
public static void main(String[] args) {
//System.out.println(possibleBst(4));
System.out.println(bstDP(78));
System.out.println("Count ="+count);
}
}