Pascal's Triangle II (LeetCode 119): Solution and Explanation

Easy
Array
Dynamic Programming

Return a single row of Pascal's triangle, numbered from 0. The follow-up asks for O(rowIndex) extra space, which rules out building the whole triangle.

Intuition

Each row of Pascal's triangle depends only on the row above it: the edges are 1 and every inner value is the sum of its two upper neighbors. So at any moment you only need one previous row, not the whole triangle.

You can go further and keep just one array. Suppose it holds row i - 1 with a spare 1 at the end. The new value at position j is old row[j - 1] + old row[j], which is exactly row[j] += row[j - 1] as long as row[j - 1] has not been overwritten yet.

That is why the inner loop runs right to left. When j moves down, every position to the right has already become row i, while j - 1 and everything left of it still hold row i - 1. Going left to right would read a value that was already updated in this pass and produce wrong numbers.

The edges take care of themselves. row[0] is always 1, and the trailing slot was initialized to 1, so it becomes the right edge of each new row as the passes reach it.

Approach: In-place row, right-to-left updates

  1. Create row with rowIndex + 1 entries, all 1.
  2. For each pass i from 2 to rowIndex, the array holds row i - 1 in positions 0..i - 1.
  3. Walk j from i - 1 down to 1 and set row[j] += row[j - 1].
  4. After the last pass, return row.

Step-by-step example

rowIndex = 3
  1. row = [1, 1, 1, 1]. Positions 0..1 already equal row 1.
  2. Pass 2, j = 1: row[1] = 1 + row[0] 1 = 2. row = [1, 2, 1, 1], so positions 0..2 are row 2.
  3. Pass 3, j = 2: row[2] = 1 + row[1] 2 = 3. row = [1, 2, 3, 1].
  4. Pass 3, j = 1: row[1] = 2 + row[0] 1 = 3. row = [1, 3, 3, 1].
  5. The passes are done, so the answer is [1, 3, 3, 1].

Code: Python, Java, C++

1class Solution:
2    def getRow(self, rowIndex: int) -> List[int]:
3        row = [1] * (rowIndex + 1)
4        for i in range(2, rowIndex + 1):
5            for j in range(i - 1, 0, -1):
6                row[j] += row[j - 1]
7        return row
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(k^2)

Pass i does i - 1 additions, so the total is about k^2 / 2 for k = rowIndex.

Space

O(k)

Only the output array of k + 1 integers is used.

Other approaches

Binomial coefficients directly (time O(k), space O(k))

Entry j of row k is C(k, j), and C(k, j) = C(k, j - 1) * (k - j + 1) / j. Computing each entry from the previous one gives the row in O(k) time. Multiply before dividing and use 64-bit integers so the division stays exact.

Build the full triangle (time O(k^2), space O(k^2))

Generate rows 0..k as in Pascal's Triangle I and return the last one. It is simple but keeps every row, which misses the follow-up.

Common mistakes

  • Looping j from left to right in the single-array version, which adds an already updated value and breaks every row after row 2.
  • Starting the array with zeros instead of ones. The trailing 1 is what becomes each new row's right edge.
  • Off-by-one on the row number: rowIndex is 0-indexed, so rowIndex = 3 has four entries.
  • In the binomial formula, dividing before multiplying, which truncates and gives wrong values.

Frequently asked questions

Why update from right to left?

row[j] needs the old values of row[j] and row[j - 1]. Moving right to left means row[j - 1] is touched after row[j], so it is still the previous row's value when row[j] reads it.

Can the values overflow a 32-bit int?

Not within the constraint. The largest value in row 33 is C(33, 16) = 1,166,803,110, which is below 2^31.

How is this different from Pascal's Triangle I?

Problem I returns every row up to numRows, so it must store the whole triangle. Here you return one row, which lets you reuse a single array.