Skip to content

Commit ba031b6

Browse files
Create 0119-pascal's-triangle-2.md
1 parent 427f075 commit ba031b6

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
---
2+
id: 119-pascals-triangle-ii
3+
title: Pascal's Triangle II
4+
sidebar_label: 0119-Pascal's Triangle II
5+
tags:
6+
- C++
7+
- Java
8+
- Python
9+
description: "Return the kth row of Pascal's triangle."
10+
---
11+
12+
## Problem Description
13+
14+
Given an integer rowIndex, return the rowIndexth (0-indexed) row of the 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: rowIndex = 3
24+
Output: [1,3,3,1]
25+
```
26+
27+
**Example 2:**
28+
29+
```
30+
Input: rowIndex = 0
31+
Output: 1
32+
```
33+
34+
### Constraints
35+
36+
- $ 0 <= rowIndex <= 33$
37+
38+
---
39+
40+
## Solution for Balanced Binary Tree Problem
41+
42+
<Tabs>
43+
<tabItem value="LinearSolution" label="LinearSolution">
44+
45+
### Approach
46+
47+
To generate the kth row of Pascal's Triangle, we can use a single list to iteratively build up the row. By updating the list from the end to the beginning, we ensure that we are always using values from the previous row (or the initial state).
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<Integer> getRow(int rowIndex) {
58+
List<Integer> row = new ArrayList<>(rowIndex + 1);
59+
for (int i = 0; i <= rowIndex; i++) {
60+
row.add(0);
61+
}
62+
row.set(0, 1);
63+
64+
for (int i = 1; i <= rowIndex; i++) {
65+
for (int j = i; j > 0; j--) {
66+
row.set(j, row.get(j) + row.get(j - 1));
67+
}
68+
}
69+
70+
return row;
71+
}
72+
}
73+
```
74+
75+
</TabItem>
76+
<TabItem value="Python" label="Python">
77+
<SolutionAuthor name="@Vipullakum007"/>
78+
```python
79+
class Solution:
80+
def getRow(self, rowIndex: int) -> List[int]:
81+
row = [1] * (rowIndex + 1)
82+
for i in range(2, rowIndex + 1):
83+
for j in range(i - 1, 0, -1):
84+
row[j] += row[j - 1]
85+
return row
86+
```
87+
88+
</TabItem>
89+
<TabItem value="C++" label="C++">
90+
<SolutionAuthor name="@Vipullakum007"/>
91+
```cpp
92+
class Solution {
93+
public:
94+
vector<int> getRow(int rowIndex) {
95+
vector<int> row(rowIndex + 1, 1);
96+
for (int i = 2; i <= rowIndex; i++) {
97+
for (int j = i - 1; j > 0; j--) {
98+
row[j] += row[j - 1];
99+
}
100+
}
101+
return row;
102+
}
103+
};
104+
```
105+
106+
</TabItem>
107+
</Tabs>
108+
109+
#### Complexity Analysis
110+
111+
- **Time Complexity**: $O(k^2)$, where k is the given rowIndex. This is because we have nested loops, one iterating up to rowIndex and the other iterating backward up to i.
112+
- **Space Complexity**: $O(k)$, where k is the given rowIndex. This is the space required to store the rowIndexth row.
113+
114+
</tabItem>
115+
</Tabs>
116+
---

0 commit comments

Comments
 (0)