-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuniquePathsII.cpp
More file actions
45 lines (34 loc) · 1.11 KB
/
uniquePathsII.cpp
File metadata and controls
45 lines (34 loc) · 1.11 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int m = obstacleGrid.size();
int n = obstacleGrid[0].size();
vector<int> curr(n,0);
if(obstacleGrid[0][0] || obstacleGrid[m-1][n-1]) return 0;
for(int i =0; i< n ; i++){
if(!obstacleGrid[0][i])
curr[i] = 1;
else
break;
}
bool sumCheck;
for(int i =1; i<m ;i++){
sumCheck = false;
if(obstacleGrid[i][0]){
curr[0] =0;
}
else sumCheck = true;
for(int j =1; j<n; j++){
if(obstacleGrid[i][j]){
curr[j] =0;
}
else{
curr[j] += curr[j-1];
if(curr[j]) sumCheck = true;
}
}
if(!sumCheck) return 0;
}
return curr[n-1];
}
};