From 12041e1d6c1aa2abf44175a94825e579277c0b07 Mon Sep 17 00:00:00 2001 From: n1i9c9k9 Date: Mon, 4 Nov 2024 11:08:53 +0100 Subject: [PATCH 1/2] added solution for bfs 130. Surrounded Regions --- surrounded-regions.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 surrounded-regions.cpp diff --git a/surrounded-regions.cpp b/surrounded-regions.cpp new file mode 100644 index 0000000..8dd278c --- /dev/null +++ b/surrounded-regions.cpp @@ -0,0 +1,64 @@ +#include +#include + +class Solution { +public: + void solve(std::vector>& board) { + if (board.empty() || board[0].empty()) return; + + int m = board.size(); + int n = board[0].size(); + + // Directions for moving up, down, left, right + std::vector> directions = { + {1, 0}, // down + {0, 1}, // right + {0, -1}, // left + {-1, 0} // up + }; + + // BFS function to mark all connected 'O's + auto bfs = [&](int startY, int startX) { + std::queue> q; + q.push({startY, startX}); + board[startY][startX] = 'E'; // Mark as visited with 'E' + + while (!q.empty()) { + auto [y, x] = q.front(); + q.pop(); + + for (const auto& dir : directions) { + int newY = y + dir.first; + int newX = x + dir.second; + + if (newY >= 0 && newY < m && newX >= 0 && newX < n && board[newY][newX] == 'O') { + board[newY][newX] = 'E'; // Mark as visited + q.push({newY, newX}); // Add to queue + } + } + } + }; + + // Check all edge 'O's + for (int i = 0; i < n; ++i) { + if (board[0][i] == 'O') bfs(0, i); // Top row + if (board[m - 1][i] == 'O') bfs(m - 1, i); // Bottom row + } + + for (int j = 1; j < m - 1; ++j) { + if (board[j][0] == 'O') bfs(j, 0); // Left column + if (board[j][n - 1] == 'O') bfs(j, n - 1); // Right column + } + + // Update the board: 'O's not connected to the edges become 'X', and 'E's back to 'O' + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + if (board[i][j] == 'O') { + board[i][j] = 'X'; // Flip 'O's to 'X's + } else if (board[i][j] == 'E') { + board[i][j] = 'O'; // Restore 'E's back to 'O's + } + } + } + } +}; \ No newline at end of file From ee8bf5f409fd0f19589ae1d9fb874d71cd8a6408 Mon Sep 17 00:00:00 2001 From: n1i9c9k9 Date: Mon, 4 Nov 2024 11:18:36 +0100 Subject: [PATCH 2/2] updated readme --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b2cac73..b6509ab 100644 --- a/README.md +++ b/README.md @@ -341,7 +341,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | 743 | [Network Delay Time](https://leetcode.com/problems/network-delay-time/) | [C++](./C++/Network-delay-time.cpp) | _O(V+E))_ | O(V) | Medium | BFS | | 111 | [Min Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/) | [JavaScript](./JavaScript/111-minimum-depth-of-binary-tree.js) | O(nlogn) | O(nlogn) | Easy | BFS | | 100 | [Same Tree](https://leetcode.com/problems/same-tree/) | [C++](https://github.com/codedecks-in/LeetCode-Solutions/blob/master/C%2B%2B/100_Same_Tree.cpp) | O(N) | O(N) | Easy | BFS | - +| 130 | [Surrounded Regions](https://leetcode.com/problems/surrounded-regions/) | [C++](./C++/kth-smallest-element-in-a-bst.cpp) | _O(m*n)_ | _O(m*n)_ | Medium | DFS | |
@@ -515,6 +515,7 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if | [Shrimadh V Rao](https://github.com/Shrimadh)
| India | C++ | [GitHub](https://github.com/Shrimadh) | [Shreyas Shrawage](https://github.com/shreyventure)
| India | Python | [CodeChef](https://www.codechef.com/users/shreyventure)
[LeetCode](https://leetcode.com/shreyventure/)
[HackerRank](https://www.hackerrank.com/shreyas_shrawage) | [Surbhi Mayank](https://github.com/surbhi2408)
| India | C++ | [GitHub](https://github.com/surbhi2408) +| [Nick H](https://github.com/n1i9c9k9)
| Germany | C++ | [GitHub](https://github.com/n1i9c9k9)