Skip to content

Commit 13896e5

Browse files
committed
Time: 6 ms (19.20%), Space: 10.1 MB (59.39%) - LeetHub
1 parent c340f51 commit 13896e5

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public:
3+
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
4+
vector<vector<int>> array(obstacleGrid.size(),
5+
vector<int>(obstacleGrid[0].size()));
6+
for (int i = 0; i < array.size(); i++) {
7+
for (int j = 0; j < array[0].size(); j++) {
8+
if (i == 0 && j == 0) {
9+
if (obstacleGrid[i][j] != 1) {
10+
array[i][j] = 1;
11+
} else {
12+
array[i][j] = 0;
13+
}
14+
} else if (i == 0) {
15+
if (obstacleGrid[i][j] != 1) {
16+
array[i][j] = array[i][j-1];
17+
} else {
18+
array[i][j] = 0;
19+
}
20+
} else if (j == 0) {
21+
if (obstacleGrid[i][j] != 1) {
22+
array[i][j] = array[i-1][j];
23+
} else {
24+
array[i][j] = 0;
25+
}
26+
} else if(obstacleGrid[i][j]==0){
27+
array[i][j] = array[i - 1][j] + array[i][j - 1];
28+
}else{
29+
array[i][j]=0;
30+
}
31+
}
32+
}
33+
return array[array.size() - 1][array[0].size() - 1];
34+
}
35+
};

0 commit comments

Comments
 (0)