Ninja technique🥷 to ACE DSA Interviews.
For the i’th row we will create a temp array of size i+1(i is 0 based | row is 1 based) of all elements 1 in it. |
currNum = numOnPrevRowAndPrevCol + numOnPrevRowAndSameCol
for all numbers except first and last.class Solution {
public:
vector<vector<int>> generate(int numRows)
{
vector<vector<int>> ans;
for (int i = 0; i < numRows; i++) {
ans.push_back(vector<int>(i + 1, 1));
for (int j = 1; j < i; j++) {
ans[i][j] = ans[i - 1][j - 1] + ans[i - 1][j];
}
}
return ans;
}
};