Skip to main content

Pascal's Triangle - Solution & Explanation

EasyArrayDynamic Programming17 min readAsked at: Bank of America, Amazon, Microsoft +14
Practice this problem

Problem Statement

Given an integer numRows, return the first numRows of Pascal's triangle.

In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:

 

Example 1:

Input: numRows = 5
Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]

Example 2:

Input: numRows = 1
Output: [[1]]

 

Constraints:

  • 1 <= numRows <= 30

Approach Overview

Problem Overview: Generate the first numRows of Pascal's Triangle. Each number in the triangle equals the sum of the two numbers directly above it. The first and last value of every row is always 1. The output is a 2D array where each inner array represents a row.

Approach 1: Iterative Construction (O(n²) time, O(n²) space)

This method builds the triangle row by row using a dynamic programming idea. Start with the first row [1]. For every new row i, create an array of size i + 1. Set the first and last elements to 1. For the inner elements, compute values using the previous row: row[j] = prevRow[j-1] + prevRow[j]. Continue this process until numRows rows are generated. The algorithm touches each element once, so the total time complexity is O(n²). The triangle itself stores n(n+1)/2 numbers, giving O(n²) space complexity. This approach is straightforward, avoids recursion overhead, and is the most common solution used in production code. It relies heavily on array indexing and sequential iteration, making it a good example of problems involving the Array pattern combined with simple Dynamic Programming.

Approach 2: Recursive Pascal's Triangle Construction (O(n²) time, O(n²) space)

The recursive strategy mirrors the mathematical definition of Pascal's Triangle. Define a function that generates the triangle up to row n. The base case returns [[1]]. For each recursive call, first compute the triangle up to n-1, then construct the new row using the last row of the previously generated triangle. The first and last elements remain 1, while middle elements are computed using adjacent pairs from the previous row. Although recursion makes the logic elegant, the triangle still requires visiting every element, resulting in O(n²) time complexity. The space complexity is also O(n²) for storing the triangle, plus O(n) recursion stack depth. Recursive implementations are useful when practicing recursion patterns or understanding the mathematical structure of Pascal's Triangle, but iterative construction is usually preferred in interviews.

Recommended for interviews: Interviewers typically expect the iterative construction approach. It demonstrates that you understand the relationship between rows and can translate that rule into array operations. Writing the brute recursive version shows conceptual understanding of the triangle's definition, but the iterative Dynamic Programming build is cleaner, avoids stack overhead, and is the solution most candidates present during coding interviews.

Approach 1: Iterative Construction Approach

This approach involves generating Pascal's Triangle row by row using an iterative method. The first row is initialized with [1], and for each subsequent row, the first and last element are always 1. The intermediate elements are calculated as sums of appropriate elements from the previous row.

In this C implementation, we allocate a 2D array to hold the triangle's content. We initialize the first and last entries of each row to 1. For each intermediate row entry, we compute its value as the sum of the two appropriate values from the preceding row, incrementally building each row from top to bottom.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(numRows^2) due to the nested loop structure.
Space Complexity: O(numRows^2) as we store all elements of the triangle.

Try this approach in the editor →

Approach 2: Recursive Pascal's Triangle Construction

This approach constructs Pascal's Triangle using recursion by calculating each value based on the combination formula. The values on the edges are always 1, and other values are calculated as combinatorial numbers derived recursively.

In this C recursive implementation, Pascal's Triangle values are computed using the combination formula. Recursive calls determine each value's placement using typical combinatorial calculations, recursively reducing the problem until base conditions are met.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(numRows^3) due to recursive calls, not optimal.
Space Complexity: O(numRows^2) for storing the triangle.

Try this approach in the editor →

Approach 3: Simulation

We first create an answer array f, then set the first row of f to [1]. Next, starting from the second row, the first and last elements of each row are 1, and for other elements f[i][j] = f[i - 1][j - 1] + f[i - 1][j].

The time complexity is O(n^2), where n is the given number of rows. Ignoring the space consumption of the answer, the space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Rust

JavaScript

C#

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Iterative Construction Approach

Time Complexity: O(numRows^2) due to the nested loop structure.
Space Complexity: O(numRows^2) as we store all elements of the triangle.

Recursive Pascal's Triangle Construction

Time Complexity: O(numRows^3) due to recursive calls, not optimal.
Space Complexity: O(numRows^2) for storing the triangle.

Simulation—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Iterative Construction (Dynamic Programming)O(n²)O(n²)Best general solution. Simple loops, no recursion overhead, commonly expected in interviews.
Recursive Pascal ConstructionO(n²)O(n²) + O(n) stackUseful for understanding the mathematical recurrence or practicing recursion patterns.

Video Solution

LeetCode Pascal's Triangle Solution Explained - Java • Nick White • 165,185 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Pascal's Triangle easy or hard?
Pascal's Triangle is considered an easy problem on most coding platforms. The challenge is recognizing the relationship between adjacent rows and implementing the row-by-row construction correctly using arrays.
How to solve Pascal's Triangle in O(n)?
Producing the full triangle cannot be done in O(n) because the output itself contains O(n^2) numbers. However, generating a single row can be done in O(n) time using combinatorial formulas or incremental updates based on binomial coefficients.
What is the best approach for Pascal's Triangle?
The iterative construction approach using dynamic programming is the most practical solution. Build the triangle row by row and compute each inner value as the sum of the two numbers above it. This method runs in O(n^2) time and uses O(n^2) space to store the triangle, which is optimal since all elements must be generated.
What data structure is used in Pascal's Triangle?
The typical implementation uses a 2D array or a list of lists to store rows of the triangle. Each row is built using values from the previous row, which makes it a simple dynamic programming problem built on top of array indexing.
What is the time complexity of Pascal's Triangle?
Generating the first n rows requires computing n(n+1)/2 elements. Every element is calculated once, so the time complexity is O(n^2). Both iterative and recursive approaches share the same complexity because they must construct the entire triangle.
Pascal's Triangle Python or Java solution approach?
Both Python and Java implementations follow the same idea: iterate from row 0 to numRows-1, initialize each row with 1s at the edges, and compute middle values using the previous row. The algorithm runs in O(n^2) time and stores the result in a list of arrays or lists.
Is Pascal's Triangle asked at Google, Amazon, or Meta interviews?
Pascal's Triangle appears frequently in coding interviews at large tech companies and is common in early interview rounds. It tests array manipulation, understanding of recurrence relations, and basic dynamic programming concepts rather than advanced algorithms.

Ready to solve this problem?

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

Practice on FleetCode