Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
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.
Example 1:
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.
Example 2:
Input: grid = [[7]] Output: 7
Constraints:
n == grid.length == grid[i].length1 <= n <= 200-99 <= grid[i][j] <= 99Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[[1,2,3],[4,5,6],[7,8,9]]
[[7]]
minimum-falling-path-sum)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1289: Minimum Falling Path Sum II
class Solution {
public int minFallingPathSum(int[][] grid) {
int n = grid.length;
int[] f = new int[n];
final int inf = 1 << 30;
for (int[] row : grid) {
int[] g = row.clone();
for (int i = 0; i < n; ++i) {
int t = inf;
for (int j = 0; j < n; ++j) {
if (j != i) {
t = Math.min(t, f[j]);
}
}
g[i] += (t == inf ? 0 : t);
}
f = g;
}
return Arrays.stream(f).min().getAsInt();
}
}
// Accepted solution for LeetCode #1289: Minimum Falling Path Sum II
func minFallingPathSum(grid [][]int) int {
f := make([]int, len(grid))
const inf = math.MaxInt32
for _, row := range grid {
g := slices.Clone(row)
for i := range f {
t := inf
for j := range row {
if j != i {
t = min(t, f[j])
}
}
if t != inf {
g[i] += t
}
}
f = g
}
return slices.Min(f)
}
# Accepted solution for LeetCode #1289: Minimum Falling Path Sum II
class Solution:
def minFallingPathSum(self, grid: List[List[int]]) -> int:
n = len(grid)
f = [0] * n
for row in grid:
g = row[:]
for i in range(n):
g[i] += min((f[j] for j in range(n) if j != i), default=0)
f = g
return min(f)
// Accepted solution for LeetCode #1289: Minimum Falling Path Sum II
impl Solution {
pub fn min_falling_path_sum(grid: Vec<Vec<i32>>) -> i32 {
let n = grid.len();
let mut f = vec![0; n];
let inf = i32::MAX;
for row in grid {
let mut g = row.clone();
for i in 0..n {
let mut t = inf;
for j in 0..n {
if j != i {
t = t.min(f[j]);
}
}
g[i] += if t == inf { 0 } else { t };
}
f = g;
}
*f.iter().min().unwrap()
}
}
// Accepted solution for LeetCode #1289: Minimum Falling Path Sum II
function minFallingPathSum(grid: number[][]): number {
const n = grid.length;
const f: number[] = Array(n).fill(0);
for (const row of grid) {
const g = [...row];
for (let i = 0; i < n; ++i) {
let t = Infinity;
for (let j = 0; j < n; ++j) {
if (j !== i) {
t = Math.min(t, f[j]);
}
}
g[i] += t === Infinity ? 0 : t;
}
f.splice(0, n, ...g);
}
return Math.min(...f);
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.