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] <= 200
Approach - Bottom Up 1D DP
- put nth element as infinity and base condition rest is easy
dp[j]means what is the value atdp[i-1][j]top left anddp[j+1]is bottom right
class Solution {
public int minPathSum(int[][] grid) {
int r = grid.length, c = grid[0].length;
int[] dp = new int[c+1];
Arrays.fill(dp, Integer.MAX_VALUE);
dp[c-1] = 0;
for (int i = r - 1; i >= 0; i--) {
for (int j = c - 1; j >=0; j--) {
dp[j] = grid[i][j] + Math.min(dp[j],dp[j+1]);
}
}
return dp[0];
}
}