Time Complexity (Big O Time):
-
The program uses dynamic programming to fill in a 2D array
dm
of sizem
xn
, where 'm' is the number of rows and 'n' is the number of columns in the grid. -
The first two loops (one for the last row and one for the last column) each run in O(max(m, n)) time because they iterate through the entire last row and last column.
-
The
recur
method is called recursively for each cell in the grid, but it uses memoization (dm
array) to avoid redundant calculations. In the worst case, it recursively computes the minimum path sum for all cells in the grid. -
Therefore, the overall time complexity of the program is O(m * n), where 'm' is the number of rows, and 'n' is the number of columns in the grid.
Space Complexity (Big O Space):
-
The program uses a 2D array
dm
of sizem
xn
to store the intermediate results. Therefore, the space complexity is O(m * n), where 'm' is the number of rows, and 'n' is the number of columns in the grid. -
The space complexity is dominated by the
dm
array, and it does not depend on the input values of 'm' and 'n' but only on the grid size. -
Additionally, the program uses some integer variables and constants, but their space usage is constant and does not depend on the grid size.
In summary, the time complexity of the provided program is O(m * n), and the space complexity is also O(m * n), where 'm' is the number of rows, and 'n' is the number of columns in the grid. The program efficiently calculates the minimum path sum in the grid using dynamic programming with memoization.