-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlec10_26.cpp
More file actions
22 lines (20 loc) · 761 Bytes
/
Copy pathlec10_26.cpp
File metadata and controls
22 lines (20 loc) · 761 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
int n = grid.size();
int m = grid[0].size();
vector<vector<int>> dp(n, vector<int>(m, 0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (i == 0 && j == 0) {
dp[i][j] = grid[i][j]; // Base case: start position
} else {
int up = (i > 0) ? dp[i - 1][j] : INT_MAX;
int left = (j > 0) ? dp[i][j - 1] : INT_MAX;
dp[i][j] = grid[i][j] + min(up, left);
}
}
}
return dp[n - 1][m - 1];
}
};