-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path64.cpp
More file actions
16 lines (16 loc) · 682 Bytes
/
Copy path64.cpp
File metadata and controls
16 lines (16 loc) · 682 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
vector<vector<int>> matrix (grid.size(), vector<int> (grid[0].size(), 0));
int min_val = 0;
for(int i=0; i<grid.size(); i++){
for(int j=0; j<grid[0].size(); j++){
if(i-1 < 0 && j-1 >= 0) matrix[i][j] = matrix[i][j-1] + grid[i][j];
else if(i-1 >= 0 && j-1 < 0) matrix[i][j] = matrix[i-1][j] + grid[i][j];
else if(i-1 < 0 && j-1 < 0) matrix[i][j] = grid[i][j];
else if(i-1 >= 0 && j-1 >= 0) matrix[i][j] = min(matrix[i][j-1], matrix[i-1][j]) + grid[i][j];
}
}
return matrix[grid.size()-1][grid[0].size()-1];
}
};