Pascal's Triangle
Input: numRows = 5 Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
//each nth row has n numbers
// first and last no is always 1
1
1 1
1 2 1
1 3 3 1
vector<vector<int>> generate(int numRows) {
vector<vector<int>> r(numRows);
for (int i = 0; i < numRows; i++) {
r[i].resize(i + 1);
r[i][0] = r[i][i] = 1;
for (int j = 1; j < i; j++)
r[i][j] = r[i - 1][j - 1] + r[i - 1][j];
}
return r;
}
if we need to find the value at r row and c col :
formula is
r-1
C
c-1
if we are given to print a particular row:
nCr -> n= row , r=col
for(int =0;i<k;i++)
{
res*=(n-i);
res/=(i+1);
}
Comments
Post a Comment