


You can only move down or right at any point in time.
The first line contains an integer 'T' denoting the number of test cases.
The first line of each test case contains two space-separated integers 'N' and ‘M’ representing the number of rows and number of columns in the grid, respectively.
Next 'N' lines will have 'M' space-separated integers, each line denotes cost values of that row.
For each test case, print the minimum sum of the path from top left to bottom right.
You don't need to print anything, it has already been taken care of. Just implement the given function.
Can you solve this in O(1) space complexity?
1 <= T <= 100
1 <= N, M <= 10^2
1 <= GRID[i][j] <= 10^5
Where 'GRID[i][j]' denotes the value of the cell in the matrix.
Time limit: 1 sec
The main idea is to use recursion to reduce the big problem into several smaller subproblems.
We can see that we were solving the same subproblems multiple times. Thus, this problem was exhibiting overlapping subproblems. This means, in this approach, we need to eliminate the need for solving the same subproblems again and again. Thus, the idea is to use Memoization.
The steps are as follows:
Initially, we were breaking the large problem into small problems but now, let us look at it differently. The idea is to solve the small problem first and then reach the final answer. Thus we will be using a bottom-up approach now.
We are using the value of GRID[i][j] only for calculating the dp values. We will use our GRID vector as our dp table.
The steps are as follows: