-
Notifications
You must be signed in to change notification settings - Fork 30
/
Minimum Path Sum.cpp
44 lines (29 loc) · 1.19 KB
/
Minimum Path Sum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
Solution by Rahul Surana
***********************************************************
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.
***********************************************************
*/
#include<bits/stdc++.h>
class Solution {
public:
// int ans = INT_MAX;
vector<vector<int>> dp;
vector<vector<int>> v;
int df(int i, int j, int n, int m, vector<vector<int>> &grid){
if(i>=n || j>= m) return 1e9;
if(dp[i][j] != -1) { return dp[i][j]; }
if(i==n-1 && j==m-1) { return grid[i][j]; }
int ans = 1e9;
ans = min(ans,df(i+1,j,n,m,grid)+grid[i][j]);
ans = min(ans,df(i,j+1,n,m,grid)+grid[i][j]);
return dp[i][j] = ans;
}
int minPathSum(vector<vector<int>>& grid) {
dp.resize(grid.size(),vector<int>(grid[0].size(),-1));
v.resize(grid.size(),vector<int>(grid[0].size(),0));
return df(0,0,grid.size(),grid[0].size(),grid);
// return ans;
}
};