File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int uniquePathsWithObstacles (int [][] obstacleGrid ) {
3+ if (obstacleGrid == null || obstacleGrid .length == 0 ) {
4+ return 0 ;
5+ }
6+
7+ int rows = obstacleGrid .length , cols = obstacleGrid [0 ].length ;
8+ int [][] dp = new int [rows ][cols ];
9+
10+ for (int i = 0 ; i < rows ; i ++) {
11+ if (obstacleGrid [i ][0 ] == 0 ) {
12+ dp [i ][0 ] = 1 ;
13+ } else {
14+ while (i < rows ) {
15+ dp [i ][0 ] = 0 ;
16+ ++i ;
17+ }
18+ }
19+ }
20+
21+ for (int j = 0 ; j < cols ; j ++) {
22+ if (obstacleGrid [0 ][j ] == 0 ) {
23+ dp [0 ][j ] = 1 ;
24+ } else {
25+ while (j < cols ) {
26+ dp [0 ][j ] = 0 ;
27+ ++j ;
28+ }
29+ }
30+ }
31+
32+ for (int i = 1 ; i < rows ; i ++) {
33+ for (int j = 1 ; j < cols ; j ++) {
34+ if (obstacleGrid [i ][j ] == 1 ) {
35+ dp [i ][j ] = 0 ;
36+ continue ;
37+ }
38+ dp [i ][j ] = dp [i - 1 ][j ] + dp [i ][j - 1 ];
39+ }
40+ }
41+
42+ return dp [rows - 1 ][cols - 1 ];
43+ }
44+ }
You can’t perform that action at this time.
0 commit comments