Skip to content

Commit 140ee11

Browse files
authored
Merge pull request #780 from Ayushmaanagarwal1211/113-path-sum-2
Added solution for 118 Pascal's Triangle
2 parents 34c7f19 + 3f721a7 commit 140ee11

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
---
2+
id: 118-pascals-triangle
3+
title: Pascal's Triangle
4+
sidebar_label: 0118-Pascal's Triangle
5+
tags:
6+
- C++
7+
- Java
8+
- Python
9+
description: "Generate the first n rows of Pascal's Triangle."
10+
---
11+
12+
## Problem Description
13+
14+
Given an integer numRows, return the first numRows of Pascal's triangle.
15+
16+
In Pascal's triangle, each number is the sum of the two numbers directly above it.
17+
18+
### Examples
19+
20+
**Example 1:**
21+
22+
```
23+
Input: numRows = 5
24+
Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
25+
```
26+
27+
**Example 2:**
28+
29+
```
30+
Input: numRows = 1
31+
Output: [[1]]
32+
```
33+
34+
### Constraints
35+
36+
- $1 <= numRows <= 30$
37+
38+
---
39+
40+
## Solution for Binary Tree Problem
41+
42+
### Intuition And Approach
43+
44+
To generate Pascal's triangle, we start with the first row [1]. Each subsequent row is generated by adding the number above and to the left with the number above and to the right. We append 1 at the beginning and end of each row.
45+
46+
<Tabs>
47+
<tabItem value="Recursive" label="Recursive">
48+
49+
50+
#### Code in Different Languages
51+
52+
<Tabs>
53+
<TabItem value="Java" label="Java" default>
54+
<SolutionAuthor name="@Vipullakum007"/>
55+
```java
56+
class Solution {
57+
public List<List<Integer>> generate(int numRows) {
58+
List<List<Integer>> triangle = new ArrayList<>();
59+
60+
if (numRows == 0) {
61+
return triangle;
62+
}
63+
64+
// First row
65+
triangle.add(new ArrayList<>());
66+
triangle.get(0).add(1);
67+
68+
for (int rowNum = 1; rowNum < numRows; rowNum++) {
69+
List<Integer> row = new ArrayList<>();
70+
List<Integer> prevRow = triangle.get(rowNum - 1);
71+
72+
// First element is always 1
73+
row.add(1);
74+
75+
// Each triangle element (except the first and last of each row)
76+
for (int j = 1; j < rowNum; j++) {
77+
row.add(prevRow.get(j - 1) + prevRow.get(j));
78+
}
79+
80+
// Last element is always 1
81+
row.add(1);
82+
83+
triangle.add(row);
84+
}
85+
86+
return triangle;
87+
}
88+
}
89+
```
90+
91+
</TabItem>
92+
<TabItem value="Python" label="Python">
93+
<SolutionAuthor name="@Vipullakum007"/>
94+
```python
95+
class Solution:
96+
def generate(self, numRows: int) -> List[List[int]]:
97+
triangle = []
98+
99+
for row_num in range(numRows):
100+
row = [1] * (row_num + 1)
101+
102+
for j in range(1, row_num):
103+
row[j] = triangle[row_num - 1][j - 1] + triangle[row_num - 1][j]
104+
105+
triangle.append(row)
106+
107+
return triangle
108+
```
109+
110+
</TabItem>
111+
<TabItem value="C++" label="C++">
112+
<SolutionAuthor name="@Vipullakum007"/>
113+
```cpp
114+
class Solution {
115+
public:
116+
vector<vector<int>> generate(int numRows) {
117+
vector<vector<int>> triangle;
118+
119+
for (int row_num = 0; row_num < numRows; row_num++) {
120+
vector<int> row(row_num + 1, 1);
121+
122+
for (int j = 1; j < row_num; j++) {
123+
row[j] = triangle[row_num - 1][j - 1] + triangle[row_num - 1][j];
124+
}
125+
126+
triangle.push_back(row);
127+
}
128+
129+
return triangle;
130+
}
131+
};
132+
```
133+
134+
</TabItem>
135+
</Tabs>
136+
137+
#### Complexity Analysis
138+
139+
- Time Complexity: $O(n^2)$ where n is the number of rows. This is because we are generating each element in the triangle, and there are $\frac{n(n+1)}{2}$ elements in total.
140+
- Space Complexity: $O(n^2)$, the space required to store the triangle.
141+
142+
</tabItem>
143+
</Tabs>
144+
145+
146+
---

0 commit comments

Comments
 (0)