> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Pascal's Triangle Python Solution with Tests

> Tested Python solution for LeetCode 118 with 12 pytest cases. Generate a practice environment with lcpy.

LeetCode 118, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Dynamic Programming](/catalog/topics/dynamic-programming). [View on LeetCode](https://leetcode.com/problems/pascals-triangle/description/).

Generate this problem as a practice environment: tested reference solution, 12 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 118   # by problem number
lcpy gen -s pascals_triangle   # by problem name
```

## Problem

Given an integer `numRows`, return the first numRows of **Pascal's triangle**.

In **Pascal's triangle**, each number is the sum of the two numbers directly above it as shown:

![Pascal's Triangle](https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif)

### Examples

```
Input: numRows = 5
Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
```

```
Input: numRows = 1
Output: [[1]]
```

### Constraints

* 1 \<= numRows \<= 30

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/pascals_triangle/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/pascals_triangle/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n^2)
    # Space: O(1) excluding output
    def generate(self, num_rows: int) -> list[list[int]]:
        triangle: list[list[int]] = [[1]]
        for _ in range(1, num_rows):
            previous = triangle[-1]
            row = [1] + [previous[i] + previous[i + 1] for i in range(len(previous) - 1)] + [1]
            triangle.append(row)
        return triangle
```

## Complexity

| Time | Space |
| - | - |
| O(n^2) | O(1) excluding output |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.