Math Pascal's triangle and combinations

101. Pascal's Triangle II

Easy

Pascal's triangle is a triangle of numbers. Its top row is a single 1. Each row below has one more number than the row above it. It starts and ends with 1, and every number in between is the sum of the two numbers just above it.

The rows are counted from 0, so row 0 is [1], row 1 is [1,1] and row 2 is [1,2,1]. Given an integer rowIndex, return row rowIndex of Pascal's triangle, as a list of its numbers from left to right.

Example 1

Input: rowIndex = 7

Output: [1,7,21,35,35,21,7,1]

Explanation: Row 6 is 1, 6, 15, 20, 15, 6, 1. Each number between the ends of row 7 adds two neighbours from it: 7 = 1 + 6, 21 = 6 + 15 and 35 = 15 + 20.

Example 2

Input: rowIndex = 0

Output: [1]

Explanation: Rows are counted from 0, so row 0 is the single 1 at the top.

Example 3

Input: rowIndex = 40

Output: [1,40,780,9880,91390,658008,3838380,18643560,76904685,273438880,847660528,2311801440,5586853480,12033222880,23206929840,40225345056,62852101650,88732378800,113380261800,131282408400,137846528820,131282408400,113380261800,88732378800,62852101650,40225345056,23206929840,12033222880,5586853480,2311801440,847660528,273438880,76904685,18643560,3838380,658008,91390,9880,780,40,1]

Check your understanding

Work out the answer for this input before you write any code.

rowIndex = 1

Constraints

  • 0 ≤ rowIndex ≤ 66

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.