Java 2 line Recursion+DP


  • 0

    Here is my simple solution, I think it's better than using double for loop
    '''
    public static int uniquePaths(int m, int n) {
    if(m == 1 || n == 1) return 1;
    return uniquePaths(m-1, n) + uniquePaths(m, n-1);
    }
    ''''


  • 1
    R

    It is not dynamic programming, it's just recursion - and a very inefficient one, because it will calculate values multiple times. You can try for yourself with values like n=20, m=20. It is in no way better than using for loops - it seems shorter, but it's a wrong solution on basic performance level.

    You could make a dynamic programming out of it by adding an array to remember values you've calculated in previous steps.


Log in to reply
 

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