> ## 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 II Python Solution

> Tested Python solution for LeetCode 1289 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 1289, [Hard](/catalog/hard). 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-ii/description/).

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

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

## Problem

Given an n x n integer matrix grid, return the minimum sum of a falling path with non-zero shifts.

A falling path with non-zero shifts is a choice of exactly one element from each row of grid such that no two elements chosen in adjacent rows are in the same column.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2021/08/10/falling-grid.jpg)

```
Input: grid = [[1,2,3],[4,5,6],[7,8,9]]
Output: 13
Explanation:
The possible falling paths are:
[1,5,9], [1,5,7], [1,6,7], [1,6,8],
[2,4,8], [2,4,9], [2,6,7], [2,6,8],
[3,4,8], [3,4,9], [3,5,7], [3,5,9]
The falling path with the smallest sum is [1,5,7], so the answer is 13.
```

```
Input: grid = [[7]]
Output: 7
```

### Constraints

* n == grid.length == grid\[i].length
* 1 \<= n \<= 200
* -99 \<= grid\[i]\[j] \<= 99

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    def min_falling_path_sum(self, grid: list[list[int]]) -> int:
        n = len(grid)
        dp = grid[0][:]
        for i in range(1, n):
            first, second = sorted(dp)[:2]
            dp = [grid[i][j] + (second if dp[j] == first else first) for j in range(n)]
        return min(dp)
```

## Complexity

| Time | Space |
| - | - |
| - | - |

## Tags

[NeetCode All](/catalog/neetcode).


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