Pascal's Triangle II Lab

Easy
2
33.3% Acceptance

In this lab, you will be working on a JavaScript function that returns a specific row of Pascal's triangle based on the provided index. Your function will implement a space-optimized algorithm that uses only O(rowIndex) extra space.

Pascal's triangle is a triangular array of binomial coefficients. Each number is the sum of the two numbers directly above it, as shown below:

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

Function Signature

Your function will have the following signature:

/** * @param {number} rowIndex * @return {number[]} */ const getRow = (rowIndex) => { };

Your function should return the specified row (0-indexed) of Pascal's triangle as an array of numbers.

Examples

Here are some examples of how your function should behave:

Example 1:

Input: rowIndex = 3
Output: [1, 3, 3, 1]

Example 2:

Input: rowIndex = 0
Output: [1]

Example 3:

Input: rowIndex = 1
Output: [1, 1]

Example 4:

Input: rowIndex = 4
Output: [1, 4, 6, 4, 1]

Constraints:

  • 0 <= rowIndex <= 33

Challenges

  1. Write the getRow function to return the specified row of Pascal's triangle.
  2. Optimize your algorithm to use only O(rowIndex) extra space.