forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0130-surrounded-regions.js
123 lines (98 loc) · 4.13 KB
/
0130-surrounded-regions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* https://leetcode.com/problems/surrounded-regions/
* Time O(ROWS * COLS) | Space O(ROWS * COLS)
* @param {character[][]} board
* @return {void} Do not return anything, modify board in-place instead.
*/
var solve = function solve(board) {
searchRows(board);/* Time O(ROWS * COLS) | Space O(ROWS * COLS) */
searchCols(board);/* Time O(ROWS * COLS) | Space O(ROWS * COLS) */
searchGrid(board);/* Time O(ROWS * COLS) | Space O(ROWS * COLS) */
}
var searchRows = (board) => {
const [ rows, cols ] = [ board.length, board[0].length ];
for (let row = 0; row < rows; row++) { /* Time O(ROWS) */
dfs(board, row, rows, 0, cols); /* Space O(ROWS) */
dfs(board, row, rows, (cols - 1), cols);/* Space O(ROWS) */
}
}
var searchCols = (board) => {
const [ rows, cols ] = [ board.length, board[0].length ];
for (let col = 1; col < (cols - 1); col++) {/* Time O(COLS) */
dfs(board, 0, rows, col, cols); /* Space O(COLS) */
dfs(board, (rows - 1), rows, col, cols);/* Space O(COLS) */
}
}
var searchGrid = (board) => {
const [ rows, cols ] = [ board.length, board[0].length ];
for (let row = 0; row < rows; row++) {/* Time O(ROWS) */
for (let col = 0; col < cols; col++) {/* Time O(COLS) */
const isO = board[row][col] === 'O';
if (isO) board[row][col] = 'X';
const isStar = board[row][col] === '*';
if (isStar) board[row][col] = 'O';
}
}
}
const dfs = (board, row, rows, col, cols) => {
const isBaseCase = board[row][col] !== 'O';
if (isBaseCase) return;
board[row][col] = '*';
for (const [ _row, _col ] of getNeighbors(row, rows, col, cols)) {
dfs(board, _row, rows, _col, cols);/* Time O(HEIGHT) | Space O(HEIGHT) */
}
}
var getNeighbors = (row, rows, col, cols) => [ [0, 1], [0, -1], [1, 0], [-1, 0] ]
.map(([ _row, _col ]) => [ (row + _row), (col + _col)])
.filter(([ _row, _col ]) => (0 <= _row) && (_row < rows) && (0 <= _col) && (_col < cols))
/**
* https://leetcode.com/problems/surrounded-regions/
* Time O(ROWS * COLS) | Space O(ROWS * COLS)
* @param {character[][]} board
* @return {void} Do not return anything, modify board in-place instead.
*/
var solve = function solve(board, queue = new Queue([])) {
searchRows(board, queue);/* Time O(ROWS + COLS) | Space O(ROWS + COLS) */
searchCols(board, queue);/* Time O(ROWS + COLS) | Space O(ROWS + COLS) */
bfs(board, queue); /* Time O(ROWS * COLS) | Space O(ROWS * COLS) */
searchGrid(board); /* Time O(ROWS * COLS) */
}
var searchRows = (board, queue) => {
const [ rows, cols ] = [ board.length, board[0].length ]
for (let row = 0; row < rows; row++) { /* Time O(ROWS) */
queue.enqueue([ row, 0 ]); /* Space O(ROWS) */
queue.enqueue([ row, (cols - 1) ]);/* Space O(ROWS) */
}
}
var searchCols = (board, queue) => {
const [ rows, cols ] = [ board.length, board[0].length ]
for (let col = 0; col < (cols - 1); col++) {/* Time O(COLS) */
queue.enqueue([ 0, col ]); /* Space O(COLS) */
queue.enqueue([ (rows - 1), col ]); /* Space O(COLS) */
}
}
var bfs = (board, queue) => {
const [ rows, cols ] = [ board.length, board[0].length ];
while (!queue.isEmpty()) {
for (let i = (queue.size() - 1); 0 <= i; i--) {/* Time O(WIDTH) */
const [ row, col ] = queue.dequeue();
const isBaseCase = board[row][col] !== 'O';
if (isBaseCase) continue;
board[row][col] = '*';
for (const [ _row, _col ] of getNeighbors(row, rows, col, cols)) {
queue.enqueue([ _row, _col ]); /* Space O(WIDTH) */
}
}
}
}
var searchGrid = (board) => {
const [ rows, cols ] = [ board.length, board[0].length ];
for (let row = 0; row < rows; row++) {/* Time O(ROWS) */
for (let col = 0; col < cols; col++) {/* Time O(COLS) */
const isO = board[row][col] === 'O';
if (isO) board[row][col] = 'X';
const isStar = board[row][col] === '*';
if (isStar) board[row][col] = 'O';
}
}
}