Pascal's Triangle - Problem

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. The triangle starts with 1 at the top, and each row begins and ends with 1.

Example: For numRows = 5, the triangle looks like:

    1
   1 1
  1 2 1
 1 3 3 1
1 4 6 4 1

Input & Output

Example 1 — Basic Triangle
$ Input: numRows = 5
Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
💡 Note: First 5 rows of Pascal's triangle. Row 0 is [1], Row 1 is [1,1], Row 2 is [1,2,1] where 2=1+1, Row 3 is [1,3,3,1] where 3=1+2, and Row 4 is [1,4,6,4,1] where 4=1+3 and 6=3+3.
Example 2 — Small Triangle
$ Input: numRows = 1
Output: [[1]]
💡 Note: Only the top of the triangle with just one element: 1
Example 3 — Two Rows
$ Input: numRows = 2
Output: [[1],[1,1]]
💡 Note: First two rows: the top [1] and second row [1,1]

Constraints

  • 1 ≤ numRows ≤ 30

Visualization

Tap to expand
Pascal's Triangle: Input numRows = 5Input:5→ Process → Output:[1][1,1][1,2,1][1,3,3,1][1,4,6,4,1]Triangle Structure:1111211331Each green number = sum of two numbers above it
Understanding the Visualization
1
Input
numRows = 5 (we want first 5 rows)
2
Process
Build triangle where each number = sum of two above
3
Output
2D array with 5 rows of Pascal's triangle
Key Takeaway
🎯 Key Insight: Pascal's triangle follows a simple pattern where each interior element is the sum of the two elements directly above it, making it perfect for dynamic programming.
Asked in
Apple 15 Microsoft 12 Google 8 Amazon 6
125.0K Views
Medium Frequency
~15 min Avg. Time
2.9K 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