Skip to content

Commit e5dd08e

Browse files
Add solution for LeetCode #118: Pascal's Triangle
1 parent 252f701 commit e5dd08e

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# Pascal's Triangle - Problem #118
2+
3+
## Problem Statement
4+
Given an integer `numRows`, return the first `numRows` of Pascal's triangle.
5+
6+
In Pascal's triangle, each number is the sum of the two numbers directly above it as follows:
7+
8+
## Examples
9+
```
10+
Input: numRows = 5
11+
Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
12+
13+
Input: numRows = 1
14+
Output: [[1]]
15+
```
16+
17+
## Approach
18+
**Key Insight**: Each row can be generated based on the previous row. The edges are always 1, and inner elements are the sum of the two elements from the previous row.
19+
20+
**Algorithm**:
21+
1. Initialize a list of lists to store the triangle.
22+
2. For each row from 0 to numRows-1:
23+
- Create a new list for the current row.
24+
- Set the first and last elements to 1.
25+
- For inner positions, set value to sum of previous row's adjacent elements.
26+
3. Return the triangle.
27+
28+
**Why this works**:
29+
- Builds the triangle row by row, using the definition of Pascal's triangle.
30+
- Efficiently computes each element without redundant calculations.
31+
32+
## Complexity Analysis
33+
- **Time Complexity**: O(numRows²) - We iterate through each row and each element in the row.
34+
- **Space Complexity**: O(numRows²) - To store the entire triangle.
35+
36+
## Key Insights
37+
- Pascal's triangle rows are binomial coefficients.
38+
- Can be optimized to O(numRows) space by generating in-place or only keeping previous row.
39+
- Useful in combinatorics and probability.
40+
41+
## Alternative Approaches
42+
1. **Mathematical Formula**: Use binomial coefficients C(n, k) = n! / (k!(n-k)!) for each position - but less efficient due to factorial computations.
43+
2. **Single Row Generation**: Generate each row independently without storing previous ones, but still O(n²) time.
44+
45+
## Solutions in Different Languages
46+
47+
### Java
48+
```java
49+
// See solution.java
50+
import java.util.*;
51+
52+
class Solution {
53+
public List<List<Integer>> generate(int numRows) {
54+
List<List<Integer>> triangle = new ArrayList<>();
55+
56+
for (int i = 0; i < numRows; i++) {
57+
List<Integer> row = new ArrayList<>();
58+
for (int j = 0; j <= i; j++) {
59+
if (j == 0 || j == i) {
60+
row.add(1);
61+
} else {
62+
row.add(triangle.get(i - 1).get(j - 1) + triangle.get(i - 1).get(j));
63+
}
64+
}
65+
triangle.add(row);
66+
}
67+
68+
return triangle;
69+
}
70+
}
71+
```
72+
73+
### JavaScript
74+
```javascript
75+
// See solution.js
76+
/**
77+
* @param {number} numRows
78+
* @return {number[][]}
79+
*/
80+
var generate = function(numRows) {
81+
const triangle = [];
82+
83+
for (let i = 0; i < numRows; i++) {
84+
const row = [];
85+
for (let j = 0; j <= i; j++) {
86+
if (j === 0 || j === i) {
87+
row.push(1);
88+
} else {
89+
row.push(triangle[i - 1][j - 1] + triangle[i - 1][j]);
90+
}
91+
}
92+
triangle.push(row);
93+
}
94+
95+
return triangle;
96+
};
97+
```
98+
99+
## Test Cases
100+
```
101+
Test Case 1: 5 → [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
102+
Test Case 2: 1 → [[1]]
103+
Test Case 3: 0 → []
104+
Test Case 4: 3 → [[1],[1,1],[1,2,1]]
105+
```
106+
107+
## Edge Cases
108+
- numRows = 0 (empty triangle)
109+
- numRows = 1 (single row)
110+
- Large numRows (up to 30 as per constraints, to avoid integer overflow)
111+
112+
## Related Problems
113+
- Pascal's Triangle II (generate single row)
114+
- Binomial Coefficient problems
115+
- Triangle path problems
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public List<List<Integer>> generate(int numRows) {
5+
List<List<Integer>> triangle = new ArrayList<>();
6+
7+
for (int i = 0; i < numRows; i++) {
8+
List<Integer> row = new ArrayList<>();
9+
for (int j = 0; j <= i; j++) {
10+
if (j == 0 || j == i) {
11+
row.add(1);
12+
} else {
13+
row.add(triangle.get(i - 1).get(j - 1) + triangle.get(i - 1).get(j));
14+
}
15+
}
16+
triangle.add(row);
17+
}
18+
19+
return triangle;
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number} numRows
3+
* @return {number[][]}
4+
*/
5+
var generate = function(numRows) {
6+
const triangle = [];
7+
8+
for (let i = 0; i < numRows; i++) {
9+
const row = [];
10+
for (let j = 0; j <= i; j++) {
11+
if (j === 0 || j === i) {
12+
row.push(1);
13+
} else {
14+
row.push(triangle[i - 1][j - 1] + triangle[i - 1][j]);
15+
}
16+
}
17+
triangle.push(row);
18+
}
19+
20+
return triangle;
21+
};

0 commit comments

Comments
 (0)