To find the number of ways to get from one corner of a grid to the opposite corner, by going down or right only. I come up with an initial idea to use the recursion to solve the problem:
def find_num_of_ways(x: int, y: int):
if x == 0 or y == 0:
return 1
return find_num_of_ways(x - 1, y) + find_num_of_ways(x, y - 1)
This can be stack overflow when x and y increase. Want to find a better way to refactor this, one is convert to tail recursion. But given 2 variables in the signature, so how to accumulate the result in order to make it tail recursion?