MCPcopy Create free account
hub / github.com/Ainevsia/Leetcode-Rust / numTrees_dp

Method numTrees_dp

96. Unique Binary Search Trees/Solution.cpp:43–52  ·  view source on GitHub ↗

dp thought O(n) time O(n) space

Source from the content-addressed store, hash-verified

41
42 // dp thought O(n) time O(n) space
43 int numTrees_dp(int n) {
44 vector<int> dp (n+1,0);
45 dp[0] = dp[1] = 1;
46 for (int i=2; i<=n; i ++ ) {
47 for (int j=0; j<i; j ++ ) {
48 dp[i] += dp[j] * dp[i - j - 1];
49 }
50 }
51 return dp[n];
52 }
53
54 // cantalan tree
55 // http://www-math.mit.edu/~rstan/ec/catalan.pdf

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected