-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPascal Triangle.cpp
More file actions
33 lines (30 loc) · 950 Bytes
/
Pascal Triangle.cpp
File metadata and controls
33 lines (30 loc) · 950 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
Platform :- Leetcode
Problem :- Pascal Triangle
Approach :- we can analyse that no of elementh at 'ith' level is 'i' ,
--> elements at level 1 = [ 1 ]
--> elements at level 2 = [ 1, 1]
--> for every level greater than 2 we have 1st and last element as '1' and 'jth' elements of that level is sum of elementh at previous level at index [ j-1] and [j]
A[i][j] = A[i-1][j-1] + A[i-1][j]
*/
class Solution {
public:
vector<vector<int>> generate(int n) {
vector<vector<int>>ans;
for(int i=0;i<n;++i){
vector<int>temp(i+1);
if(i==0){
temp[i]=1;
}
else {
temp[0]=1;
temp[i]=1;
for(int j=1;j<i;++j){
temp[j]=ans[i-1][j-1]+ans[i-1][j];
}
}
ans.push_back(temp);
}
return ans;
}
};