Math Pascal's triangle and combinations

100. Pascal's Triangle

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 triangle is usually drawn centred, so each number sits between the two numbers it adds. The pictures below write it left-aligned instead. There, the two numbers are the one directly above and the one to its left in the row above.

Given an integer numRows, return the first numRows rows of Pascal's triangle, top row first. Each row is a list of its numbers from left to right.

Example 1

11112113311464115101051

Input: numRows = 6

Output: [[1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1], [1,5,10,10,5,1]]

Explanation: Every row starts and ends with 1. In the last row, 5 = 1 + 4 and 10 = 4 + 6: each is the number directly above it plus the one to the left of that.

Example 2

1

Input: numRows = 1

Output: [[1]]

Explanation: Only the top row, which is a single 1.

Example 3

111

Input: numRows = 2

Output: [[1],[1,1]]

Explanation: The second row is just its two end 1s. It has no numbers in between, so nothing is added yet.

Check your understanding

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

numRows = 5

Constraints

  • 1 ≤ numRows ≤ 67

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.