Why taking O(n^2) space causing MLE in this code?


  • 0
    A
    class Solution {
    public:
        bool canJump(int A[], int n) {
            bool **b=new bool*[n];
            int i,j,k;
            for(i=0;i<n;i++)
            b[i]=new bool[n];
            for(i=0;i<n;i++)
            for(j=0;j<n;j++)
            b[i][j]=false;
            for(int i=0;i<n;i++)
            {
                for(int j=i;j<=i+A[i];j++)
                b[i][j]=true;
            }
            if(b[0][n-1]) return true;
            for(i=0;i<n;i++)
            {
                j=i+1;
                if(j<n && b[i][j])
                {
                    k=j+1;
                    while(k<n && b[j][k])
                    {
                        b[i][k]=true;
                        k++;
                    }
                }
            }
            return b[0][n-1];
        }
    };

  • 0
    S

    Could you please update your post with words of algorithm explanation and comments in your code? Thanks.


Log in to reply
 

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