> ## 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 Falling Path Sum Python Solution

> Tested Python solution for LeetCode 931 with 13 pytest cases. Generate a practice environment with lcpy.

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

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

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

## Problem

\<p>Given an \<code>n x n\</code> array of integers \<code>matrix\</code>, return \<em>the minimum sum of any \<strong>falling path\</strong> through\</em> \<code>matrix\</code>.\</p>

\<p>A \<strong>falling path\</strong> starts at any element in the first row and chooses the element in the next row that is either directly below or diagonally left/right. Specifically, the next element from position \<code>(row, col)\</code> will be \<code>(row + 1, col - 1)\</code>, \<code>(row + 1, col)\</code>, or \<code>(row + 1, col + 1)\</code>.\</p>

### Examples

![Example 1](https://assets.leetcode.com/uploads/2021/11/03/failing1-grid.jpg)

```
Input: matrix = [[2,1,3],[6,5,4],[7,8,9]]
Output: 13
Explanation: There are two falling paths with a minimum sum as shown.
```

![Example 2](https://assets.leetcode.com/uploads/2021/11/03/failing2-grid.jpg)

```
Input: matrix = [[-19,57],[-40,-5]]
Output: -59
Explanation: The falling path with a minimum sum is shown.
```

### Constraints

* n == matrix.length == matrix\[i].length
* 1 \<= n \<= 100
* -100 \<= matrix\[i]\[j] \<= 100

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n^2)
    # Space: O(n)
    def min_falling_path_sum(self, matrix: list[list[int]]) -> int:
        n = len(matrix)
        dp = matrix[0][:]
        for row in range(1, n):
            new_dp = [0] * n
            for col in range(n):
                best = dp[col]
                if col > 0:
                    best = min(best, dp[col - 1])
                if col < n - 1:
                    best = min(best, dp[col + 1])
                new_dp[col] = matrix[row][col] + best
            dp = new_dp
        return min(dp)
```

## Complexity

| Time | Space |
| - | - |
| O(n^2) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


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