Skip to main content

Pascal's Triangle II - Solution & Explanation

EasyArrayDynamic Programming15 min readAsked at: Amazon, Microsoft, Goldman Sachs +3
Practice this problem

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.

Code

C

C++

Java

Python

C#

JavaScript

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.

Try this approach in the editor →

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n).
Space Complexity: O(n) for storing the row.

Try this approach in the editor →

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

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Iterative Approach

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.

Combinatorial Approach

Time Complexity: O(n).
Space Complexity: O(n) for storing the row.

Recursion—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Iterative Dynamic ProgrammingO(n^2)O(n)When explaining Pascal's Triangle recurrence or when mathematical insight is not required.
Combinatorial (Binomial Coefficients)O(n)O(1) extraWhen 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 is classified as an Easy problem. The DP approach is straightforward once you understand the triangle recurrence, while the combinatorial optimization adds a slightly deeper mathematical insight.
Pascal's Triangle II Python/Java solution
Python and Java implementations usually follow the same logic: either update a list in reverse order for the DP approach or compute binomial coefficients iteratively for the O(n) combinatorial solution.
How to solve Pascal's Triangle II in O(n)?
Use the binomial coefficient relationship between consecutive elements. Start with 1 for C(n,0), then compute the next value using C(n,k) = C(n,k-1) * (n-k+1) / k. This produces the entire row sequentially in a single loop.
What is the best approach for Pascal's Triangle II?
The combinatorial approach using binomial coefficients is the most efficient. Each element in row n is C(n, k), which can be computed from the previous coefficient using C(n, k) = C(n, k-1) * (n-k+1) / k. This generates the entire row in O(n) time with O(1) extra space.
Is Pascal's Triangle II asked at Google/Amazon/Meta?
Pascal's Triangle variations frequently appear in interviews at companies like Amazon, Google, and Meta because they test dynamic programming fundamentals, mathematical reasoning, and space optimization techniques.
What data structure is used in Pascal's Triangle II?
The solution typically uses a single array or list to store the current row. In the dynamic programming approach, the array is updated from right to left to preserve values from the previous state.
What is the time complexity of Pascal's Triangle II?
The common dynamic programming solution runs in O(n^2) time because each row requires updating multiple elements. The optimized combinatorial solution computes each element once and runs in O(n) time.

Ready to solve this problem?

Practice Pascal's Triangle II with our built-in code editor and test cases.

Practice on FleetCode