Why is DP with memoization solution giving TLE ?


  • 0
    S
    class Solution {
    private:
        int lookup[101][101];
        int recurse(int m, int n) {
            if(lookup[m][n] == -1) {
                if(m==1) return 1;
                if(n==1) return 1;
                lookup[m][n] = uniquePaths(m-1,n) + uniquePaths(m,n-1);
            }
            return lookup[m][n];
        }
    public:
        int uniquePaths(int m, int n) {
            memset(lookup,-1,sizeof(lookup));
            return recurse(m,n);
        }
    };

  • 0

    Because you're recursively calling the wrong function.


Log in to reply
 

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