Pascal's Triangle II - Problem
Pascal's Triangle II challenges you to find a specific row in the famous mathematical structure known as Pascal's Triangle.

In Pascal's Triangle, each row starts and ends with 1, and every other number is the sum of the two numbers directly above it. Your task is to return the rowIndex-th row (0-indexed) as an array.

Goal: Given an integer rowIndex, return the values in that specific row of Pascal's Triangle.

Example Triangle:
Row 0:     [1]
Row 1: [1,1]
Row 2: [1,2,1]
Row 3: [1,3,3,1]
Row 4: [1,4,6,4,1]

Notice how each number (except the edges) equals the sum of the two numbers above it.

Input & Output

example_1.py β€” Basic Case
$ Input: rowIndex = 3
β€Ί Output: [1,3,3,1]
πŸ’‘ Note: Row 3 of Pascal's Triangle is [1,3,3,1]. The middle elements are calculated as: position 1 = 1+2=3, position 2 = 2+1=3 from the previous row [1,2,1].
example_2.py β€” Edge Case
$ Input: rowIndex = 0
β€Ί Output: [1]
πŸ’‘ Note: The first row (index 0) of Pascal's Triangle contains only one element: 1. This is the base case for the triangle.
example_3.py β€” Small Case
$ Input: rowIndex = 1
β€Ί Output: [1,1]
πŸ’‘ Note: Row 1 of Pascal's Triangle is [1,1]. This row establishes the pattern where each row starts and ends with 1.

Constraints

  • 0 ≀ rowIndex ≀ 33
  • The answer is guaranteed to fit in a 32-bit integer
  • Follow-up: Could you optimize your algorithm to use only O(rowIndex) extra space?

Visualization

Tap to expand
Pascal's Triangle - Mathematical Pattern1Row 011Row 1121Row 21331Row 314641Row 4Pattern: Each highlighted number = sum of two numbers above itExample: 3 = 1 + 2, 6 = 3 + 3🎯 Key InsightWe don't need to store the entire triangle - just transform one row into the next!
Understanding the Visualization
1
Start Simple
Begin with the base row [1]
2
Expand & Update
Add a new 1, then update middle elements from right to left
3
Repeat Process
Continue until we reach the target rowIndex
4
Mathematical Beauty
Each element equals the sum of elements directly above it
Key Takeaway
🎯 Key Insight: Pascal's Triangle has a beautiful mathematical property where each element is the sum of the two elements above it. We can generate any row efficiently by transforming a single array in-place, achieving optimal space complexity.
Asked in
Apple 15 Microsoft 12 Amazon 8 Google 6
78.0K Views
Medium Frequency
~15 min Avg. Time
3.3K 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