Whats wrong with this code...can anyone rectify


#1

vector<vector > Solution::solve(int A)
{
vector<vector> ans;
if(A==1)
return {{1}};
if(A==2)
return {{1},{1,1}};
ans[0].push_back(1);
ans[1].push_back(1);
ans[1].push_back(1);
for(int i=2;i<A;i++)
{
ans[i][0]=1;
for(int j=1;j<i;j++)
{
ans[i][j]=(ans[i-1][j-1]+ans[i-1][j]);
}
ans[i][i]=1;
}
return ans;
}


#2

you can not assign value to 2d vector like ans[i][0]=1 because it is dynamic so it goes out of bounds and gives runtime error so, to remove error you have to use a temporary vector now put the values to the temp vector and then push the temp vector to the 2d vector.


#3

vector<vector > dp;
for(int i=0;i<n;i++)
dp.push_back(vector(i+1,1));