forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0130-surrounded-regions.ts
48 lines (42 loc) · 1.14 KB
/
0130-surrounded-regions.ts
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
/**
Do not return anything, modify board in-place instead.
*/
function solve(board: string[][]): void {
const rowLen = board.length;
const colLen = board[0].length;
const lastRow = rowLen - 1;
const lastCol = colLen - 1;
for (let r = 0; r < rowLen; ++r) {
markSeen(r, 0);
markSeen(r, lastCol);
}
for (let c = 1; c < lastCol; ++c) {
markSeen(0, c);
markSeen(lastRow, c);
}
for (let r = 0; r < rowLen; ++r) {
for (let c = 0; c < colLen; ++c) {
switch (board[r][c]) {
case 'O':
board[r][c] = 'X';
break;
case 'A':
board[r][c] = 'O';
break;
}
}
}
function markSeen(r: number, c: number): void {
if (!inBounds(r, c) || board[r][c] !== 'O') {
return;
}
board[r][c] = 'A';
markSeen(r - 1, c);
markSeen(r + 1, c);
markSeen(r, c - 1);
markSeen(r, c + 1);
}
function inBounds(r: number, c: number): boolean {
return r >= 0 && c >= 0 && r < rowLen && c < colLen;
}
};