The Catalan Numbers
Let's solve The Catalan Numbers problem using Dynamic Programming.
Statement
Given a number n
, find the Catalan number.
Catalan numbers are a special sequence of numbers given by the following set of formulas:
The first expression gives the base case of the formula. The second expression says that the Catalan number is simply the sum of products of specific Catalan number pairs. These specific pairs are just Catalan numbers with the same distance from either end of the series.
for example would be equal to:
The Catalan numbers form the following series:
Note: The Catalan numbers readily appear in many interesting counting problems.
- The number of ways to put parentheses around numbers for multiplication.
- The number of paths to climb up a x grid without going above the diagonal.
- The number of possible binary trees with leaf nodes.
Constraints:
-
n
Examples
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.