![]() |
VOOZH | about |
In combinatorics, the Eulerian Number A(n, m), is the number of permutations of the numbers 1 to n in which exactly m elements are greater than previous element.
For example, there are 4 permutations of the number 1 to 3 in which exactly 1 element is greater than the previous elements.
Examples:
Input : n = 3, m = 1
Output : 4
Please see above diagram (There
are 4 permutations where 1 no. is
greater.
Input : n = 4, m = 1
Output : 11
Eulerian Numbers are the coefficients of the Eulerian polynomials described below.
The Eulerian polynomials are defined by the exponential generating function
The Eulerian polynomials can be computed by the recurrence
An explicit formula for A(n, m) is
We can calculate A(n, m) by recurrence relation:
Example:
Suppose, n = 3 and m = 1.
Therefore,
A(3, 1)
= (3 - 1) * A(2, 0) + (1 + 1) * A(2, 1)
= 2 * A(2, 0) + 2 * A(2, 1)
= 2 * 1 + 2 * ( (2 - 1) * A(1, 0) + (1 + 1) * A(1, 1))
= 2 + 2 * (1 * 1 + 2 * ((1 - 1) * A(0, 0) + (1 + 1) * A(0, 1))
= 2 + 2 * (1 + 2 * (0 * 1 + 2 * 0)
= 2 + 2 * (1 + 2 * 0)
= 2 + 2 * 1
= 2 + 2
= 4
We can verify this with example shown above.
Below is the implementation of finding A(n, m):
4
Time Complexity: O(2n)
Auxiliary Space: O(log(n)), Due, to recursive call stack
Below is the implementation of finding A(n, m) using Dynamic Programming:
4
Time Complexity: O(n*m)
Auxiliar Space: O(n*m)
Efficient approach: Space optimization
In the previous approach, the current value dp[i][j] is only depend upon the current and previous row values of DP. So to optimize the space complexity we use a single 1D array to store the computations.
Implementation steps:
Implementation:
4
Time Complexity: O(n*m)
Auxiliary Space: O(m)