I am getting Memory Limit Exceeded


#1

Does anyone have an idea why I am getting this error? To me, it looks like I am using a similar amount of memory like their solution. Thanks!

Your submission ran out of memory for the following input:

A : [ 8, 5, 19, 11, 10, 7, 18, 16, 13, 17 ]

Here is my code:

bool doesExist(vector A, int k)
{
for(int i=0; i<A.size(); ++i)
if(A[i]==k)
return true;
return false;
}

void createSubsets(vector &A, vector currentElements, vector< vector > &allSubsets)
{
for(int i=0; i<A.size(); ++i)
{
if(doesExist(currentElements,A[i]))
continue;

    currentElements.push_back(A[i]);
    allSubsets.push_back(currentElements);
    
    if(currentElements[currentElements.size()-1] != A[A.size()-1])
        createSubsets(A,currentElements, allSubsets);

    currentElements.pop_back();
}

}

vector<vector > Solution::subsets(vector &A) {

sort(A.begin(), A.end());

vector< vector<int> > allSubsets;
vector<int> currentElements;
allSubsets.push_back(currentElements);

if(!A.empty())
    createSubsets(A,currentElements,allSubsets);

return allSubsets;

}