101 Logo
onenoughtone

Problem Statement

Number Pattern Generator

You're a mathematics teacher preparing a lesson on combinatorial patterns. To help your students visualize the concept of binomial coefficients, you want to generate Pascal's Triangle.

In Pascal's Triangle, each number is the sum of the two numbers directly above it. The first row always contains just the number 1.

Given a non-negative integer numRows, your task is to generate the first numRows of Pascal's Triangle.

For example, when numRows = 5, the triangle looks like:

[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]

Examples

Example 1:

Input: numRows = 5
Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
Explanation: The first 5 rows of Pascal's Triangle.

Example 2:

Input: numRows = 1
Output: [[1]]
Explanation: Just the first row, which is always [1].

Constraints

  • 1 <= numRows <= 30

Problem Breakdown

To solve this problem, we need to:

  1. Each row starts and ends with 1
  2. Each element inside a row is the sum of the two elements above it
  3. The nth row has n elements
  4. The triangle is symmetric around its center
  5. The sum of the elements in the nth row is 2^(n-1)
  6. Each row represents the coefficients of the binomial expansion (a+b)^n
ProblemSolutionCode
101 Logo
onenoughtone