@@ -18,33 +18,59 @@ const pacificAtlantic = (heights) => {
18
18
const pacificVisited = Array . from ( { length : m } , ( ) => new Array ( n ) . fill ( false ) ) ;
19
19
const atlanticVisited = Array . from ( { length : m } , ( ) => new Array ( n ) . fill ( false ) ) ;
20
20
21
+ // console.log("pacificVisited")
22
+ // console.log(pacificVisited)
23
+ // console.log("atlanticVisited")
24
+ // console.log(atlanticVisited)
25
+
21
26
// Add border cells to the respective queues and mark them as visited
22
27
for ( let i = 0 ; i < m ; i ++ ) {
23
28
pacificQueue . push ( [ i , 0 ] ) ;
24
29
atlanticQueue . push ( [ i , n - 1 ] ) ;
25
30
pacificVisited [ i ] [ 0 ] = true ;
26
31
atlanticVisited [ i ] [ n - 1 ] = true ;
27
32
}
33
+
28
34
for ( let j = 0 ; j < n ; j ++ ) {
29
35
pacificQueue . push ( [ 0 , j ] ) ;
30
36
atlanticQueue . push ( [ m - 1 , j ] ) ;
31
37
pacificVisited [ 0 ] [ j ] = true ;
32
38
atlanticVisited [ m - 1 ] [ j ] = true ;
33
39
}
34
40
41
+ // NOTE: Form grids that wraps the sides that touch the ocean
42
+ console . log ( "pacificVisited" )
43
+ console . log ( pacificVisited )
44
+ console . log ( "pacificQueue" )
45
+ console . log ( pacificQueue )
46
+
47
+ console . log ( "atlanticVisited" )
48
+ console . log ( atlanticVisited )
49
+ console . log ( "atlanticQueue" )
50
+ console . log ( atlanticQueue )
51
+
35
52
// Perform BFS from the Pacific Ocean
36
53
bfs ( pacificQueue , pacificVisited , heights ) ;
37
54
38
55
// Perform BFS from the Atlantic Ocean
39
56
bfs ( atlanticQueue , atlanticVisited , heights ) ;
40
57
58
+ console . log ( "-----> After BFS" )
59
+ console . log ( "pacificVisited" )
60
+ console . log ( pacificVisited )
61
+ console . log ( "pacificQueue" )
62
+ console . log ( pacificQueue )
63
+
64
+ console . log ( "atlanticVisited" )
65
+ console . log ( atlanticVisited )
66
+ console . log ( "atlanticQueue" )
67
+ console . log ( atlanticQueue )
68
+
41
69
// Find cells that can flow to both oceans
42
70
const result = [ ] ;
43
71
for ( let i = 0 ; i < m ; i ++ ) {
44
72
for ( let j = 0 ; j < n ; j ++ ) {
45
- if ( pacificVisited [ i ] [ j ] && atlanticVisited [ i ] [ j ] ) {
46
- result . push ( [ i , j ] ) ;
47
- }
73
+ if ( pacificVisited [ i ] [ j ] && atlanticVisited [ i ] [ j ] ) result . push ( [ i , j ] ) ;
48
74
}
49
75
}
50
76
@@ -74,3 +100,21 @@ const bfs = (queue, visited, heights) => {
74
100
}
75
101
}
76
102
} ;
103
+
104
+ // let x = pacificAtlantic([
105
+ // [1, 2, 3],
106
+ // [6, 5, 4],
107
+ // [2, 7, 2],
108
+ // ]);
109
+
110
+ let x = pacificAtlantic ( [
111
+ [ 1 , 2 , 2 , 3 , 5 ] ,
112
+ [ 3 , 2 , 3 , 4 , 4 ] ,
113
+ [ 2 , 4 , 5 , 3 , 1 ] ,
114
+ [ 6 , 7 , 1 , 4 , 5 ] ,
115
+ [ 5 , 1 , 1 , 2 , 4 ]
116
+ ] ) ;
117
+
118
+ // let x = pacificAtlantic([[1]]);
119
+ console . log ( "Result" ) ;
120
+ console . log ( x ) ;
0 commit comments