minimun-path-sum
Problem
Problem Description
Solution
This is a simple DP problem, current sum can either come from up or left, since it can only move down or right.
For each sum, grid[r][c] = min(grid[r-1][c], grid[r][c-1]) + grid[r][c];
then you find the min path to current position (r,c).
after scan the whole grid, return bottom right value.
NOTE: Below implementation, modified original input grid, from algorithm point of view to reduce space complexity to O(1)
. But from real project, modified original input not recommended, use extra space.
For example:
Complexity Analysis
Time Complexity: O(N*M)
Space Complexity: O(1)
N - number of grid rows
M - number of grid columns
Code
Last updated