Problem
Return the first n rows of Pascal’s triangle. Each row starts and ends with 1; every interior entry is the sum of the two entries above it.
Worked examples
Input: n = 4
Output: [[1],[1,1],[1,2,1],[1,3,3,1]]
For example, the middle 3 is 1 + 2 from the preceding row.
Hints
Hint 1
Build rows using only the row immediately above.
Solution approach
- For each row index r, allocate r + 1 ones.
- For positions 1 through r - 1, add the previous row at j - 1 and j.
- Return all rows; n = 0 produces an empty list.
Complexity
O(n²) time and O(n²) output space.
Report & practice notes
Restated practice version with original examples and explanation. The source is a candidate account, not an official question paper; assessment details can vary. Difficulty is our editorial estimate.
Read the candidate’s source report ↗