Can anyone help me memonize it?


#1

I dont think its possible to memonize the recursive approach since we can have same element repeated more than once we will have to go deeep!

void getMin(int A,vector<int> vec,vector<int> ans,vector<int> &final){
    if(A<0)
        return ;
    if(A==0){
        if(ans.size()>final.size())
            final=ans;
        return ;
    }
    for(int i=0;i<vec.size();i++){
        ans.push_back(i);
        getMin(A-vec[i],vec,ans,final);
        ans.pop_back();
    }
}
vector<int> Solution::solve(int A, vector<int> &B) {
    vector<int> res,tmp;
    getMin(A,B,tmp,res);
    return res;
}

#2

I am also stuck at this…don’t think recursive approach will work here