Tensorum
LeetCode 119 · Triangle / Pascal

Pascal's Triangle II

LeetCode 119 · Easy · solution, intuition & step-by-step walkthrough

C(r, i+1) = C(r, i) · (r − i) / (i + 1).

EasyArrayDynamic Programming
0 msruntime · beats 100%
17.8 MBmemory · beats 100%
1approach
3accepted runs · last 11 May 2025

This is how I solved Pascal's Triangle II (LeetCode 119), an easy problem from the Triangle / Pascal pattern. Below: the intuition, the approach step by step, my accepted Python code, animated test cases and a line-by-line debugger.

Intuition

Row r of Pascal's triangle is C(r, 0), C(r, 1), …, C(r, r). Each coefficient comes from the previous one by multiplying by (r − i) and dividing by (i + 1), so the row can be produced in a single pass without building the triangle above it.

Approach

  1. 1

    Start the row with 1.

  2. 2

    For i from 0 to r-1, update the running coefficient with multiply-then-divide.

  3. 3

    Append each coefficient to the row.

How I solved it

Running nCrPythonO(r) time · O(1) extra space
class Solution:
    def getRow(self, rowIndex: int) -> List[int]:
        res=[1]
        ans=1
        for i in range(rowIndex):
            ans=ans*(rowIndex-i)
            ans= ans//(i+1)
            res.append(ans)
        return res
Loading the step-through…

Pascal's Triangle II — FAQ

What is the intuition behind Pascal's Triangle II?

Row r of Pascal's triangle is C(r, 0), C(r, 1), …, C(r, r). Each coefficient comes from the previous one by multiplying by (r − i) and dividing by (i + 1), so the row can be produced in a single pass without building the triangle above it.

How do you solve Pascal's Triangle II (LeetCode 119)?

1. Start the row with 1. 2. For i from 0 to r-1, update the running coefficient with multiply-then-divide. 3. Append each coefficient to the row.

What is the time and space complexity of Pascal's Triangle II?

Time: O(r). Space: O(r) for the output. (Accepted approach: Running nCr.)

What are common mistakes in Pascal's Triangle II?

Multiply before dividing so the integer division stays exact.