-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path110.h
More file actions
21 lines (21 loc) · 656 Bytes
/
110.h
File metadata and controls
21 lines (21 loc) · 656 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
/**
* @param grid: a list of lists of integers.
* @return: An integer, minimizes the sum of all numbers along its path
*/
int minPathSum(vector<vector<int> > &grid) {
// write your code here
int r = grid.size();
int c = grid[0].size();
for(int i = 0;i < r;i++){
for (int j = 0;j < c;j++){
if(i == 0 && j == 0) continue;
int left = i-1 < 0 ? INT_MAX : grid[i-1][j];
int up = j-1 < 0 ? INT_MAX : grid[i][j-1];
grid[i][j] += min(left,up);
}
}
return grid[r-1][c-1];
}
};