Pascal's Triangle
Easy
Topics
Given an integer n, return the first n rows of Pascal’s triangle.
Each row starts and ends with 1, and every other entry is the sum of the two entries directly above it in the previous row.
On this judge the rows are shown separated by |.
Example 1
i has i + 1 entries. The shaded edges are always 1; every interior entry is the sum of the two directly above it, which is the whole rule. The answer is these 5 rows.Input: n = 5 Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]] Explanation: Row 4 is built from row 3: 1, then 1+2, then 2+1, then 1.
Example 2
Input: n = 1 Output: [[1]] Explanation: The first row is a single 1.
Example 3
Input: n = 2 Output: [[1],[1,1]] Explanation: The second row is two 1s, with no interior entry to compute.
Constraints
- 1 <= n <= 30
Editorial — intuition, approach, complexity and pitfalls
Intuition
Every entry is defined in terms of the row above it, so the rows have to be produced in order — but that is the only constraint, and it means each row is built from exactly one predecessor and nothing earlier.
The shape of a row is fixed before any arithmetic happens: row i (0-indexed) has i + 1 entries, the first and last are 1, and the j-th interior entry is previous[j - 1] + previous[j]. Writing it that way makes the bounds the whole problem — there is no accumulator and nothing to search.
Each entry is also the binomial coefficient "i choose j", which is worth knowing because it answers the sibling problem that asks for one row on its own without building the ones above it.
Approach
- Start with the first row,
[1]. - For each subsequent row, open with a
1. - For each interior position, add the two entries above it — the previous row at that index and at the index before it.
- Close with a
1and append the row. Stop afternrows.
Walkthrough
n = 5
row 0 [1]
row 1 1, 1 [1, 1]
row 2 1, 1+1, 1 [1, 2, 1]
row 3 1, 1+2, 2+1, 1 [1, 3, 3, 1]
row 4 1, 1+3, 3+3, 3+1, 1 [1, 4, 6, 4, 1]
Each interior entry reads exactly two numbers from the row
above it, which is why one previous row is all that has to
be kept if only the last row is wanted.Complexity
- Time — O(n^2): the triangle holds n(n+1)/2 entries and each costs one addition.
- Space — O(n^2): the output itself. Producing only the last row needs O(n).
Common Pitfalls
- Appending the trailing
1inside the interior loop, which gives row 1 three entries instead of two. - Running the interior loop to
irather thani - 1, which reads past the end of the previous row. - Returning
n + 1rows by starting the loop at 0 after seeding the first row. - Mutating the previous row in place while reading it. Each row needs its own list, or the sums pick up values already overwritten.
Full Solution — all languages
class Solution:
def generate(self, n):
rows = [[1]]
for i in range(1, n):
previous = rows[-1]
row = [1]
for j in range(1, i):
row.append(previous[j - 1] + previous[j])
row.append(1)
rows.append(row)
return rows[:n]import java.util.*;
class Solution {
public List<List<Integer>> generate(int n) {
List<List<Integer>> rows = new ArrayList<>();
rows.add(new ArrayList<>(Arrays.asList(1)));
for (int i = 1; i < n; i++) {
List<Integer> previous = rows.get(rows.size() - 1);
List<Integer> row = new ArrayList<>();
row.add(1);
for (int j = 1; j < i; j++) row.add(previous.get(j - 1) + previous.get(j));
row.add(1);
rows.add(row);
}
return rows.subList(0, n);
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: vector<vector<int>> generate(int n) {
vector<vector<int>> rows;
rows.push_back({1});
for (int i = 1; i < n; i++) {
vector<int>& previous = rows.back();
vector<int> row;
row.push_back(1);
for (int j = 1; j < i; j++) row.push_back(previous[j - 1] + previous[j]);
row.push_back(1);
rows.push_back(row);
}
rows.resize(n);
return rows;
}
};/**
* @param {number} n
* @return {number[][]}
*/ var generate = function(n) {
const rows = [[1]];
for (let i = 1; i < n; i++) {
const previous = rows[rows.length - 1];
const row = [1];
for (let j = 1; j < i; j++) row.push(previous[j - 1] + previous[j]);
row.push(1);
rows.push(row);
}
return rows.slice(0, n);
};