3 line code in Ruby


  • 0
    P
    def find_paths(m, n, _n, i, j)
        return 1 if i == -1 or i == m or j == -1 or j == n
        return 0 if _n == 0
        
        return find_paths(m,n,_n-1,i-1,j) + find_paths(m,n,_n-1,i+1,j) + find_paths(m,n,_n-1,i,j-1) + find_paths(m,n,_n-1,i,j+1)
    end
    

    Not sure why it gives time limit exceeded though. Unable to understand why this is worse performing than the DP solution.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.