This is a classic dynamic programming problem. We need to count how many ways exist when we start from the top-left cell in the grid and make way till the bottom-right cell.
Let’s think about reduction. Reduction is the process of breaking down a problem into smaller sub-problems. To calculate the number of ways to reach cell (m, n)
- is basically the number of ways to reach cell (m-1, n)
PLUS the number of ways to reach cell (m, n-1)
.
Note: the number of ways to go from (0,0)
to (m, n)
== the number of ways to go from (m, n)
to (0, 0)
.
Code
Python3
Big O Analysis
-
Runtime
The runtime complexity here is with the caching - without caching it’s .
-
Memory
The memory usage is since the caching mechanism would require some sort of dictionary.
— A