Skip to content

Commit cf620df

Browse files
committed
2022-05-19 update: added "Unique Binary Search Trees"
1 parent a980a2a commit cf620df

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.smlnskgmail.jaman.leetcodejava.medium;
2+
3+
// https://leetcode.com/problems/unique-binary-search-trees
4+
public class UniqueBinarySearchTrees {
5+
6+
private final int input;
7+
8+
public UniqueBinarySearchTrees(int input) {
9+
this.input = input;
10+
}
11+
12+
public int solution() {
13+
int[] catalan = new int[input + 2];
14+
catalan[0] = 1;
15+
catalan[1] = 1;
16+
for (int i = 2; i <= input; i++) {
17+
for (int j = 0; j < i; j++) {
18+
catalan[i] += catalan[j] * catalan[i - j - 1];
19+
}
20+
}
21+
return catalan[input];
22+
}
23+
24+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.smlnskgmail.jaman.leetcodejava.medium;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
public class UniqueBinarySearchTreesTest {
8+
9+
@Test
10+
public void defaultTest() {
11+
assertEquals(
12+
5,
13+
new UniqueBinarySearchTrees(3).solution()
14+
);
15+
}
16+
17+
}

0 commit comments

Comments
 (0)