The first line of input contains an integer 'T' representing the number of the test case.
The first and the only line of each test case contains two space-separated integers ‘M’ and ‘N’, denoting the number of rows and number of columns of the matrix respectively.
For every test case, return a single integer, which is the total number of unique paths for traveling from top-left to bottom-right cells of the matrix.
The output of each test case is printed in a separate line.
You don’t have to print anything, it has already been taken care of. Just implement the given function.
1 ≤ T ≤ 100
1 ≤ M ≤ 15
1 ≤ N ≤ 15
Where ‘M’ is the number of rows and ‘N’ is the number of columns in the matrix.
Time limit: 1 sec
We can easily count the total number of paths by making a recursive algorithm.
The steps are as follows:
Our last approach was very simple and easy, but it’s time complexity was of exponential order. We can improve our solution by taking care of the overlapping subproblems. Thus, in this approach, we will eliminate the need for solving the same subproblems again and again by storing them in a lookup table. This approach is known as Memoization.
The steps are as follows:
As we already know that we can improve our solution by taking care of the overlapping subproblems. Also, we can obtain the optimal solution of the problem by using the optimal solutions of its subproblems. Here, the concept of Dynamic Programming can be applied here and we can avoid the recomputation of the same subproblems by storing the result in a temporary 2-D array ‘DP’.
The steps are as follows:
The previous approach improved our solution to a large extent by avoiding the common subproblem recomputations. But the space complexity can be even further improved as the results of subproblems can also be stored in a 1-D array.
The steps are as follows: