
Sponsored
Sponsored
This approach uses Depth-First Search (DFS) to mark all 'O's connected to the boundary as safe. These 'O's cannot be converted to 'X' because they are on the boundary or connected to the boundary. The rest of the 'O's can be safely converted to 'X'.
Time Complexity: O(m * n), Space Complexity: O(m * n) due to the DFS recursion stack.
1public class Solution {
2 private void Dfs(char[][] board, int row, int col, int m, int n) {
3 if (row < 0 || col < 0 || row >= m || col >= n || board[row][col] != 'O') return;
4 board[row][col] = 'A';
5 Dfs(board, row - 1, col, m, n);
6 Dfs(board, row + 1, col, m, n);
7 Dfs(board, row, col - 1, m, n);
8 Dfs(board, row, col + 1, m, n);
9 }
10
11 public void Solve(char[][] board) {
12 int m = board.Length;
13 if (m == 0) return;
14 int n = board[0].Length;
15
16 for (int i = 0; i < m; i++) {
17 if (board[i][0] == 'O') Dfs(board, i, 0, m, n);
18 if (board[i][n - 1] == 'O') Dfs(board, i, n - 1, m, n);
19 }
20
21 for (int j = 0; j < n; j++) {
22 if (board[0][j] == 'O') Dfs(board, 0, j, m, n);
23 if (board[m - 1][j] == 'O') Dfs(board, m - 1, j, m, n);
24 }
25
26 for (int i = 0; i < m; i++) {
27 for (int j = 0; j < n; j++) {
28 if (board[i][j] == 'O') board[i][j] = 'X';
29 if (board[i][j] == 'A') board[i][j] = 'O';
30 }
31 }
32 }
33}The C# solution mirrors the logic of the other implementations using Dfs to mark non-surrounded 'O's and then flipping the rest. It's consistent with DFS based boundary approach.
This approach uses Breadth-First Search (BFS) utilizing a queue to explore 'O's connected to the boundary. This approach is iterative and avoids deep recursion, keeping the method stack usage low.
Time Complexity: O(m * n), Space Complexity: O(m * n) for the queue's maximal use.
1#include <vector>
2#include <queue>
using namespace std;
void bfs(vector<vector<char>>& board, int row, int col, int m, int n) {
queue<pair<int, int>> q;
q.push({row, col});
board[row][col] = 'A';
while (!q.empty()) {
auto [curRow, curCol] = q.front();
q.pop();
int directions[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
for (const auto& dir : directions) {
int newRow = curRow + dir[0];
int newCol = curCol + dir[1];
if (newRow >= 0 && newRow < m && newCol >= 0 && newCol < n && board[newRow][newCol] == 'O') {
board[newRow][newCol] = 'A';
q.push({newRow, newCol});
}
}
}
}
void solve(vector<vector<char>>& board) {
int m = board.size();
if (m == 0) return;
int n = board[0].size();
for (int i = 0; i < m; ++i) {
if (board[i][0] == 'O') bfs(board, i, 0, m, n);
if (board[i][n - 1] == 'O') bfs(board, i, n - 1, m, n);
}
for (int j = 0; j < n; ++j) {
if (board[0][j] == 'O') bfs(board, 0, j, m, n);
if (board[m - 1][j] == 'O') bfs(board, m - 1, j, m, n);
}
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j) {
if (board[i][j] == 'O') board[i][j] = 'X';
if (board[i][j] == 'A') board[i][j] = 'O';
}
}This C++ version employs a queue to process already identified border 'O's into safe zones, using BFS efficiently for limited stack usage.