diff --git a/05- May/README.md b/05- May/README.md index f76030556..668c8c8f7 100644 --- a/05- May/README.md +++ b/05- May/README.md @@ -41,6 +41,7 @@ 1. **[Minimum Number of Vertices to Reach All Nodes](#18--minimum-number-of-vertices-to-reach-all-nodes)** 1. **[Is Graph Bipartite?](#19--is-graph-bipartite)** 1. **[Evaluate Division](#20--evaluate-division)** +1. **[Shortest Bridge](#21--shortest-bridge)**


@@ -1121,4 +1122,72 @@ public: } }; ``` + +
+

+ +## 21) [Shortest Bridge](https://leetcode.com/problems/shortest-bridge/) + +### Difficulty + +![](https://img.shields.io/badge/Medium-orange?style=for-the-badge) + +### Related Topic + +`Array` `Depth-First Search` `Breadth-First Search` `Matrix` + +### Code + + +```cpp +class Solution { + + int n; + vector < vector < int > > grid; + + // Check if the current position (r, c) is a valid position for island expansion + bool is_valid(int r, int c, set < pair < int, int > >& isL) { + return min(r, c) >= 0 && max(r, c) < n && !isL.count({r, c}) && grid[r][c]; + } + + // Depth-first search to expand the island + void dfs(int r, int c, set < pair < int, int > >& isL) { + if (!is_valid(r, c, isL)) return; + isL.insert({r, c}); // Mark the current position as visited + grid[r][c] = 0; // Set the current position as water + dfs(r + 1, c, isL); // Check the bottom position + dfs(r - 1, c, isL); // Check the top position + dfs(r, c + 1, isL); // Check the right position + dfs(r, c - 1, isL); // Check the left position + } + +public: + int shortestBridge(vector>& grid) { + this -> n = grid.size(); + this -> grid = grid; + + set < pair < int, int > > isl1, isl2; // Sets to store the positions of the two islands + bool isL = true; // Flag to differentiate between the two islands + + for (int x = 0; x < n; x++) { + for (int y = 0; y < n; y++) { + if (grid[x][y]) { + if (isL) + dfs(x, y, isl1), isL = false; // Expand the first island + else + dfs(x, y, isl2); // Expand the second island + } + } + } + + int ans = INT_MAX; + for (auto [x1, y1] : isl1) + for (auto [x2, y2] : isl2) + ans = min(ans, abs(x1 - x2) + abs(y1 - y2) - 1); // Calculate the minimum distance between the two islands + + + return ans; + } +}; +``` \ No newline at end of file