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.
Move from brute-force thinking to an efficient approach using array strategy.
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.
Example 1:
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.
Example 2:
Input: grid = [[1,2,3],[4,5,6]] Output: 12
Constraints:
m == grid.lengthn == grid[i].length1 <= m, n <= 2000 <= grid[i][j] <= 200Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[[1,3,1],[1,5,1],[4,2,1]]
[[1,2,3],[4,5,6]]
unique-paths)dungeon-game)cherry-pickup)minimum-path-cost-in-a-grid)maximum-number-of-points-with-cost)class Solution {
public int minPathSum(int[][] grid) {
int m = grid.length, n = grid[0].length;
int[] dp = new int[n];
dp[0] = grid[0][0];
for (int c = 1; c < n; c++) {
dp[c] = dp[c - 1] + grid[0][c];
}
for (int r = 1; r < m; r++) {
dp[0] += grid[r][0];
for (int c = 1; c < n; c++) {
dp[c] = Math.min(dp[c], dp[c - 1]) + grid[r][c];
}
}
return dp[n - 1];
}
}
func minPathSum(grid [][]int) int {
m, n := len(grid), len(grid[0])
dp := make([]int, n)
dp[0] = grid[0][0]
for c := 1; c < n; c++ {
dp[c] = dp[c-1] + grid[0][c]
}
for r := 1; r < m; r++ {
dp[0] += grid[r][0]
for c := 1; c < n; c++ {
if dp[c-1] < dp[c] {
dp[c] = dp[c-1] + grid[r][c]
} else {
dp[c] = dp[c] + grid[r][c]
}
}
}
return dp[n-1]
}
class Solution:
def minPathSum(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
dp = [0] * n
dp[0] = grid[0][0]
for c in range(1, n):
dp[c] = dp[c - 1] + grid[0][c]
for r in range(1, m):
dp[0] += grid[r][0]
for c in range(1, n):
dp[c] = min(dp[c], dp[c - 1]) + grid[r][c]
return dp[-1]
impl Solution {
pub fn min_path_sum(grid: Vec<Vec<i32>>) -> i32 {
let m = grid.len();
let n = grid[0].len();
let mut dp = vec![0; n];
dp[0] = grid[0][0];
for c in 1..n {
dp[c] = dp[c - 1] + grid[0][c];
}
for r in 1..m {
dp[0] += grid[r][0];
for c in 1..n {
dp[c] = dp[c].min(dp[c - 1]) + grid[r][c];
}
}
dp[n - 1]
}
}
function minPathSum(grid: number[][]): number {
const m = grid.length;
const n = grid[0].length;
const dp: number[] = Array(n).fill(0);
dp[0] = grid[0][0];
for (let c = 1; c < n; c++) {
dp[c] = dp[c - 1] + grid[0][c];
}
for (let r = 1; r < m; r++) {
dp[0] += grid[r][0];
for (let c = 1; c < n; c++) {
dp[c] = Math.min(dp[c], dp[c - 1]) + grid[r][c];
}
}
return dp[n - 1];
}
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.