119. Pascal's Triangle II - Explanation
Description
You are given an integer rowIndex, return the rowIndexth (0-indexed) row of the Pascal's triangle.
In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:

Example 1:
Input: rowIndex = 3
Output: [1,3,3,1]Example 2:
Input: rowIndex = 0
Output: [1]Example 3:
Input: rowIndex = 1
Output: [1,1]Constraints:
1 <= numRows <= 33
Topics
Prerequisites
Before attempting this problem, you should be comfortable with:
- Dynamic Programming - Building each row from the previous row, using both top-down (recursive) and bottom-up (iterative) approaches
- Recursion - Understanding how to compute a row by first computing all previous rows
- Combinatorics - Recognizing that Pascal's Triangle values are binomial coefficients C(n, k) and computing them efficiently
1. Dynamic Programming (Top-Down)
Intuition
To get row n, we need row n - 1 first. Each row depends on the previous one, with interior elements being the sum of two adjacent elements from above. Recursion naturally models this dependency: we compute the previous row, then build the current row from it.
Algorithm
- Base case: If
rowIndex == 0, return[1]. - Recursively get the previous row.
- Start the current row with
[1]. - For each index from
1torowIndex - 1:- Add
prevRow[i - 1] + prevRow[i]to the current row.
- Add
- Append
1to complete the row. - Return the current
row.
Time & Space Complexity
- Time complexity:
- Space complexity:
2. Dynamic Programming (Bottom-Up)
Intuition
We build the entire triangle iteratively from row 0 up to the target row. Each row is constructed using values from the previous row. Though we store all rows, we only need the last one as our answer.
Algorithm
- Create a 2D list where row
ihasi + 1elements, all initialized to1. - For each row from index 2 to
rowIndex:- For each interior position
j:- Set
res[i][j] = res[i - 1][j - 1] + res[i - 1][j].
- Set
- For each interior position
- Return
res[rowIndex].
Time & Space Complexity
- Time complexity:
- Space complexity:
3. Dynamic Programming (Space Optimized - I)
Intuition
We only need the previous row to compute the current row, so we don't need to store the entire triangle. We keep one row at a time and build the next row by adding contributions from adjacent elements.
Algorithm
- Start with
res = [1]. - For each iteration from
0torowIndex - 1:- Create
nextRowof sizelen(res) + 1, filled with0. - For each element in the current row, add its value to
nextRow[j]andnextRow[j + 1]. - Replace
reswithnextRow.
- Create
- Return
res.
Time & Space Complexity
- Time complexity:
- Space complexity:
4. Dynamic Programming (Space Optimized - II)
Intuition
We can update the row in place by iterating from right to left. This ensures we don't overwrite values we still need. Each element becomes the sum of itself and the element to its left, which matches how Pascal's Triangle is built.
Algorithm
- Initialize
rowwithrowIndex + 1elements, all set to1. - For each row from
1torowIndex - 1:- Iterate from index
idown to1:- Add
row[j - 1]torow[j].
- Add
- Iterate from index
- Return
row.
Time & Space Complexity
- Time complexity:
- Space complexity:
5. Combinatorics
Intuition
The values in row n of Pascal's Triangle are the binomial coefficients C(n, 0), C(n, 1), ..., C(n, n). We can compute each coefficient incrementally from the previous one using the formula: C(n, k) = C(n, k - 1) * (n - k + 1) / k.
Algorithm
- Start with
row = [1]. - For each
ifrom1torowIndex:- Compute the next value as
row[last] * (rowIndex - i + 1) / i. - Append it to the
row.
- Compute the next value as
- Return
row.
Time & Space Complexity
- Time complexity:
- Space complexity:
Common Pitfalls
Integer Overflow in Combinatorics Approach
When computing binomial coefficients using the formula C(n, k) = C(n, k-1) * (n - k + 1) / k, the intermediate multiplication can overflow before the division. Always multiply first and divide immediately, or use long types to hold intermediate results before casting back to int.
Wrong Iteration Direction in Space-Optimized DP
When updating the row in place, iterating from left to right overwrites values that are still needed for subsequent calculations. You must iterate from right to left so that each element is updated using the original (unmodified) values from the previous logical row.
Sign in to join the discussion