1
// time O(n * m) | space O(1)3
// We essentially invert this question4
// Instead of looking whether an 'O' node is surrounded,5
// we check if an 'O' node is on the edge (outer layer can't be surrounded)6
// and check if that is connected with any other nodes 'O' nodes (top, down, left, right).7
// We do no care if it is not connected to an 'O' edge node and thus never dfs for it.8
var solve = function (board) {9
if (!board.length) return [];11
for (let i = 0; i < board.length; i++) {12
for (let j = 0; j < board[0].length; j++) {13
// Only dfs if an 'O' and on the edge15
board[i][j] === "O" &&17
i === board.length - 1 ||19
j === board[0].length - 1)26
for (let i = 0; i < board.length; i++) {27
for (let j = 0; j < board[0].length; j++) {28
if (board[i][j] === "V") {43
c >= board[0].length ||44
board[r][c] === "X" ||