File tree 1 file changed +65
-0
lines changed
1 file changed +65
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[][] } grid
3
+ * @return {number }
4
+ */
5
+ const maximumMinimumPath = function ( grid ) {
6
+ const m = grid . length ,
7
+ n = grid [ 0 ] . length
8
+ const dirs = [
9
+ [ 0 , 1 ] ,
10
+ [ 0 , - 1 ] ,
11
+ [ 1 , 0 ] ,
12
+ [ - 1 , 0 ] ,
13
+ ]
14
+
15
+ const set = new Set ( )
16
+
17
+ let ceil = Math . min ( grid [ 0 ] [ 0 ] , grid [ m - 1 ] [ n - 1 ] )
18
+ for ( let i = 0 ; i < m ; i ++ ) {
19
+ for ( let j = 0 ; j < n ; j ++ ) {
20
+ if ( grid [ i ] [ j ] <= ceil ) {
21
+ set . add ( grid [ i ] [ j ] )
22
+ }
23
+ }
24
+ }
25
+ const arr = Array . from ( set )
26
+ arr . sort ( ( a , b ) => a - b )
27
+ let l = 0 ,
28
+ r = arr . length - 1
29
+ while ( l < r ) {
30
+ const mid = r - ( ( r - l ) >> 1 )
31
+ if ( valid ( arr [ mid ] ) ) {
32
+ l = mid
33
+ } else {
34
+ r = mid - 1
35
+ }
36
+ }
37
+
38
+ return arr [ l ]
39
+
40
+ function valid ( v ) {
41
+ const memo = Array . from ( { length : m } , ( ) => Array ( n ) . fill ( 0 ) )
42
+
43
+ function dfs ( x , y ) {
44
+ if ( x === m - 1 && y === n - 1 ) return true
45
+ memo [ x ] [ y ] = 1
46
+ for ( const [ dx , dy ] of dirs ) {
47
+ const nx = x + dx ,
48
+ ny = y + dy
49
+ if (
50
+ nx >= 0 &&
51
+ nx < m &&
52
+ ny >= 0 &&
53
+ ny < n &&
54
+ memo [ nx ] [ ny ] === 0 &&
55
+ grid [ nx ] [ ny ] >= v &&
56
+ dfs ( nx , ny )
57
+ )
58
+ return true
59
+ }
60
+ return false
61
+ }
62
+
63
+ return dfs ( 0 , 0 )
64
+ }
65
+ }
You can’t perform that action at this time.
0 commit comments