I am trying to code with problem:
There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time. Given the two integers m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner. I have successfully created the code for it and I am in the process of optimizing the code to run faster.
My first method is:
gridTraveler_memo = {}
def gridTraveler(m,n):
if (m and n) not in gridTraveler_memo:
if m==1 and n==1:
return 1
elif m==0 or n==0:
return 0
else:
gridTraveler_memo[m,n] = gridTraveler(m-1,n) + gridTraveler(m,n-1)
return gridTraveler_memo[m,n]
print(gridTraveler(18,18))
My second method is using functools.cache (uisng the answer for here):
import functools
@functools.cache
def gridTraveler(m,n):
if m==1 and n==1:
return 1
elif m==0 or n==0:
return 0
else:
return gridTraveler(m-1,n) + gridTraveler(m,n-1)
print(gridTraveler(18,18))
My second set of code works a lot faster than my first set of code. However i don't fully understand "functools.cache" function, as i am new to python.
In my limited expertise, it seems that the first set code is more of a good pratice for the future. For example when working on a big project, using "functools.cache" means you are using more memory. Am i correct in assuming that