Skip to content

Commit

Permalink
Time: 76 ms (63.1%), Space: 55.6 MB (42.06%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
HassanMehmood413 committed Oct 3, 2024
1 parent 1225f15 commit 6dc683d
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions 0130-surrounded-regions/0130-surrounded-regions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* @param {character[][]} board
* @return {void} Do not return anything, modify board in-place instead.
*/
var solve = function (board) {
let rows = board.length
let cols = board[0].length


var dfs = function (start, end) {
if (start < 0 || start >= rows || end < 0 || end >= cols || board[start][end] != "O") {
return
}
board[start][end] = "B"

for (let [r, c] of [[-1, 0], [1, 0], [0, -1], [0, 1]]) {
let x = r + start
let y = c + end
dfs(x, y)

}
}



// check all rows
for (let i = 0; i < rows; i++) {
if (board[i][0] == "O") {
dfs(i, 0)
}
if (board[i][cols - 1] == "O") {
dfs(i, cols - 1)
}
}
for (let i = 0; i < cols; i++) {
if (board[0][i] == "O") {
dfs(0, i)
}
if (board[rows - 1][i] == "O") {
dfs(rows - 1, i)
}
}

for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (board[i][j] == "O") {
board[i][j] = "X"
}
else if (board[i][j] == 'B') {
board[i][j] = "O"
}
}
}
return board
};

0 comments on commit 6dc683d

Please sign in to comment.