Hone logo
Hone
Problems

Generating a Specific Row of Pascal's Triangle

Pascal's Triangle is a triangular array of binomial coefficients. Each number in the triangle is the sum of the two numbers directly above it. This pattern has applications in combinatorics, probability, and polynomial expansion. This challenge focuses on efficiently generating a specific row of Pascal's Triangle without generating all preceding rows.

Problem Description

You are tasked with writing a function that takes an integer rowIndex as input and returns the rowIndex-th row of Pascal's Triangle. The rows are 0-indexed, meaning the top row (containing just 1) is row 0.

Key Requirements:

  • The function should return a list (or array) of integers representing the rowIndex-th row of Pascal's Triangle.
  • The output list should be 0-indexed.

Expected Behavior:

  • For rowIndex = 0, the output should be [1].
  • For rowIndex = 1, the output should be [1, 1].
  • For rowIndex = 2, the output should be [1, 2, 1].
  • And so on, following the rules of Pascal's Triangle.

Edge Cases:

  • The input rowIndex will be a non-negative integer.

Examples

Example 1:

Input: rowIndex = 3
Output: [1, 3, 3, 1]
Explanation:
Row 0: [1]
Row 1: [1, 1]
Row 2: [1, 2, 1]
Row 3: [1, 3, 3, 1] (Each element is the sum of the two elements directly above it: 1+2=3, 2+1=3)

Example 2:

Input: rowIndex = 0
Output: [1]
Explanation: The 0th row of Pascal's Triangle is simply [1].

Example 3:

Input: rowIndex = 4
Output: [1, 4, 6, 4, 1]
Explanation:
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] (1+3=4, 3+3=6, 3+1=4)

Constraints

  • 0 <= rowIndex <= 33
  • The input rowIndex will be an integer.
  • The solution should aim for optimal space complexity, ideally O(rowIndex) or better, as generating the entire triangle might exceed memory limits for larger rowIndex.

Notes

  • Recall that each number in Pascal's Triangle is the sum of the two numbers directly above it. The edges of the triangle are always 1.
  • Consider how you can build the current row based on the previous row.
  • Think about if you can optimize the space complexity by not storing all previous rows.
  • The problem statement specifies "Pascal's Triangle II", implying that an efficient solution for generating only the specified row is expected.
Loading editor...
plaintext