File tree Expand file tree Collapse file tree 1 file changed +7
-6
lines changed
scripts/algorithms/U/Unique Paths II Expand file tree Collapse file tree 1 file changed +7
-6
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 111 ms (Top 27.88%) | Memory: 42.8 MB (Top 44.48%)
1
2
var uniquePathsWithObstacles = function ( obstacleGrid ) {
2
3
const m = obstacleGrid . length ;
3
4
const n = obstacleGrid [ 0 ] . length ;
4
-
5
+
5
6
const results = new Array ( m ) . fill ( 0 ) ;
6
7
for ( let i = 0 ; i < m ; i ++ ) {
7
8
results [ i ] = new Array ( n ) . fill ( 0 ) ;
8
9
}
9
-
10
+
10
11
for ( let i = 0 ; i < m ; i ++ ) {
11
12
for ( let j = 0 ; j < n ; j ++ ) {
12
13
if ( i === 0 && j === 0 ) {
13
14
results [ 0 ] [ 0 ] = obstacleGrid [ 0 ] [ 0 ] ? 0 : 1 ;
14
15
} else if ( ! obstacleGrid [ i ] [ j ] ) {
15
16
const up = i === 0 ? 0 : results [ i - 1 ] [ j ] ;
16
17
const left = j === 0 ? 0 : results [ i ] [ j - 1 ] ;
17
-
18
+
18
19
results [ i ] [ j ] = up + left ;
19
- }
20
+ }
20
21
}
21
22
}
22
-
23
+
23
24
return results [ m - 1 ] [ n - 1 ] ;
24
- }
25
+ }
You can’t perform that action at this time.
0 commit comments