Pascal's Triangle III
Pattern: Matrix traversal
The first n rows of Pascal's triangle again, this time with each row built on its own from the binomial formula. It is a good check that you understand both ways to fill the triangle.
The problem
Given n, return the first n rows of Pascal's triangle as a list of rows. The first row is [1], and each value
inside a row is the sum of the two values above it. For example, n = 4 gives
[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1]].
The function name pascalTriangleIII matches the takeUforward practice page. Use the link above for the exact
statement and limits. The output is the same as LeetCode's Pascal's Triangle, so this guide builds each row a different
way: from the formula of Pascal's Triangle II instead of from the row above.
Think first
- Can you build any single row without looking at the row above it?
- If each row is built on its own, what do you need to remember between rows?
- Which version is easier to get right, and which needs less memory per step?
Hints
Brute force, and why it fails
Compute every value with factorials: slow and it overflows. Building from the row above (the Pascal's Triangle way) and building each row from the formula (this way) are both O(n²) in total.
The key insight
Pascal's triangle has two equal descriptions: sums of neighbours above, and binomial numbers C(r, c). Either one fills
every row, and knowing both lets you jump straight to a single row or a single value.
Dry run
| r | 0 |
|---|
n = 4. Row 0 has one value, C(0, 0) = 1.
The plan
rows = empty list
for r from 0 to n - 1:
row = empty list, value = 1
for c from 0 to r:
add value to row
value = value * (r - c) / (c + 1)
add row to rows
return rows
Complexity
- Time
O(n²)- Space
O(n²)
There are about n² / 2 values and each takes O(1) work. The output holds all of them.
Edge cases
n = 1:[[1]].- Rows count from 1 in the statement but from 0 in the formula, so row
rof the formula is the statement's rowr + 1. - Up to 30 rows, every value fits in a 32-bit number.
I've built my logic, show the code
Java
import java.util.*;
class Solution {
public List<List<Integer>> pascalTriangleIII(int n) {
List<List<Integer>> rows = new ArrayList<>();
for (int r = 0; r < n; r++) {
List<Integer> row = new ArrayList<>();
long value = 1; // C(r, 0)
for (int c = 0; c <= r; c++) {
row.add((int) value);
value = value * (r - c) / (c + 1); // next value in the same row
}
rows.add(row);
}
return rows;
}
}C++
class Solution {
public:
vector<vector<int>> pascalTriangleIII(int n) {
vector<vector<int>> rows;
for (int r = 0; r < n; r++) {
vector<int> row;
long long value = 1; // C(r, 0)
for (int c = 0; c <= r; c++) {
row.push_back(static_cast<int>(value));
value = value * (r - c) / (c + 1); // next value in the same row
}
rows.push_back(row);
}
return rows;
}
};Python
class Solution:
def pascalTriangleIII(self, n: int) -> list[list[int]]:
rows = []
for r in range(n):
row, value = [], 1 # C(r, 0)
for c in range(r + 1):
row.append(value)
value = value * (r - c) // (c + 1) # next value in the same row
rows.append(row)
return rowsCommon mistakes
- Mixing up the 1-based row numbers of the statement with the 0-based
rof the formula, so every row is one too long. - Dividing before multiplying.
- Resetting
valueonce outside the row loop instead of at the start of every row.