easy +10 pts

Catalan number

Compute the nth Catalan number efficiently using dynamic programming.

In combinatorics, the Catalan numbers form a sequence of natural numbers that occur in various counting problems, often involving recursively defined objects. The nth Catalan number C_n (0-indexed) is defined by the recurrence: C_0 = 1 C_n = sum_{i=0}^{n-1} C_i * C_{n-1-i} for n >= 1 Your task is to implement the function `catalan_number(n: int) -> int` that returns C_n. The input n will be a non-negative integer. Use dynamic programming (or any correct method) to compute the value efficiently. The result will fit within the standard integer range in Python. You must write the function with the exact signature `catalan_number(n: int) -> int`.

Constraints

0 <= n <= 20 Time complexity: O(n^2) is acceptable. Space complexity: O(n).

Example

>>> catalan_number(0)
1
>>> catalan_number(1)
1
>>> catalan_number(2)
2
>>> catalan_number(3)
5
>>> catalan_number(10)
16796
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use an array to store C_0 through C_n.
Initialize C_0 = 1.
For each m from 1 to n, compute C_m as the sum of C_i * C_{m-1-i} for i from 0 to m-1.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.