Pascal's Triangle

Easy

Topics
ArrayDynamic Programming

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

1····11···121··1331·14641
always 1, the two edgessum of the two cells above it
Row 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.
Blank cells are not part of the triangle — the rows are ragged, not rectangular.
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

1
always 1, the two edges
Input:  n = 1
Output: [[1]]
Explanation: The first row is a single 1.

Example 3

1·11
always 1, the two edges
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

  1. Start with the first row, [1].
  2. For each subsequent row, open with a 1.
  3. For each interior position, add the two entries above it — the previous row at that index and at the index before it.
  4. Close with a 1 and append the row. Stop after n rows.

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 1 inside the interior loop, which gives row 1 three entries instead of two.
  • Running the interior loop to i rather than i - 1, which reads past the end of the previous row.
  • Returning n + 1 rows 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]
SensAI
Beta
Tap the mic to stop voice mode