Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
670 B
3
Indexable
Never
class Solution {
public:
    int solve(vector<vector<int>>& grid,int x,int y,int n,int m,vector<vector<int>>&dp){
        if(x<0 || x>=m || y<0 || y>=n) return 1e9;
        if(x==n-1 && y==m-1) return grid[x][y];
        
        if(dp[x][y]!=-1) return dp[x][y];
        int l = solve(grid,x,y+1,n,m,dp); //going right
        int d = solve(grid,x+1,y,n,m,dp);  // going down

        return dp[x][y]=grid[x][y]+min(l,d);

    }
    int minPathSum(vector<vector<int>>& grid) {
        int n=grid.size(); //row
        int m=grid[0].size(); //column
        vector<vector<int>>dp(n,vector<int>(m,-1));
        return solve(grid,0,0,n,m,dp);
    }
};