[Medium] 64. Minimum Path Sum
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.
Thinking Process
- 2D DP Pattern: Classic grid DP problem with optimal substructure
- Define state: what subproblem does
dp[i](ordp[i][j]) represent? - Recurrence: how does the answer build from smaller indices?
- Base cases first; optimize space if only prior row/layer is needed.
Common Approaches
Typical techniques for this pattern:
| Approach | Time | Space | Notes |
|---|---|---|---|
| 1D DP (this problem) | O(n) | O(n) or O(1) | Linear recurrence |
| 2D DP | O(nm) | O(nm) or O(n) | Grid or two-sequence problems |
| State machine DP | O(n) | O(1) | Buy/sell, hold/not-hold states |
| Memoization (top-down) | Same as DP | O(n) | Recursive + cache |
Examples
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
Explanation: The path is 1 → 2 → 3 → 6.
Constraints
m == grid.lengthn == grid[i].length1 <= m, n <= 2000 <= grid[i][j] <= 200
Space Optimization
We can optimize space to O(min(m, n)) by using a 1D array:
class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
const int N = grid.size(), M = grid[0].size();
vector<int> dp(M);
// Initialize first row
dp[0] = grid[0][0];
for(int j = 1; j < M; j++) {
dp[j] = dp[j-1] + grid[0][j];
}
// Process remaining rows
for(int i = 1; i < N; i++) {
dp[0] += grid[i][0]; // First column
for(int j = 1; j < M; j++) {
dp[j] = grid[i][j] + min(dp[j], dp[j-1]);
}
}
return dp[M-1];
}
};
Key Insight: We only need the previous row to compute the current row, so we can use a 1D array and update it row by row.
Common Mistakes
- Single cell:
grid = [[5]]→ return5 - Single row:
grid = [[1,2,3]]→ return6(sum of row) - Single column:
grid = [[1],[2],[3]]→ return6(sum of column) -
All zeros:
grid = [[0,0],[0,0]]→ return0 - Wrong initialization: Not handling first row/column separately
- Index errors: Off-by-one errors in loops
- Wrong recurrence: Using
maxinstead ofmin - Base case errors: Not initializing
dp[0][0]correctly - Empty grid: Not handling empty grid case
Related Problems
- LC 62: Unique Paths - Count paths (similar structure)
- LC 63: Unique Paths II - With obstacles
- LC 120: Triangle - Triangular grid minimum path
- LC 174: Dungeon Game - Reverse DP approach
- LC 931: Minimum Falling Path Sum - 3-directional moves
Key Takeaways
- 2D DP Pattern: Classic grid DP problem with optimal substructure
- Base Cases: First row and column have only one path
- Recurrence: Choose minimum of top or left neighbor
- Space Optimization: Can reduce to O(min(m, n)) using rolling array
References
- LC 64: Minimum Path Sum on LeetCode
- LeetCode Discuss — LC 64: Minimum Path Sum
- LeetCode Editorial (may require premium)