Pascal's Triangle II - Solution & Explanation
Problem Statement
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:
0 <= rowIndex <= 33
Follow up: Could you optimize your algorithm to use only O(rowIndex) extra space?
Approach Overview
Problem Overview: Given an integer rowIndex, return the exact row from Pascal’s Triangle. Each element is the sum of the two numbers directly above it, and the first and last values of every row are always 1. The challenge is to compute the requested row efficiently without building unnecessary rows.
Approach 1: Iterative Dynamic Programming (O(n2) time, O(n) space)
This approach builds the row iteratively using the same recurrence rule that defines Pascal’s Triangle. Start with a list initialized to zeros with length rowIndex + 1, set the first element to 1, and update values row by row. To avoid overwriting values you still need, iterate from right to left: row[j] = row[j] + row[j-1]. This works because each update uses values from the previous iteration state. The algorithm runs a nested loop where the outer loop represents rows and the inner loop updates elements within the row, giving O(n^2) time and O(n) space.
This method directly mirrors the recurrence relation and is easy to reason about in interviews. It’s a classic example of space-optimized dynamic programming using a single array instead of a full 2D triangle. The row is treated like a rolling DP state.
Approach 2: Combinatorial Approach (O(n) time, O(1) extra space)
Each value in Pascal’s Triangle corresponds to a binomial coefficient: C(n, k). The kth element in row n equals n! / (k! * (n-k)!). Instead of computing factorials directly, use the recurrence between consecutive coefficients: C(n, k) = C(n, k-1) * (n - k + 1) / k. Start with 1 for C(n,0), then iteratively compute the next coefficient using the previous value.
This produces the entire row in a single pass. Because each element is derived from the previous one using constant-time arithmetic, the algorithm runs in O(n) time and uses only the output array, resulting in O(1) extra space. The approach relies on mathematical properties of combinations and works well when you recognize the binomial pattern inside the array output.
The combinatorial method is the most efficient solution and avoids nested loops entirely. It’s common in competitive programming and shows strong understanding of the underlying mathematics.
Recommended for interviews: Start by explaining the iterative DP solution because it clearly demonstrates the Pascal recurrence and space optimization. Then mention the combinatorial formula as the optimal improvement. Interviewers typically expect the DP reasoning first, but the O(n) combinatorial approach shows deeper insight and stronger algorithmic maturity.
Approach 1: Iterative Approach
The iterative approach builds each row of Pascal's Triangle from the topmost row down to the target row, updating values in-place. The key observation is that each element at position j in a row is the sum of the element at position j and j-1 from the previous row. By starting from the end of the row, you can use the same array for updating values without overwriting the necessary data.
This C program achieves the solution using in-place updates for constructing Pascal's Triangle. It manages memory dynamically and prints the resulting row for a given rowIndex.
Complexity
Time Complexity: O(n^2) where n is rowIndex as it involves nested iteration to build each row.
Space Complexity: O(n) for storing the result row.
Approach 2: Combinatorial Approach
This approach utilizes the combinatorial formula for a specific row in Pascal's Triangle. Specifically, the k-th element in the n-th row is given by the binomial coefficient: C(n, k) = n! / (k! * (n-k)!). Using this fact, we can derive all the values of the row without constructing the triangle iteratively.
In this C solution, a helper function calculates binomial coefficients using combinatorial properties, avoiding overflow by working iteratively.
Complexity
Time Complexity: O(n).
Space Complexity: O(n) for storing the row.
Approach 3: Recursion
We create an array f of length rowIndex + 1, initially all elements are 1.
Next, starting from the second row, we calculate the value of the jth element in the current row from back to front, f[j] = f[j] + f[j - 1], where j \in [1, i - 1].
Finally, return f.
The time complexity is O(n^2), and the space complexity is O(n). Here, n is the given number of rows.
Code
Python
Java
C++
Go
TypeScript
Rust
JavaScript
Complexity Comparison
| Approach | Complexity |
|---|---|
| Iterative Approach | Time Complexity: O(n^2) where n is |
| Combinatorial Approach | Time Complexity: O(n). |
| Recursion | — |
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Dynamic Programming | O(n^2) | O(n) | When explaining Pascal's Triangle recurrence or when mathematical insight is not required. |
| Combinatorial (Binomial Coefficients) | O(n) | O(1) extra | When optimizing for linear time and minimal space using the binomial coefficient relationship. |
Video Solution
Pascal's Triangle ii | LeetCode 119 | C++, Java, Python • Knowledge Center • 29,623 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Pascal's Triangle II easy or hard?
Pascal's Triangle II Python/Java solution
How to solve Pascal's Triangle II in O(n)?
What is the best approach for Pascal's Triangle II?
Is Pascal's Triangle II asked at Google/Amazon/Meta?
What data structure is used in Pascal's Triangle II?
What is the time complexity of Pascal's Triangle II?
Ready to solve this problem?
Practice Pascal's Triangle II with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor