File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int uniquePathsWithObstacles (vector<vector<int >>& obstacleGrid) {
4
+ // Start typing your C/C++ solution below
5
+ // DO NOT write int main() function
6
+
7
+ int m = obstacleGrid.size ();
8
+ if (m == 0 ) return 0 ;
9
+ int n = obstacleGrid[0 ].size ();
10
+
11
+ vector<vector<int >> dp (m, vector<int >(n, 0 ));
12
+
13
+ for (int i = 0 ; i < m; i++) {
14
+ if (obstacleGrid[i][0 ] == 0 )
15
+ dp[i][0 ] = 1 ;
16
+ else
17
+ break ;
18
+ }
19
+ for (int j = 0 ; j < n; j++) {
20
+ if (obstacleGrid[0 ][j] == 0 )
21
+ dp[0 ][j] = 1 ;
22
+ else
23
+ break ;
24
+ }
25
+ for (int i = 1 ; i < m; i++) {
26
+ for (int j = 1 ; j < n; j++) {
27
+ if (obstacleGrid[i][j] == 0 )
28
+ dp[i][j] = dp[i-1 ][j] + dp[i][j-1 ];
29
+ }
30
+ }
31
+ return dp[m-1 ][n-1 ];
32
+
33
+ }
34
+ };
You can’t perform that action at this time.
0 commit comments