educative.io

Shouldn't the space complexity by O(N**2)?

I read the solution and it saids

Space will be used for the recursion stack O(N).

We also need O(N) for the currentPath.

Since the currentPath is part of the recursion function argument, shouldn’t the Space complexity be
O(N*N) → O(N**2)

Thanks for the help in advance!

Since the currentpath is part of the recursion function argument, the Space complexity is
O(N) + O(N) → O(N). We create currentpath once outside recursive function and pass it to the recursive function so no matter how long the recursive stack currentpath is just O(N).

1 Like