Minimum Path Sum
Return the minimum sum path from top-left to bottom-right.
Examples
Input: [[1,3,1],[1,5,1],[4,2,1]]
Output: 7
Input: [[1,2,3],[4,5,6]]
Output: 12
Hints
Use dynamic programming to build a 2D table where `dp[i][j]` represents the minimum path sum to reach cell `(i, j)` from `(0, 0)`.
For each cell `(i, j)`, compute `dp[i][j]` as `grid[i][j] + min(dp[i-1][j], dp[i][j-1])`, handling boundary conditions (first row/column) separately.
Optimize space by using a 1D array (rolling array) to store only the current and previous row, reducing space complexity from O(mn) to O(n).
Related Problems
Minimum Path Sum
Return the minimum sum path from top-left to bottom-right.