Home
DSA Patterns
Dynamic Programming
Pascal's Triangle
Pascal's Triangle (LeetCode 118)
Generate the first numRows rows of Pascal's triangle bottom-up: every row starts and ends with 1, and each inside value is the sum of the two above.
Problem Understanding
Pascal's Triangle: given numRows, return the first numRows rows of Pascal's triangle. In the triangle, each number is the sum of the two numbers directly above it.
Example: numRows = 5 returns [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]].
The Intuition: Each Row Comes From the One Above
This is dynamic programming at its simplest: a value is built from two smaller answers that are already known.
- Row 0 is
[1]. - Every later row starts and ends with
1. - Each inside value is
prev[j − 1] + prev[j], the two values above it.
Nothing older than the previous row is ever needed, the same one-step dependency as Climbing Stairs. The numbers in row n are the binomial coefficients C(n, k), which is why the triangle also counts paths in a grid, as in Unique Paths.
Pascal's Triangle Visualization
ROW 0 = [1]
rows
row
0
value
1
rows built
1
Row 0 is [1]
Next
Row 1 starts with 1
numRows
The triangle grows one value at a time. For an inside value, the two values above it that were added are tinted. Try Seven rows to see the binomial coefficients 1, 6, 15, 20, 15, 6, 1 appear.
Pascal's Triangle Dry Run
Input: numRows = 5
| Row | Built from | Row |
|---|---|---|
| 0 | - | [1] |
| 1 | edges only | [1, 1] |
| 2 | 1 + 1 | [1, 2, 1] |
| 3 | 1 + 2, 2 + 1 | [1, 3, 3, 1] |
| 4 | 1 + 3, 3 + 3, 3 + 1 | [1, 4, 6, 4, 1] |
Pascal's Triangle LeetCode Solution
Build each row from the previous one.
Pascal's Triangle Code
1var generate = function(numRows) {2 const rows = [[1]];3 for (let r = 1; r < numRows; r++) {4 const prev = rows[r - 1], row = [1];5 for (let j = 1; j < r; j++) row.push(prev[j - 1] + prev[j]);6 row.push(1);7 rows.push(row);8 }9 return rows;10};
Pascal's Triangle in Python, Java, C++, TypeScript and C
Pascal's Triangle in Python
class Solution:
def generate(self, numRows: int) -> List[List[int]]:
rows = [[1]]
for r in range(1, numRows):
prev = rows[-1]
row = [1]
for j in range(1, r):
row.append(prev[j - 1] + prev[j])
row.append(1)
rows.append(row)
return rowsPascal's Triangle in Java
class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> rows = new ArrayList<>();
rows.add(List.of(1));
for (int r = 1; r < numRows; r++) {
List<Integer> prev = rows.get(r - 1), row = new ArrayList<>();
row.add(1);
for (int j = 1; j < r; j++) row.add(prev.get(j - 1) + prev.get(j));
row.add(1);
rows.add(row);
}
return rows;
}
}Pascal's Triangle in C++
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> rows = {{1}};
for (int r = 1; r < numRows; r++) {
vector<int> row(r + 1, 1);
for (int j = 1; j < r; j++) row[j] = rows[r - 1][j - 1] + rows[r - 1][j];
rows.push_back(row);
}
return rows;
}
};Pascal's Triangle in TypeScript
function generate(numRows: number): number[][] {
const rows: number[][] = [[1]];
for (let r = 1; r < numRows; r++) {
const prev = rows[r - 1], row: number[] = [1];
for (let j = 1; j < r; j++) row.push(prev[j - 1] + prev[j]);
row.push(1);
rows.push(row);
}
return rows;
}Pascal's Triangle in C
int** generate(int numRows, int* returnSize, int** returnColumnSizes) {
int** rows = malloc(numRows * sizeof(int*));
*returnColumnSizes = malloc(numRows * sizeof(int));
*returnSize = numRows;
for (int r = 0; r < numRows; r++) {
rows[r] = malloc((r + 1) * sizeof(int));
(*returnColumnSizes)[r] = r + 1;
rows[r][0] = rows[r][r] = 1;
for (int j = 1; j < r; j++) rows[r][j] = rows[r - 1][j - 1] + rows[r - 1][j];
}
return rows;
}Edge Cases & Common Mistakes
- numRows = 1: return
[[1]]; the loop does not run. - Row 1:
[1, 1]has no inside values; the inner loop runs zero times. - Off-by-one on the inner loop: inside positions are
1 … r − 1; positions 0 and r are the edges. - Reusing the same row object: in Java and JavaScript, create a new list for each row instead of mutating the previous one.
- Pascal's Triangle II (LeetCode 119): to return only row k in O(k) space, update a single array from right to left:
row[j] += row[j − 1].
Summary
Start with [1]. Each new row begins and ends with 1, and every inside value is the sum of the two values above it. Repeat for numRows rows. O(n²) time and space, which is the size of the output.
More practice: DSA interview questions with answers · patterns cheat sheet
