Pascal's Triangle II - Problem

Given an integer rowIndex, return the rowIndex-th (0-indexed) row of Pascal's triangle.

In Pascal's triangle, each number is the sum of the two numbers directly above it. The triangle starts with 1 at the top, and each subsequent row begins and ends with 1.

Example: Row 0: [1], Row 1: [1,1], Row 2: [1,2,1], Row 3: [1,3,3,1]

Input & Output

Example 1 — Row 3
$ Input: rowIndex = 3
Output: [1,3,3,1]
💡 Note: Row 3 of Pascal's triangle: The 4th row (0-indexed) contains the values [1,3,3,1], where each inner element is the sum of the two elements above it from the previous row.
Example 2 — Row 0
$ Input: rowIndex = 0
Output: [1]
💡 Note: Row 0 is the first row of Pascal's triangle and contains only the value 1.
Example 3 — Row 1
$ Input: rowIndex = 1
Output: [1,1]
💡 Note: Row 1 contains [1,1] - two 1s at the edges as every row starts and ends with 1.

Constraints

  • 0 ≤ rowIndex ≤ 33

Visualization

Tap to expand
Pascal's Triangle II: Get Row 3Input: rowIndex = 3Row 01Row 111Row 2121Row 3 (Target)1331Calculation: 1+2=3, 2+1=3Output: [1, 3, 3, 1]
Understanding the Visualization
1
Input
rowIndex = 3 (want the 4th row, 0-indexed)
2
Process
Calculate row 3 values using triangle pattern or formula
3
Output
Return [1, 3, 3, 1]
Key Takeaway
🎯 Key Insight: Each Pascal's triangle element equals C(row, col) - use binomial coefficient formula for optimal O(n) solution
Asked in
Apple 15 Microsoft 12
155.4K Views
Medium Frequency
~15 min Avg. Time
4.2K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen