Given a
Note: You can only move either down or right at any point in time.
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
Approach
Java
public class MinimumPathSum {public static void main(String[] args) {int[][] grid = { { 1, 3, 1 }, { 1, 5, 1 }, { 4, 2, 1 } };System.out.println(minPathSum(grid));}static int minPathSum(int[][] grid) {int n = grid.length;if (n == 0)return 0;int m = grid[0].length;int dp[][] = new int[n][m];dp[0][0] = grid[0][0];for (int i = 1; i < m; i++)dp[0][i] = dp[0][i - 1] + grid[0][i];for (int i = 1; i < n; i++)dp[i][0] = dp[i - 1][0] + grid[i][0];for (int i = 1; i < n; i++) {for (int j = 1; j < m; j++) {// update the valuedp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j];}}return dp[n - 1][m - 1];}}
C++
#include <bits/stdc++.h>using namespace std;int minPathSum(vector<vector<int>>& grid){int n=grid.size();if(n==0)return 0;int m=grid[0].size();int dp[n][m];dp[0][0]=grid[0][0];for(int i=1;i<m;i++)dp[0][i]=dp[0][i-1]+grid[0][i];for(int i=1;i<n;i++)dp[i][0]=dp[i-1][0]+grid[i][0];for(int i=1;i<n;i++){for(int j=1;j<m;j++){//update the valuedp[i][j]=min(dp[i-1][j],dp[i][j-1])+grid[i][j];}}return dp[n-1][m-1];}int main(){vector<vector<int>> grid ={{1,3,1},{1,5,1},{4,2,1}};cout<<minPathSum(grid);return 0;}
No comments:
Post a Comment