-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path118. Pascal's Triangle.cpp
More file actions
57 lines (41 loc) · 1.7 KB
/
118. Pascal's Triangle.cpp
File metadata and controls
57 lines (41 loc) · 1.7 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// -----Approach 1 ------------------------------------------------------------
/*
Problem Link: https://leetcode.com/problems/pascals-triangle/
Time: 6 ms (Beats 6.52%), Space: 6.4 MB (Beats 83.65%)
*/
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> res;
for(int i=0; i<numRows; i++){
vector<int> ans(i+1, 1); // we are making corresponding elements for the pascal triangle
for(int j=1; j<i; j++){ // we are using the row value to actually jump to the initial step
ans[j]= res[i-1][j-1] + res[i-1][j]; // we are using the above two values to get this value
// res.push_back(ans);
}
res.push_back(ans);
}
return res;
}
};
// -----Approach 2 ------------------------------------------------------------
/*
Problem Link: https://leetcode.com/problems/pascals-triangle/
Time: 4 ms (Beats 30.5%), Space: 6.6 MB (Beats 58.96%)
*/
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> res(numRows);
for(int i=0; i<numRows; i++){
// vector<int> ans(i+1, 1); // we are making corresponding elements for the pascal triangle
res[i].resize(i + 1);
res[i][0] = res[i][i] = 1;
for(int j=1; j<i; j++){ // we are using the row value to actually jump to the initial step
res[i][j]= res[i-1][j-1] + res[i-1][j]; // we are using the above two values to get this value
}
// res.push_back(ans);
}
return res;
}
};