Menu
CoddyTech

Pascal's Triangle

In Pascal's triangle, the first row is [1]. Every later row is one entry longer, starts and ends with 1, and each entry in between is the sum of the two entries directly above it. You get an integer numRows. Return the first numRows rows of the triangle, top row first, each row as an array of integers.

Function

generate(numRows: integer) → integer-2d-array
numRowsinteger
how many rows of the triangle to build
Returnsinteger-2d-array
the first numRows rows, top row first

Constraints

  • 1 ≤ numRows ≤ 30
  • Every entry of the first 30 rows fits in a 32-bit signed integer. The largest is 77558760, in the middle of row 30.

Examples

Input
numRows = 5
Output
[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]
Explanation
Each inner entry adds the two above it. In the fourth row, 3 = 1 + 2 and 3 = 2 + 1. In the fifth row, 4 = 1 + 3, 6 = 3 + 3 and 4 = 3 + 1.

lock icon+13 hidden tests on Submit

challenge icon

Follow-up

Can you build only the last row in a single array, updating it in place row after row instead of keeping the rows above? Which way must the inner loop run, and why?

Reset code
def generate(numRows):
    # Write code here
Test cases

Case 1

Case 2

Input

numRows = 5

Expected

[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]