-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimumPathSum.cpp
More file actions
28 lines (27 loc) · 841 Bytes
/
minimumPathSum.cpp
File metadata and controls
28 lines (27 loc) · 841 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
// Source: https://leetcode.com/problems/minimum-path-sum/
// Author: Miao Zhang
// Date: 2021-01-13
class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
int m = grid.size();
int n = grid[0].size();
vector<vector<int>> dp(m, vector<int>(n, 0));
int tmp;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (i - 1 >= 0 && j - 1 >= 0) {
tmp = min(dp[i - 1][j], dp[i][j - 1]);
} else if (i - 1 >= 0) {
tmp = dp[i - 1][j];
} else if (j - 1 >= 0) {
tmp = dp[i][j - 1];
} else {
tmp = 0;
}
dp[i][j] = grid[i][j] + tmp;
}
}
return dp[m - 1][n - 1];
}
};