LeetCode Problem

How to Solve Pascal's Triangle II

To solve Pascal's Triangle II, directly compute only the requested row using dynamic programming rather than building the full triangle. Iteratively update a single array from the end toward the beginning to maintain correct state transitions. This approach minimizes memory while accurately reflecting the combinatorial sums for the rowIndex-th row.

GhostInterview Help

Need help with Pascal's Triangle II without spending extra time grinding it?

GhostInterview can read Pascal's Triangle II from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #119State transition dynamic programmingReviewed 2026-03-08
Difficulty
Easy
Primary pattern
State transition dynamic programming
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

To solve Pascal's Triangle II, directly compute only the requested row using dynamic programming rather than building the full triangle. Iteratively update a single array from the end toward the beginning to maintain correct state transitions. This approach minimizes memory while accurately reflecting the combinatorial sums for the rowIndex-th row.

Problem Statement

Given a non-negative integer rowIndex, return the rowIndex-th row (0-indexed) of Pascal's Triangle. Each element in the row is the sum of the two elements directly above it in the previous row.

For example, rowIndex = 3 should return [1,3,3,1], and rowIndex = 0 should return [1]. Implement an approach that efficiently calculates the row using array-based dynamic programming and proper state transitions.

Examples

Example 1

Input: rowIndex = 3

Output: [1,3,3,1]

Example details omitted.

Example 2

Input: rowIndex = 0

Output: [1]

Example details omitted.

Example 3

Input: rowIndex = 1

Output: [1,1]

Example details omitted.

Constraints

  • 0 <= rowIndex <= 33

Solution Approach

Iterative Array Update

Initialize an array of size rowIndex + 1 with 1s and iterate from the second row to rowIndex. Update each element from end to start to avoid overwriting needed values.

In-Place Dynamic Programming

Use a single array to store current row values, modifying elements in place. For each row, update elements from right to left based on the sum of the current element and the one before it.

Space Optimization

Avoid constructing the entire triangle by maintaining only one array for the current row. This reduces space complexity to O(rowIndex) while still using the standard state transition logic.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

Time complexity is O(rowIndex^2) due to nested iteration to update each element, and space complexity is O(rowIndex) because only a single row array is maintained.

What Interviewers Usually Probe

  • Check if you can compute only the requested row without building the full triangle.
  • Expect discussion on in-place updates and avoiding overwriting previous row values.
  • Consider edge cases like rowIndex = 0 or rowIndex = 1 to confirm correct base cases.

Common Pitfalls or Variants

Common pitfalls

  • Overwriting elements from left to right, which corrupts intermediate sums.
  • Attempting to build the full Pascal's Triangle when only one row is needed.
  • Ignoring base cases, leading to index out-of-bounds errors or incorrect first row values.

Follow-up variants

  • Return the triangle up to rowIndex instead of only one row.
  • Compute a specific element in rowIndex-th row using combinatorial formula.
  • Modify the algorithm to handle large rowIndex values efficiently with BigInteger types.

How GhostInterview Helps

  • Highlights the correct dynamic programming state transitions for efficient single-row computation.
  • Provides step-by-step array update examples for interview-ready reasoning.
  • Identifies subtle overwriting pitfalls that often cause wrong outputs in Pascal's Triangle II.

Topic Pages

FAQ

What is the best approach for Pascal's Triangle II using dynamic programming?

Use a single array updated from right to left, applying state transitions to compute only the requested row.

How do I handle rowIndex = 0 or rowIndex = 1?

Return [1] for rowIndex = 0 and [1,1] for rowIndex = 1, handling base cases before iteration.

Can I reduce space usage when generating Pascal's Triangle II?

Yes, by maintaining a single array and updating it in place, space complexity is O(rowIndex).

Why does updating from left to right fail?

It overwrites values needed for computing subsequent elements, breaking the state transition logic.

Is there a combinatorial formula alternative?

Yes, each element can be computed using C(rowIndex, k), but iterative DP is often simpler for interviews.

GhostInterview Solver

Need direct help with Pascal's Triangle II instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Pascal's Triangle II from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.