1- //////////////////////////////////////////////////////////////////////////////
2- // Depth First Search (DFS)
3- // Time: O(m*n)
4- // Space: O(m*n)
5- // You can implement either Depth First Search (DFS) or Breadth First Search
6- // (BFS). The only noticeable impact is the performance cost of the BFS queue
7- // is higher than the DFS call stack.
8- //////////////////////////////////////////////////////////////////////////////
9-
101/**
2+ * https://leetcode.com/problems/surrounded-regions/
3+ * Time O(ROWS * COLS) | Space O(ROWS * COLS)
114 * @param {character[][] } board
125 * @return {void } Do not return anything, modify board in-place instead.
136 */
14- function solve ( board ) {
15- const rowLen = board . length ;
16- const colLen = board [ 0 ] . length ;
17- const lastRow = rowLen - 1 ;
18- const lastCol = colLen - 1 ;
19-
20- for ( let r = 0 ; r < rowLen ; ++ r ) {
21- markSeen ( r , 0 ) ;
22- markSeen ( r , lastCol ) ;
7+ var solve = function solve ( board ) {
8+ searchRows ( board ) ; /* Time O(ROWS * COLS) | Space O(ROWS * COLS) */
9+ searchCols ( board ) ; /* Time O(ROWS * COLS) | Space O(ROWS * COLS) */
10+ searchGrid ( board ) ; /* Time O(ROWS * COLS) | Space O(ROWS * COLS) */
11+ }
12+
13+ var searchRows = ( board ) => {
14+ const [ rows , cols ] = [ board . length , board [ 0 ] . length ] ;
15+
16+ for ( let row = 0 ; row < rows ; row ++ ) { /* Time O(ROWS) */
17+ dfs ( board , row , rows , 0 , cols ) ; /* Space O(ROWS) */
18+ dfs ( board , row , rows , ( cols - 1 ) , cols ) ; /* Space O(ROWS) */
2319 }
24- for ( let c = 1 ; c < lastCol ; ++ c ) {
25- markSeen ( 0 , c ) ;
26- markSeen ( lastRow , c ) ;
20+ }
21+
22+ var searchCols = ( board ) => {
23+ const [ rows , cols ] = [ board . length , board [ 0 ] . length ] ;
24+
25+ for ( let col = 1 ; col < ( cols - 1 ) ; col ++ ) { /* Time O(COLS) */
26+ dfs ( board , 0 , rows , col , cols ) ; /* Space O(COLS) */
27+ dfs ( board , ( rows - 1 ) , rows , col , cols ) ; /* Space O(COLS) */
2728 }
29+ }
2830
29- for ( let r = 0 ; r < rowLen ; ++ r ) {
30- for ( let c = 0 ; c < colLen ; ++ c ) {
31- switch ( board [ r ] [ c ] ) {
32- case 'O' :
33- board [ r ] [ c ] = 'X' ;
34- break ;
35- case 'A' :
36- board [ r ] [ c ] = 'O' ;
37- break ;
38- }
31+ var searchGrid = ( board ) => {
32+ const [ rows , cols ] = [ board . length , board [ 0 ] . length ] ;
33+
34+ for ( let row = 0 ; row < rows ; row ++ ) { /* Time O(ROWS) */
35+ for ( let col = 0 ; col < cols ; col ++ ) { /* Time O(COLS) */
36+ const isO = board [ row ] [ col ] === 'O' ;
37+ if ( isO ) board [ row ] [ col ] = 'X' ;
38+
39+ const isStar = board [ row ] [ col ] === '*' ;
40+ if ( isStar ) board [ row ] [ col ] = 'O' ;
3941 }
4042 }
43+ }
4144
42- /**
43- * @param {number } r
44- * @param {number } c
45- * @return {void }
46- */
47- function markSeen ( r , c ) {
48- if ( ! inBounds ( r , c ) || board [ r ] [ c ] !== 'O' ) {
49- return ;
50- }
45+ const dfs = ( board , row , rows , col , cols ) => {
46+ const isBaseCase = board [ row ] [ col ] !== 'O' ;
47+ if ( isBaseCase ) return ;
48+
49+ board [ row ] [ col ] = '*' ;
50+
51+ for ( const [ _row , _col ] of getNeighbors ( row , rows , col , cols ) ) {
52+ dfs ( board , _row , rows , _col , cols ) ; /* Time O(HEIGHT) | Space O(HEIGHT) */
53+ }
54+ }
55+
56+ var getNeighbors = ( row , rows , col , cols ) => [ [ 0 , 1 ] , [ 0 , - 1 ] , [ 1 , 0 ] , [ - 1 , 0 ] ]
57+ . map ( ( [ _row , _col ] ) => [ ( row + _row ) , ( col + _col ) ] )
58+ . filter ( ( [ _row , _col ] ) => ( 0 <= _row ) && ( _row < rows ) && ( 0 <= _col ) && ( _col < cols ) )
59+
60+
61+ /**
62+ * https://leetcode.com/problems/surrounded-regions/
63+ * Time O(ROWS * COLS) | Space O(ROWS * COLS)
64+ * @param {character[][] } board
65+ * @return {void } Do not return anything, modify board in-place instead.
66+ */
67+ var solve = function solve ( board , queue = new Queue ( [ ] ) ) {
68+ searchRows ( board , queue ) ; /* Time O(ROWS + COLS) | Space O(ROWS + COLS) */
69+ searchCols ( board , queue ) ; /* Time O(ROWS + COLS) | Space O(ROWS + COLS) */
70+ bfs ( board , queue ) ; /* Time O(ROWS * COLS) | Space O(ROWS * COLS) */
71+ searchGrid ( board ) ; /* Time O(ROWS * COLS) */
72+ }
5173
52- board [ r ] [ c ] = 'A' ;
74+ var searchRows = ( board , queue ) => {
75+ const [ rows , cols ] = [ board . length , board [ 0 ] . length ]
5376
54- markSeen ( r - 1 , c ) ;
55- markSeen ( r + 1 , c ) ;
56- markSeen ( r , c - 1 ) ;
57- markSeen ( r , c + 1 ) ;
77+ for ( let row = 0 ; row < rows ; row ++ ) { /* Time O(ROWS) */
78+ queue . enqueue ( [ row , 0 ] ) ; /* Space O(ROWS) */
79+ queue . enqueue ( [ row , ( cols - 1 ) ] ) ; /* Space O(ROWS) */
5880 }
81+ }
82+
83+ var searchCols = ( board , queue ) => {
84+ const [ rows , cols ] = [ board . length , board [ 0 ] . length ]
5985
60- /**
61- * @param {number } r
62- * @param {number } c
63- * @return {boolean }
64- */
65- function inBounds ( r , c ) {
66- return r >= 0 && c >= 0 && r < rowLen && c < colLen ;
86+ for ( let col = 0 ; col < ( cols - 1 ) ; col ++ ) { /* Time O(COLS) */
87+ queue . enqueue ( [ 0 , col ] ) ; /* Space O(COLS) */
88+ queue . enqueue ( [ ( rows - 1 ) , col ] ) ; /* Space O(COLS) */
6789 }
6890}
91+
92+ var bfs = ( board , queue ) => {
93+ const [ rows , cols ] = [ board . length , board [ 0 ] . length ] ;
94+
95+ while ( ! queue . isEmpty ( ) ) {
96+ for ( let i = ( queue . size ( ) - 1 ) ; 0 <= i ; i -- ) { /* Time O(WIDTH) */
97+ const [ row , col ] = queue . dequeue ( ) ;
98+
99+ const isBaseCase = board [ row ] [ col ] !== 'O' ;
100+ if ( isBaseCase ) continue ;
101+
102+ board [ row ] [ col ] = '*' ;
103+
104+ for ( const [ _row , _col ] of getNeighbors ( row , rows , col , cols ) ) {
105+ queue . enqueue ( [ _row , _col ] ) ; /* Space O(WIDTH) */
106+ }
107+ }
108+ }
109+ }
110+
111+ var searchGrid = ( board ) => {
112+ const [ rows , cols ] = [ board . length , board [ 0 ] . length ] ;
113+
114+ for ( let row = 0 ; row < rows ; row ++ ) { /* Time O(ROWS) */
115+ for ( let col = 0 ; col < cols ; col ++ ) { /* Time O(COLS) */
116+ const isO = board [ row ] [ col ] === 'O' ;
117+ if ( isO ) board [ row ] [ col ] = 'X' ;
118+
119+ const isStar = board [ row ] [ col ] === '*' ;
120+ if ( isStar ) board [ row ] [ col ] = 'O' ;
121+ }
122+ }
123+ }
0 commit comments