Skip to content

Commit 18472db

Browse files
committed
Algorithm for Catalan Number (DP)
1 parent 622ab09 commit 18472db

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

algorithm/category.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@
7272
"sliding_window": "Sliding Window",
7373
"integer_partition": "Integer Partition",
7474
"ugly_numbers": "Ugly Numbers",
75-
"pascal_triangle": "Pascal's Trangle"
75+
"pascal_triangle": "Pascal's Trangle",
76+
"catalan_number": "Catalan Number"
7677
}
7778
},
7879
"greedy": {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
A[0] = 1;
2+
tracer._notify ( 0, A[0] )._wait();
3+
tracer._denotify ( 0 );
4+
A[1] = 1;
5+
tracer._notify ( 1, A[1] )._wait();
6+
tracer._denotify ( 1 );
7+
8+
for (var i = 2; i <= N; i++) {
9+
for (var j = 0; j < i; j++) {
10+
A[i] += A[j] * A[i-j-1];
11+
tracer._select( j )._wait();
12+
tracer._select( i - j -1 )._wait();
13+
tracer._notify( i, A[i])._wait();
14+
tracer._deselect( j );
15+
tracer._deselect( i - j - 1 );
16+
tracer._denotify( i );
17+
}
18+
}
19+
20+
logger._print ( ' The ' + N + 'th Catalan Number is ' + A[N] );
21+
tracer._select( N )._wait();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var N = 10;
2+
var A = new Array ( N+1 );
3+
for (var i = N; i >= 0; i--) {
4+
A[i] = 0;
5+
}
6+
7+
var tracer = new Array1DTracer( ' Catalan Numbers ')._setData( A );
8+
var logger = new LogTracer();

algorithm/dp/catalan_number/desc.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"Catalan Number": " Pascal's triangle is a triangular array of the binomial coefficients.",
3+
"Applications": [
4+
"The number of ways to stack coins on a bottom row that consists of n consecutive coins in a plane, such that no coins are allowed to be put on the two sides of the bottom coins and every additional coin must be above two other coins, is the nth Catalan number",
5+
"The number of ways to group a string of n pairs of parentheses, such that each open parenthesis has a matching closed parenthesis, is the nth Catalan number",
6+
"The number of ways to cut an n+2-sided convex polygon in a plane into triangles by connecting vertices with straight, non-intersecting lines is the nth Catalan number. This is the application in which Euler was interested."
7+
],
8+
"Complexity": {
9+
"time": " O(N<sup>2</sup>)",
10+
"space": "O(N)"
11+
},
12+
"References": [
13+
"<a href='https://en.wikipedia.org/wiki/Catalan_number'>Wikipedia</a>",
14+
"<a href='http://oldweb.sbc.edu/sites/default/files/Honors/XiaotongJiang.July20_0.pdf'>Sweet Briar College"
15+
],
16+
"files": {
17+
"catalan_number": "Catalan Number"
18+
}
19+
}

0 commit comments

Comments
 (0)