-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path064_Minimum_Path_Sum.cpp
More file actions
35 lines (35 loc) · 990 Bytes
/
Copy path064_Minimum_Path_Sum.cpp
File metadata and controls
35 lines (35 loc) · 990 Bytes
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
class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
int m = grid.size();
if(m == 0)
return 0;
int n = grid[0].size();
dp = new int*[m];
for(int i = 0; i < m; i++) {
dp[i] = new int[n];
memset(dp[i], 0, sizeof(int) * n);
}
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++) {
int a = INT_MAX, b = INT_MAX;
if(ck(i - 1, j, m, n))
a = dp[i - 1][j];
if(ck(i, j - 1, m, n))
b = dp[i][j - 1];
if(a == INT_MAX && b == INT_MAX)
a = b = 0;
dp[i][j] = grid[i][j] + min(a, b);
}
return dp[m - 1][n - 1];
}
private:
int **dp;
bool ck(int x, int y, int m, int n) {
if(x < 0 || x >= m)
return false;
if(y < 0 || y >= n)
return false;
return true;
}
};