> ## 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.

# Minimum Path Sum Python Solution with Tests

> Tested Python solution for LeetCode 64 with 19 pytest cases. Generate a practice environment with lcpy.

LeetCode 64, [Medium](https://leetcode-py.wisl.dev/catalog/medium.md). Topics: [Array](https://leetcode-py.wisl.dev/catalog/topics/array), [Dynamic Programming](https://leetcode-py.wisl.dev/catalog/topics/dynamic-programming), [Matrix](https://leetcode-py.wisl.dev/catalog/topics/matrix). [View on LeetCode](https://leetcode.com/problems/minimum-path-sum/description/).

Generate this problem as a practice environment: tested reference solution, 19 [parametrized pytest cases](https://leetcode-py.wisl.dev/practice/testing.md), and a playground notebook:

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

## Problem

Given a `m x n` `grid` filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path.

**Note:** You can only move either down or right at any point in time.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2020/11/05/minpath.jpg)

```
Input: grid = [[1,3,1],[1,5,1],[4,2,1]]
Output: 7
Explanation: Because the path 1 → 3 → 1 → 1 → 1 minimizes the sum.
```

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

### Constraints

* `m == grid.length`
* `n == grid[i].length`
* `1 <= m, n <= 200`
* `0 <= grid[i][j] <= 200`

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(m * n)
    # Space: O(1)
    def min_path_sum(self, grid: list[list[int]]) -> int:
        m = len(grid)
        n = len(grid[0])

        # Initialize first row
        for j in range(1, n):
            grid[0][j] += grid[0][j - 1]

        # Initialize first column
        for i in range(1, m):
            grid[i][0] += grid[i - 1][0]

        # Fill the rest of the grid
        for i in range(1, m):
            for j in range(1, n):
                grid[i][j] += min(grid[i - 1][j], grid[i][j - 1])

        return grid[-1][-1]
```

## Complexity

| Time | Space |
| - | - |
| O(m \* n) | O(1) |

## Tags

[NeetCode 250](https://leetcode-py.wisl.dev/catalog/neetcode-250.md), [NeetCode All](https://leetcode-py.wisl.dev/catalog/neetcode.md), [AlgoMaster 75](https://leetcode-py.wisl.dev/catalog/algo-master-75.md).


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