Why am i getting memorty limit exceeded??
Try this case :
You will get an infinite loop here :
And you will exceed the memory limit of your vector “path”.
However, even if you fixed that , I think you would still get TLE because you have O(n^2) complexity.
I recommend using this implementation