Minimum Path Sum
Given a m * n grid filled with non-negative numbers, find a path from top left to bottom right, which minizes the sum of all numbers along its path.
- Moves can only be taken down or right at any point in time.
Example:
1 | Input: grid = [[1, 3, 1], [1, 5, 1], [4, 2, 1]]; |
Constraints:
- 1 <= m, n <= 200
- 0 <= grid[i, j] <= 200
Solution
1 | class MinimumPathSum { |
Explanation:
Using a dp array to indicate the current minimum path sum. And this sum can only be the sum of the up dp point and current grid point or the left dp point and current grid point. The dp function can be defined as follow:
1 | dp[i, j] := min(dp[i - 1, j] + grid[i, j], dp[i, j - 1] + grid[i, j]) |