-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlec10_23.cpp
More file actions
28 lines (24 loc) · 1.01 KB
/
Copy pathlec10_23.cpp
File metadata and controls
28 lines (24 loc) · 1.01 KB
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
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& mat) {
int n = mat.size();
int m = mat[0].size();
vector<int> prev(m, 0); // Should be of size m (columns)
for (int i = 0; i < n; i++) { // Iterate row-wise
vector<int> curr(m, 0);
for (int j = 0; j < m; j++) { // Iterate column-wise
if (mat[i][j] == 1) {
curr[j] = 0; // Obstacle, no path
} else if (i == 0 && j == 0) {
curr[j] = 1; // Start cell
} else {
int up = (i > 0) ? prev[j] : 0;
int left = (j > 0) ? curr[j - 1] : 0;
curr[j] = up + left;
}
}
prev = curr; // Move current row to previous row
}
return prev[m - 1]; // Return last cell of last row
}
};