Skip to content

Commit 94ab509

Browse files
Create 0118-pascal's-triangle.md
1 parent d12904c commit 94ab509

File tree

1 file changed

+147
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)