I Think Leetcode is Buggy


  • 0
    N

    it says my code past all test case as well as memory limit exceed I dont understand can someone check this.

    public:
        int numSquares(int n) {
            vector<int> v;
            for(int i= 1; i<=n; i++)
            {
                if(i*i == n)
                    return 1;
                else if(i*i < n)
                    v.push_back(i*i);
                else
                    break;
            }
            queue<pair<int,int>> q;
            for(int& i: v)
                q.push(make_pair(i,1));
            while(!q.empty())
            {
                auto node = q.front();
                q.pop();
                if(node.first == n)
                    return node.second;
                if(node.first < n)
                {
                    for(int&i : v)
                    {
                        if(node.first+i == n)
                            return node.second+1;
                        q.push(make_pair(node.first+i,node.second+1));
                    }
                }
            }
            return -1;
        }
    };
    

Log in to reply
 

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