-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay 176.java
More file actions
35 lines (27 loc) · 926 Bytes
/
Day 176.java
File metadata and controls
35 lines (27 loc) · 926 Bytes
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
import java.util.*;
class Solution {
public ArrayList<Integer> countBSTs(int[] arr) {
int n = arr.length;
int[] catalan = new int[n + 1];
catalan[0] = 1;
catalan[1] = 1;
for (int i = 2; i <= n; i++) {
catalan[i] = 0;
for (int j = 0; j < i; j++) {
catalan[i] += catalan[j] * catalan[i - j - 1];
}
}
ArrayList<Integer> result = new ArrayList<>();
for (int i = 0; i < n; i++) {
int root = arr[i];
int leftCount = 0;
int rightCount = 0;
for (int j = 0; j < n; j++) {
if (arr[j] < root) leftCount++;
else if (arr[j] > root) rightCount++;
}
result.add(catalan[leftCount] * catalan[rightCount]);
}
return result;
}
}