101 Logo
onenoughtone

Problem Statement

Row Extractor

You're a mathematics researcher studying combinatorial patterns. You need to extract a specific row from Pascal's Triangle for your calculations, but you want to do it efficiently without generating the entire triangle.

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

Given a row index rowIndex (0-indexed), your task is to return the rowIndexth row of Pascal's Triangle.

For example, when rowIndex = 3, you should return the 4th row [1,3,3,1].

Follow-up: Could you optimize your algorithm to use only O(rowIndex) extra space?

Examples

Example 1:

Input: rowIndex = 3
Output: [1,3,3,1]
Explanation: The 4th row (0-indexed 3rd row) of Pascal's Triangle.

Example 2:

Input: rowIndex = 0
Output: [1]
Explanation: The 1st row (0-indexed 0th row) is just [1].

Example 3:

Input: rowIndex = 1
Output: [1,1]
Explanation: The 2nd row (0-indexed 1st row) is [1,1].

Constraints

  • 0 <= rowIndex <= 33

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 rowIndex-th row has rowIndex+1 elements
  4. The row is symmetric around its center
  5. The sum of the elements in the rowIndex-th row is 2^rowIndex
  6. Each row represents the coefficients of the binomial expansion (a+b)^rowIndex
  7. We can compute the row directly using the formula C(rowIndex, i) = rowIndex! / (i! * (rowIndex-i)!)
  8. We can also compute the row iteratively using the property C(n, k) = C(n, k-1) * (n-k+1) / k
ProblemSolutionCode
101 Logo
onenoughtone