Pascal's Triangle II

Given an integer rowIndex, return row number rowIndex (0-indexed) of Pascal's triangle. In Pascal's triangle every number is the sum of the two numbers directly above it, and each row starts and ends with 1. Follow up: can you use only O(rowIndex) extra space?

30:00

Pascal's Triangle II
easy
Free written solution
Topics
Companies

Given an integer rowIndex, return row number rowIndex (0-indexed) of Pascal's triangle. In Pascal's triangle every number is the sum of the two numbers directly above it, and each row starts and ends with 1.

Follow up: can you use only O(rowIndex) extra space?

Example 1:
Input: {"rowIndex":3}
Output: [1,3,3,1]
Constraints:
  • 0≤rowIndex≤330 \leq \text{rowIndex} \leq 33

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"rowIndex":3}

Allocate one array of 4 slots, all set to 1. It will be rewritten in place into each row of the triangle in turn.

Pascal's triangle

each value = the two above it

r0

1

r1

1
1

row[] (one array, 4 slots)

already row 1

still row 0

1

0

1

1

1

2

1

3

Variables
VariableValue
rowIndex3
row[1,1,1,1]
DepthFunction Call
Stack empty
0/7