-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0096.go
58 lines (49 loc) · 1.57 KB
/
0096.go
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
// Source: https://leetcode.com/problems/unique-binary-search-trees
// Title: Unique Binary Search Trees
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Given an integer n, return the number of structurally unique BST's (binary search trees) which has exactly n nodes of unique values from 1 to n.
//
// Example 1:
//
// https://assets.leetcode.com/uploads/2021/01/18/uniquebstn3.jpg
//
// Input: n = 3
// Output: 5
//
// Example 2:
//
// Input: n = 1
// Output: 1
//
// Constraints:
//
// 1 <= n <= 19
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Pick any number R as root, assume that there are L/R number smaller/greater than R.
// The number of BST = #BST(L) * #BST(R)
func numTrees(n int) int {
arr := make([]int, n+1)
arr[0] = 1
for i := 1; i <= n; i++ {
for j := 0; j < i; j++ {
arr[i] += arr[j] * arr[i-1-j]
}
}
return arr[n]
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Catalan number
var a000170 = []int{
1, 1, 2, 5, 14,
42, 132, 429, 1430, 4862,
16796, 58786, 208012, 742900, 2674440,
9694845, 35357670, 129644790, 477638700, 1767263190,
}
func numTrees2(n int) int {
return a000170[n]
}