![]() |
VOOZH | about |
In combinatorial mathematics, the Lobb number Lm, n counts the number of ways that n + m open parentheses can be arranged to form the start of a valid sequence of balanced parentheses.
The Lobb number are parameterized by two non-negative integers m and n with n >= m >= 0. It can be obtained by:
Lobb Number is also used to count the number of ways in which n + m copies of the value +1 and n - m copies of the value -1 may be arranged into a sequence such that all of the partial sums of the sequence are non- negative.
Examples :
Input : n = 3, m = 2 Output : 5 Input : n =5, m =3 Output :35
The idea is simple, we use a function that computes binomial coefficients for given values. Using this function and above formula, we can compute Lobb numbers.
35
Time Complexity: O(2*n*(m+n))
Auxiliary Space: O((2*n)*(m+n))
Efficient approach: Space optimization
In 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:
35
Time Complexity: O(n^2)
Auxiliary Space: O(k)