id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
2
{ "code": "class Solution {\npublic:\n void visit(vector<vector<char>>& grid, int i, int j) {\n std::vector<std::pair<int, int>> toVisit;\n toVisit.emplace_back(i, j);\n int m = grid.size();\n int n = grid[0].size();\n while (!toVisit.empty()) {\n auto [row, col] = toVisit.back();\n toVisit.pop_back();\n // if (grid[row][col] != '1') continue;\n grid[row][col] = 'x';\n if (row > 0 && grid[row - 1][col] == '1') toVisit.emplace_back(row - 1, col);\n if (col > 0 && grid[row][col - 1] == '1') toVisit.emplace_back(row, col - 1);\n if (row < m - 1 && grid[row + 1][col] == '1') toVisit.emplace_back(row + 1, col);\n if (col < n - 1 && grid[row][col + 1] == '1') toVisit.emplace_back(row, col + 1);\n }\n }\n int numIslands(vector<vector<char>>& grid) {\n int num = 0;\n for (int i = 0; i < grid.size(); ++i) {\n for (int j = 0; j < grid[i].size(); ++j) {\n if (grid[i][j] == '1') {\n ++num;\n visit(grid, i, j);\n }\n }\n }\n return num;\n }\n};", "memory": "16243" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
2
{ "code": "class Solution {\n queue<std::pair<int, int>> find, fill;\n set<std::pair<int, int>> check_set = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\npublic:\n void fillIsland(vector<vector<char>> &grid){\n while(fill.size() > 0){\n auto [ci, cj] = fill.front();\n fill.pop();\n\n for(auto &[x, y] : check_set){\n int i = ci + x, j = cj + y;\n if(i >= 0 && i < grid.size() && j >= 0 && j < grid[0].size()){\n if(grid[i][j] == '1'){\n grid[i][j] = '#';\n fill.push({i, j});\n } else if (grid[i][j] == '0'){\n grid[i][j] = '#';\n find.push({i, j});\n }\n }\n }\n }\n }\n\n int numIslands(vector<vector<char>>& grid) {\n int cnt_islands = 0;\n\n if(grid[0][0] == '1'){\n fill.push({0, 0});\n ++cnt_islands;\n fillIsland(grid);\n } else {\n find.push({0, 0});\n }\n grid[0][0] = '#';\n\n while(find.size() > 0){\n auto [ci, cj] = find.front();\n find.pop();\n\n for(auto &[x, y] : check_set){\n int i = ci + x, j = cj + y;\n if(i >= 0 && i < grid.size() && j >= 0 && j < grid[0].size()){\n if(grid[i][j] == '1'){\n ++cnt_islands;\n grid[i][j] = '#';\n fill.push({i, j});\n fillIsland(grid);\n } else if (grid[i][j] == '0'){\n grid[i][j] = '#';\n find.push({i, j});\n }\n }\n }\n }\n\n return cnt_islands;\n }\n};", "memory": "16576" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int withinBound(int i, int j, int m, int n) {\n return 0<=i && i<m && 0<=j && j<n;\n }\n int numIslands(vector<vector<char>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n vector<vector<int>> v(m, vector<int>(n,0));\n queue<pair<int,int>> q;\n int out=0;\n int r[] = {0, -1, 1, 0};\n int c[] = {-1, 0, 0, 1};\n for(int i=0;i<m;i++) {\n for(int j=0;j<n;j++) {\n if(grid[i][j] == '1' && !v[i][j]) {\n out++;\n v[i][j] = 1;\n q.push({i,j});\n while(!q.empty()){\n pair<int,int> p = q.front();\n q.pop();\n for(int k=0;k<4;k++) {\n int i1 = p.first + r[k];\n int i2 = p.second + c[k];\n if(withinBound(i1,i2,m,n) && grid[i1][i2] == '1' && !v[i1][i2]) {\n v[i1][i2] = 1;\n q.push({i1, i2});\n }\n }\n } \n }\n }\n }\n return out;\n }\n};", "memory": "16576" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int n =0;\n int r = grid.size();\n int c = grid[0].size();\n \n queue<pair<int,int>> q;\n \n for(int i=0; i < r; i++){\n for(int j=0; j< c; j++){\n \n if(grid[i][j] == '1'){\n n++;\n q.push({i,j});\n while(!q.empty()){\n auto t = q.front();\n int f = t.first;\n int s = t.second;\n cout << to_string(t.first)<< endl;\n q.pop();\n if (f-1>=0 && grid[f-1][s] == '1') { grid[f-1][s] = '0'; q.push({f-1,s});}\n if (f+1< r && grid[f+1][s] == '1') { grid[f+1][s] = '0'; q.push({f+1,s});}\n if (s-1>=0 && grid[f][s-1] == '1') { grid[f][s-1] = '0'; q.push({f,s-1});}\n if (s+1 <c && grid[f][s+1] == '1') { grid[f][s+1] = '0'; q.push({f,s+1});}\n }\n }\n }\n \n }\n return n;\n }\n};", "memory": "16910" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
2
{ "code": "class Solution {\npublic:\n class DisjointSet\n {\n public:\n vector<int> parent, rank, size;\n int count;\n DisjointSet(int n)\n {\n rank.resize(n+1, 0);\n size.resize(n+1, 1);\n parent.resize(n+1);\n count = 0;\n for(int i = 0;i<=n;i++)\n parent[i] = i;\n }\n int find(int x)\n {\n if(x==parent[x])\n return x;\n return parent[x] = find(parent[x]);\n }\n void union_by_rank(int x, int y)\n {\n int par_x = find(x);\n int par_y = find(y);\n if(par_x==par_y)\n return;\n else\n {\n if(rank[par_x]>rank[par_y])\n parent[par_y] = par_x;\n else if(rank[par_x]<rank[par_y])\n parent[par_x] = par_y;\n else\n {\n parent[par_y] = par_x;\n rank[par_x]++;\n }\n }\n }\n void union_by_size(int x, int y)\n {\n int par_x = find(x);\n int par_y = find(y);\n if(par_x==par_y)\n return;\n else\n {\n if(size[par_x]>size[par_y])\n {\n parent[par_y] = par_x;\n size[par_x] += size[par_y];\n }\n else\n {\n parent[par_y] = par_x;\n size[par_x] += size[par_y];\n }\n count--;\n }\n }\n void increment_count()\n {\n count++;\n }\n };\n int numIslands(vector<vector<char>>& grid) {\n int r = grid.size();\n int c = grid[0].size();\n DisjointSet ds(r*c);\n vector<int> directions = {0, 1, 0};\n \n for(int i = 0;i<grid.size();i++)\n {\n for(int j = 0;j<grid[0].size();j++)\n {\n char curr = grid[i][j];\n if(curr=='1')\n {\n ds.increment_count();\n for(int k = 0;k<directions.size()-1;k++)\n {\n int new_r = i + directions[k];\n int new_c = j + directions[k+1];\n if(new_r>=0 && new_r<r && new_c>=0 && new_c<c && grid[new_r][new_c]=='1')\n {\n int hash_for_curr = i*c+j;\n int hash_for_new = new_r*c + new_c;\n ds.union_by_size(hash_for_curr, hash_for_new);\n }\n }\n }\n }\n }\n return ds.count;\n }\n};", "memory": "16910" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
2
{ "code": "class Solution {\npublic:\n class DisjointSet\n {\n public:\n vector<int> parent, rank, size;\n int count;\n DisjointSet(int n)\n {\n rank.resize(n+1, 0);\n size.resize(n+1, 1);\n parent.resize(n+1);\n count = 0;\n for(int i = 0;i<=n;i++)\n parent[i] = i;\n }\n int find(int x)\n {\n if(x==parent[x])\n return x;\n return parent[x] = find(parent[x]);\n }\n void union_by_rank(int x, int y)\n {\n int par_x = find(x);\n int par_y = find(y);\n if(par_x==par_y)\n return;\n else\n {\n if(rank[par_x]>rank[par_y])\n parent[par_y] = par_x;\n else if(rank[par_x]<rank[par_y])\n parent[par_x] = par_y;\n else\n {\n parent[par_y] = par_x;\n rank[par_x]++;\n }\n }\n }\n void union_by_size(int x, int y)\n {\n int par_x = find(x);\n int par_y = find(y);\n if(par_x==par_y)\n return;\n else\n {\n if(size[par_x]>size[par_y])\n {\n parent[par_y] = par_x;\n size[par_x] += size[par_y];\n }\n else\n {\n parent[par_y] = par_x;\n size[par_x] += size[par_y];\n }\n count--;\n }\n }\n void increment_count()\n {\n count++;\n }\n };\n int numIslands(vector<vector<char>>& grid) {\n int r = grid.size();\n int c = grid[0].size();\n DisjointSet ds(r*c);\n vector<int> directions = {0, 1, 0};\n \n for(int i = 0;i<grid.size();i++)\n {\n for(int j = 0;j<grid[0].size();j++)\n {\n char curr = grid[i][j];\n if(curr=='1')\n {\n ds.increment_count();\n for(int k = 0;k<directions.size()-1;k++)\n {\n int new_r = i + directions[k];\n int new_c = j + directions[k+1];\n if(new_r>=0 && new_r<r && new_c>=0 && new_c<c && grid[new_r][new_c]=='1')\n {\n int hash_for_curr = i*c+j;\n int hash_for_new = new_r*c + new_c;\n ds.union_by_size(hash_for_curr, hash_for_new);\n }\n }\n }\n }\n }\n return ds.count;\n }\n};", "memory": "17244" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
2
{ "code": "class Solution {\n stack<std::pair<int, int>> find, fill;\n set<std::pair<int, int>> check_set = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\npublic:\n void fillIsland(vector<vector<char>> &grid){\n while(fill.size() > 0){\n auto [ci, cj] = fill.top();\n fill.pop();\n\n for(auto &[x, y] : check_set){\n int i = ci + x, j = cj + y;\n if(i >= 0 && i < grid.size() && j >= 0 && j < grid[0].size()){\n if(grid[i][j] == '1'){\n grid[i][j] = '#';\n fill.push({i, j});\n } else if (grid[i][j] == '0'){\n grid[i][j] = '#';\n find.push({i, j});\n }\n }\n }\n }\n }\n\n int numIslands(vector<vector<char>>& grid) {\n int cnt_islands = 0;\n\n if(grid[0][0] == '1'){\n fill.push({0, 0});\n ++cnt_islands;\n fillIsland(grid);\n } else {\n find.push({0, 0});\n }\n grid[0][0] = '#';\n\n while(find.size() > 0){\n auto [ci, cj] = find.top();\n find.pop();\n\n for(auto &[x, y] : check_set){\n int i = ci + x, j = cj + y;\n if(i >= 0 && i < grid.size() && j >= 0 && j < grid[0].size()){\n if(grid[i][j] == '1'){\n ++cnt_islands;\n grid[i][j] = '#';\n fill.push({i, j});\n fillIsland(grid);\n } else if (grid[i][j] == '0'){\n grid[i][j] = '#';\n find.push({i, j});\n }\n }\n }\n }\n\n return cnt_islands;\n }\n};", "memory": "17578" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
2
{ "code": "class Solution {\npublic:\n void dfs(int i, int j , vector<vector<char>>& grid, vector<vector<int>>& visited, int row, int col ){\n if(i >= row || j >= col || i < 0 || j < 0) return;\n if(grid[i][j] != '1' || visited[i][j] == 1) return;\n visited[i][j] = 1;\n dfs(i-1, j, grid, visited, row, col);\n dfs(i+1, j, grid, visited, row, col);\n dfs(i, j-1, grid, visited, row, col);\n dfs(i, j+1, grid, visited, row, col);\n }\n int numIslands(vector<vector<char>>& grid) {\n int row = grid.size();\n int col = grid[0].size();\n int count = 0;\n vector<vector<int>> visited(row);\n for(int i=0; i<row; i++){\n for(int j=0; j<col; j++){\n visited[i].push_back(0);\n }\n }\n\n for(int i=0; i<row; i++){\n for(int j=0; j<col; j++){\n if(visited[i][j] == 0 && grid[i][j] == '1'){\n count++;\n dfs(i, j, grid, visited, row, col);\n }\n }\n }\n return count;\n }\n};", "memory": "17911" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
2
{ "code": "class disjoint {\npublic:\n vector<int> size, parent;\n disjoint(int n) {\n size.resize(n + 1);\n parent.resize(n+1);\n for (int i = 0; i <= n; i++) {\n parent[i] = i;\n size[i] = 1;\n }\n }\n int findpar(int u) {\n if (u == parent[u]) {\n return u;\n }\n return parent[u] = findpar(parent[u]);\n }\n void unionbysize(int u, int v) {\n int ulp_u = findpar(u);\n int ulp_v = findpar(v);\n if (ulp_u == ulp_v)\n return;\n if (size[ulp_u] < size[ulp_v]) {\n parent[ulp_u] = ulp_v;\n size[ulp_v] += size[ulp_u];\n } else {\n parent[ulp_v] = ulp_u;\n size[ulp_u] += size[ulp_v];\n }\n }\n};\nclass Solution {\npublic:\n bool isvalid(int newr, int newc, int n, int m) {\n return (newr >= 0 && newr < n && newc >= 0 && newc < m);\n }\n int numIslands(vector<vector<char>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n int cnt = 0;\n disjoint ds(n * m);\n vector<vector<int>> vis(n, vector<int>(m, 0));\n for (int row = 0; row < n; row++) {\n for (int col = 0; col < m; col++) {\n if (grid[row][col] == '0' || vis[row][col] == 1) {\n continue;\n }\n vis[row][col] = 1;\n cnt++;\n int dr[] = {1, 0, -1, 0};\n int dc[] = {0, -1, 0, 1};\n for (int i = 0; i < 4; i++) {\n int newr = row + dr[i];\n int newc = col + dc[i];\n if (isvalid(newr, newc, n, m) && grid[newr][newc] == '1' &&\n vis[newr][newc] == 0) {\n int node = row * m + col;\n int adjnode = newr * m + newc;\n if (ds.findpar(node) != ds.findpar(adjnode)) {\n cnt--;\n ds.unionbysize(node, adjnode);\n }\n }\n }\n }\n }\n return cnt;\n }\n};", "memory": "18245" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
2
{ "code": "#include <stack>\n#include <utility>\n\nstruct Index\n{\n int x = -1, y = -1;\n};\n\nclass Solution\n{\n void push_to_stack(\n const vector<vector<char>> &grid,\n vector<vector<int>> &nodeStates,\n std::stack<Index> &dfsStack,\n int X,\n int Y)\n {\n if (grid[X][Y] == '1' && nodeStates[X][Y] == 0)\n {\n nodeStates[X][Y] = 1;\n dfsStack.push({X, Y});\n }\n }\npublic:\n int numIslands(vector<vector<char>> &grid)\n {\n int islands = 0;\n // To keep track of the node traversal state\n // 0: unexplored\n // 1: visited\n // 2: completed\n vector<vector<int>> nodeStates;\n for (auto i = 0ul; i < grid.size(); ++i)\n {\n nodeStates.push_back(vector<int>());\n for (auto j = 0ul; j < grid[i].size(); ++j)\n {\n nodeStates[i].push_back(0);\n }\n }\n // DFS traversal stack stores node index and next neighbor index\n // to be explored of the node\n std::stack<Index> dfsStack;\n\n for (int i = 0; i < grid.size(); ++i)\n {\n for (int j = 0; j < grid[i].size(); ++j)\n {\n if (grid[i][j] == '1' && nodeStates[i][j] == 0)\n {\n islands += 1;\n\n // Start a DFS search\n nodeStates[i][j] = 1;\n dfsStack.push({i, j});\n\n while (!dfsStack.empty())\n {\n auto tos = dfsStack.top();\n dfsStack.pop();\n auto X = tos.x;\n auto Y = tos.y;\n if(X - 1 >= 0) \n push_to_stack(grid, nodeStates, dfsStack, X-1, Y);\n if(X + 1 < grid.size()) \n push_to_stack(grid, nodeStates, dfsStack, X+1, Y);\n if(Y - 1 >= 0) \n push_to_stack(grid, nodeStates, dfsStack, X, Y-1);\n if(Y + 1 < grid[0].size()) \n push_to_stack(grid, nodeStates, dfsStack, X, Y+1);\n nodeStates[X][Y] = 2;\n \n }\n }\n }\n }\n\n return islands;\n }\n};", "memory": "18579" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
2
{ "code": "#include <stack>\n#include <utility>\n\nstruct Index\n{\n int x = -1, y = -1;\n};\n\nclass Solution\n{\n void push_to_stack(\n const vector<vector<char>> &grid,\n vector<vector<int>> &nodeStates,\n std::stack<Index> &dfsStack,\n int X,\n int Y)\n {\n if (grid[X][Y] == '1' && nodeStates[X][Y] == 0)\n {\n nodeStates[X][Y] = 1;\n dfsStack.push({X, Y});\n }\n }\npublic:\n int numIslands(vector<vector<char>> &grid)\n {\n int islands = 0;\n // To keep track of the node traversal state\n // 0: unexplored\n // 1: visited\n // 2: completed\n vector<vector<int>> nodeStates;\n for (auto i = 0ul; i < grid.size(); ++i)\n {\n nodeStates.push_back(vector<int>());\n for (auto j = 0ul; j < grid[i].size(); ++j)\n {\n nodeStates[i].push_back(0);\n }\n }\n // DFS traversal stack stores node index and next neighbor index\n // to be explored of the node\n std::stack<Index> dfsStack;\n\n for (int i = 0; i < grid.size(); ++i)\n {\n for (int j = 0; j < grid[i].size(); ++j)\n {\n if (grid[i][j] == '1' && nodeStates[i][j] == 0)\n {\n islands += 1;\n\n // Start a DFS search\n nodeStates[i][j] = 1;\n dfsStack.push({i, j});\n\n while (!dfsStack.empty())\n {\n auto tos = dfsStack.top();\n dfsStack.pop();\n auto X = tos.x;\n auto Y = tos.y;\n if(X - 1 >= 0) \n push_to_stack(grid, nodeStates, dfsStack, X-1, Y);\n if(X + 1 < grid.size()) \n push_to_stack(grid, nodeStates, dfsStack, X+1, Y);\n if(Y - 1 >= 0) \n push_to_stack(grid, nodeStates, dfsStack, X, Y-1);\n if(Y + 1 < grid[0].size()) \n push_to_stack(grid, nodeStates, dfsStack, X, Y+1);\n nodeStates[X][Y] = 2;\n \n }\n }\n }\n }\n\n return islands;\n }\n};", "memory": "18579" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
2
{ "code": "#include <stack>\n#include <utility>\n\nstruct Index\n{\n int x = -1, y = -1;\n};\n\nclass Solution\n{\n void push_to_stack(\n const vector<vector<char>> &grid,\n vector<vector<int>> &nodeStates,\n std::stack<Index> &dfsStack,\n int X,\n int Y)\n {\n if (grid[X][Y] == '1' && nodeStates[X][Y] == 0)\n {\n nodeStates[X][Y] = 1;\n dfsStack.push({X, Y});\n }\n }\npublic:\n int numIslands(vector<vector<char>> &grid)\n {\n int islands = 0;\n // To keep track of the node traversal state\n // 0: unexplored\n // 1: visited\n // 2: completed\n vector<vector<int>> nodeStates;\n for (auto i = 0ul; i < grid.size(); ++i)\n {\n nodeStates.push_back(vector<int>());\n for (auto j = 0ul; j < grid[i].size(); ++j)\n {\n nodeStates[i].push_back(0);\n }\n }\n // DFS traversal stack stores node index and next neighbor index\n // to be explored of the node\n std::stack<Index> dfsStack;\n\n for (int i = 0; i < grid.size(); ++i)\n {\n for (int j = 0; j < grid[i].size(); ++j)\n {\n if (grid[i][j] == '1' && nodeStates[i][j] == 0)\n {\n islands += 1;\n\n // Start a DFS search\n nodeStates[i][j] = 1;\n dfsStack.push({i, j});\n\n while (!dfsStack.empty())\n {\n auto tos = dfsStack.top();\n dfsStack.pop();\n auto X = tos.x;\n auto Y = tos.y;\n if(X - 1 >= 0) \n push_to_stack(grid, nodeStates, dfsStack, X-1, Y);\n if(X + 1 < grid.size()) \n push_to_stack(grid, nodeStates, dfsStack, X+1, Y);\n if(Y - 1 >= 0) \n push_to_stack(grid, nodeStates, dfsStack, X, Y-1);\n if(Y + 1 < grid[0].size()) \n push_to_stack(grid, nodeStates, dfsStack, X, Y+1);\n nodeStates[X][Y] = 2;\n \n }\n }\n }\n }\n\n return islands;\n }\n};", "memory": "18913" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
2
{ "code": "class Solution {\n private : \n void bfs(queue<pair<int,int>> &q,vector<vector<char>> &grid,vector<vector<int>> &visited,int m,int n)\n {\n while(!q.empty())\n {\n pair<int,int> temp=q.front();\n q.pop();\n\n int i=temp.first;\n int j=temp.second;\n\n if(i - 1 >= 0 && grid[i-1][j] - '0' && !visited[i-1][j]) //up\n {\n q.push(pair<int,int>(i-1,j));\n visited[i-1][j]=1; //as visited is defined true when it is pushed onto the queue\n }\n\n if(j - 1 >= 0 && grid[i][j-1] - '0' && !visited[i][j-1]) //left\n {\n q.push(pair<int,int>(i,j-1));\n visited[i][j-1]=1;\n\n }\n\n if(i+1 < m && grid[i+1][j] - '0' && !visited[i+1][j]) //down\n {\n q.push(pair<int,int>(i+1,j));\n visited[i+1][j]=1;\n }\n\n if(j+1 < n && grid[i][j+1] - '0' && !visited[i][j+1]) //right\n {\n q.push(pair<int,int>(i,j+1));\n visited[i][j+1]=1;\n }\n\n }\n }\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int countIslands=0;\n int m=grid.size();\n int n=grid[0].size();\n\n vector<vector<int>> visited(m,vector<int>(n,0));\n queue<pair<int,int>> q;\n\n for(int i=0;i<m;i++)\n {\n for(int j=0;j<n;j++)\n {\n if(grid[i][j] - '0')\n {\n if(!visited[i][j])\n {\n q.push(pair<int,int>(i,j));\n visited[i][j]=1;\n countIslands++;\n bfs(q,grid,visited,m,n);\n }\n }\n }\n }\n return countIslands;\n\n }\n};", "memory": "18913" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
2
{ "code": "struct Index\n{\n int x = -1, y = -1;\n};\n\nclass Solution\n{\n void push_to_stack(\n const vector<vector<char>> &grid,\n vector<vector<int>> &nodeStates,\n std::vector<Index> &dfsStack,\n int X,\n int Y)\n {\n if (grid[X][Y] == '1' && nodeStates[X][Y] == 0)\n {\n nodeStates[X][Y] = 1;\n dfsStack.push_back({X, Y});\n }\n }\npublic:\n int numIslands(vector<vector<char>> &grid)\n {\n int islands = 0;\n // To keep track of the node traversal state\n // 0: unexplored\n // 1: visited\n // 2: completed\n vector<vector<int>> nodeStates;\n for (auto i = 0ul; i < grid.size(); ++i)\n {\n nodeStates.push_back(vector<int>());\n for (auto j = 0ul; j < grid[i].size(); ++j)\n {\n nodeStates[i].push_back(0);\n }\n }\n // DFS traversal stack stores node index and next neighbor index\n // to be explored of the node\n std::vector<Index> dfsStack;\n dfsStack.reserve(1000);\n\n for (int i = 0; i < grid.size(); ++i)\n {\n for (int j = 0; j < grid[i].size(); ++j)\n {\n if (grid[i][j] == '1' && nodeStates[i][j] == 0)\n {\n islands += 1;\n\n // Start a DFS search\n nodeStates[i][j] = 1;\n dfsStack.push_back({i, j});\n\n while (!dfsStack.empty())\n {\n auto tos = dfsStack.back();\n dfsStack.pop_back();\n auto X = tos.x;\n auto Y = tos.y;\n if(X - 1 >= 0) \n push_to_stack(grid, nodeStates, dfsStack, X-1, Y);\n if(X + 1 < grid.size()) \n push_to_stack(grid, nodeStates, dfsStack, X+1, Y);\n if(Y - 1 >= 0) \n push_to_stack(grid, nodeStates, dfsStack, X, Y-1);\n if(Y + 1 < grid[0].size()) \n push_to_stack(grid, nodeStates, dfsStack, X, Y+1);\n nodeStates[X][Y] = 2;\n \n }\n }\n }\n }\n\n return islands;\n }\n};", "memory": "19246" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
2
{ "code": "struct Index\n{\n int x = -1, y = -1;\n};\n\nclass Solution\n{\n void push_to_stack(\n const vector<vector<char>> &grid,\n vector<vector<int>> &nodeStates,\n std::vector<Index> &dfsStack,\n int X,\n int Y)\n {\n if (grid[X][Y] == '1' && nodeStates[X][Y] == 0)\n {\n nodeStates[X][Y] = 1;\n dfsStack.push_back({X, Y});\n }\n }\npublic:\n int numIslands(vector<vector<char>> &grid)\n {\n int islands = 0;\n // To keep track of the node traversal state\n // 0: unexplored\n // 1: visited\n // 2: completed\n vector<vector<int>> nodeStates;\n for (auto i = 0ul; i < grid.size(); ++i)\n {\n nodeStates.push_back(vector<int>());\n for (auto j = 0ul; j < grid[i].size(); ++j)\n {\n nodeStates[i].push_back(0);\n }\n }\n // DFS traversal stack stores node index and next neighbor index\n // to be explored of the node\n std::vector<Index> dfsStack;\n dfsStack.reserve(1000);\n\n for (int i = 0; i < grid.size(); ++i)\n {\n for (int j = 0; j < grid[i].size(); ++j)\n {\n if (grid[i][j] == '1' && nodeStates[i][j] == 0)\n {\n islands += 1;\n\n // Start a DFS search\n nodeStates[i][j] = 1;\n dfsStack.push_back({i, j});\n\n while (!dfsStack.empty())\n {\n auto tos = dfsStack.back();\n dfsStack.pop_back();\n auto X = tos.x;\n auto Y = tos.y;\n if(X - 1 >= 0) \n push_to_stack(grid, nodeStates, dfsStack, X-1, Y);\n if(X + 1 < grid.size()) \n push_to_stack(grid, nodeStates, dfsStack, X+1, Y);\n if(Y - 1 >= 0) \n push_to_stack(grid, nodeStates, dfsStack, X, Y-1);\n if(Y + 1 < grid[0].size()) \n push_to_stack(grid, nodeStates, dfsStack, X, Y+1);\n nodeStates[X][Y] = 2;\n \n }\n }\n }\n }\n\n return islands;\n }\n};", "memory": "19246" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int totalIslands = 0;\n int rowSize = grid.size();\n int colSize = grid[0].size();\n \n queue<pair<int, int>> myQueue;\n for (int r = 0; r < rowSize; r++)\n {\n for (int c = 0; c < colSize; c++)\n {\n if (grid[r][c] == '1')\n {\n totalIslands++;\n myQueue.push(make_pair(r, c));\n while (!myQueue.empty())\n {\n auto curr = myQueue.front();\n myQueue.pop();\n int i = curr.first;\n int j = curr.second;\n if (grid[i][j] == '0') continue;\n grid[i][j] = '0';\n \n if (i - 1 >= 0) myQueue.push(make_pair(i - 1, j));\n if (i + 1 < rowSize) myQueue.push(make_pair(i + 1, j));\n if (j - 1 >= 0) myQueue.push(make_pair(i, j - 1));\n if (j + 1 < colSize) myQueue.push(make_pair(i, j + 1));\n }\n }\n }\n }\n \n return totalIslands;\n }\n};", "memory": "19580" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
2
{ "code": "class Solution {\r\npublic:\r\n void bfs(deque<pair<int, int>> &adj, vector<vector<int>> &vis, vector<vector<char>>& grid){\r\n int x[4] = {1, -1, 0, 0};\r\n int y[4] = {0, 0, 1, -1};\r\n int xn, yn;\r\n while(!adj.empty()){\r\n auto now = adj.front();\r\n adj.pop_front();\r\n for(auto i =0;i<4;i++){\r\n xn = now.first+x[i];\r\n yn = now.second+y[i];\r\n // cout<<\"checking (\"<<xn<< yn<<\")\"<<endl;\r\n if(xn>=0&&xn<grid.size()&&yn>=0&&yn<grid[0].size()){\r\n // cout<<\"checking (\"<<xn<< yn<<\")\"<<endl;\r\n if(grid[xn][yn]=='1'&&vis[xn][yn]==0){\r\n \r\n vis[xn][yn] = 1;\r\n adj.push_back({xn, yn});\r\n }\r\n }\r\n }\r\n }\r\n }\r\n int numIslands(vector<vector<char>>& grid) {\r\n int m = grid.size(), n = grid[0].size();\r\n deque<pair<int, int>> adj;\r\n vector<vector<int>> vis(m);\r\n for(auto i =0;i<m;i++){\r\n for(auto j = 0; j<n; j++){\r\n vis[i].push_back(0);\r\n }\r\n }\r\n int island = 0;\r\n for(auto i =0;i<m;i++){\r\n for(auto j = 0; j<n; j++){\r\n // cout<<\"checking x\"<<i<<\"y\"<<j<<endl;\r\n if(vis[i][j]==0&&grid[i][j]=='1'){\r\n cout<<\"going in\"<<endl;\r\n adj.push_back({i, j});\r\n island++;\r\n bfs(adj, vis, grid);\r\n }\r\n }\r\n }\r\n return island;\r\n }\r\n};", "memory": "19580" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
2
{ "code": "struct Index\n{\n int x = -1, y = -1;\n};\n\nclass Solution\n{\n void push_to_stack(\n const vector<vector<char>> &grid,\n vector<vector<int>> &nodeStates,\n std::vector<Index> &dfsStack,\n int X,\n int Y)\n {\n if (grid[X][Y] == '1' && nodeStates[X][Y] == 0)\n {\n nodeStates[X][Y] = 1;\n dfsStack.push_back({X, Y});\n }\n }\npublic:\n int numIslands(vector<vector<char>> &grid)\n {\n int islands = 0;\n // To keep track of the node traversal state\n // 0: unexplored\n // 1: visited\n // 2: completed\n vector<vector<int>> nodeStates;\n for (auto i = 0ul; i < grid.size(); ++i)\n {\n nodeStates.push_back(vector<int>());\n for (auto j = 0ul; j < grid[i].size(); ++j)\n {\n nodeStates[i].push_back(0);\n }\n }\n // DFS traversal stack stores node index and next neighbor index\n // to be explored of the node\n std::vector<Index> dfsStack;\n dfsStack.reserve(10000);\n\n for (int i = 0; i < grid.size(); ++i)\n {\n for (int j = 0; j < grid[i].size(); ++j)\n {\n if (grid[i][j] == '1' && nodeStates[i][j] == 0)\n {\n islands += 1;\n\n // Start a DFS search\n nodeStates[i][j] = 1;\n dfsStack.push_back({i, j});\n\n while (!dfsStack.empty())\n {\n auto tos = dfsStack.back();\n dfsStack.pop_back();\n auto X = tos.x;\n auto Y = tos.y;\n if(X - 1 >= 0) \n push_to_stack(grid, nodeStates, dfsStack, X-1, Y);\n if(X + 1 < grid.size()) \n push_to_stack(grid, nodeStates, dfsStack, X+1, Y);\n if(Y - 1 >= 0) \n push_to_stack(grid, nodeStates, dfsStack, X, Y-1);\n if(Y + 1 < grid[0].size()) \n push_to_stack(grid, nodeStates, dfsStack, X, Y+1);\n nodeStates[X][Y] = 2;\n \n }\n }\n }\n }\n\n return islands;\n }\n};", "memory": "19914" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int count = 0;\n\n int m = grid.size();\n int n = grid[0].size();\n\n int visited[m * n];\n for (size_t i = 0; i < m*n; i++) {\n visited[i] = 0;\n }\n\n std::queue<std::pair<int, int>> bfs_queue;\n\n for (size_t i = 0; i < m; i++) {\n for (size_t j = 0; j < n; j++) {\n int idx = j + i * n;\n\n if (visited[idx] == 1) {\n continue;\n }\n\n if (grid[i][j] != '1') {\n continue;\n }\n\n bfs_queue.push(std::make_pair(i, j));\n count++;\n \n while (!bfs_queue.empty()) {\n auto idx_pair = bfs_queue.front();\n bfs_queue.pop();\n\n if (idx_pair.first < 0 || idx_pair.first >= m || idx_pair.second < 0 ||idx_pair.second >= n) {\n continue;\n }\n\n if (grid[idx_pair.first][idx_pair.second] != '1') {\n continue;\n }\n\n int idx = idx_pair.second + idx_pair.first * n;\n if (visited[idx] == 1) {\n continue;\n }\n\n bfs_queue.push(std::make_pair(idx_pair.first, idx_pair.second - 1));\n bfs_queue.push(std::make_pair(idx_pair.first + 1, idx_pair.second));\n bfs_queue.push(std::make_pair(idx_pair.first - 1, idx_pair.second));\n bfs_queue.push(std::make_pair(idx_pair.first, idx_pair.second + 1));\n visited[idx] = 1;\n }\n }\n }\n return count;\n }\n};", "memory": "19914" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
2
{ "code": "class Solution {\r\n\r\n int res_{0};\r\npublic:\r\n\r\n vector<pair<int, int>> GetVaildNeighbors(vector<vector<char>>& grid, int x, int y) {\r\n int m = grid.size();\r\n int n = grid[0].size();\r\n vector<pair<int,int>> neighbors;\r\n if(x > 0 and grid[x-1][y] == '1') {\r\n neighbors.emplace_back(x-1, y);\r\n }\r\n if(y > 0 and grid[x][y-1] == '1') {\r\n neighbors.emplace_back(x, y-1);\r\n }\r\n if(x < m-1 and grid[x+1][y] == '1') {\r\n neighbors.emplace_back(x+1, y);\r\n } \r\n if(y < n-1 and grid[x][y+1] == '1') {\r\n neighbors.emplace_back(x, y+1);\r\n }\r\n return neighbors;\r\n }\r\n\r\n void DFS(vector<vector<char>>& grid, int x, int y) {\r\n grid[x][y] = '0';\r\n auto neighbor = GetVaildNeighbors(grid, x, y);\r\n for(auto [n_x, n_y] : neighbor) {\r\n DFS(grid, n_x, n_y);\r\n }\r\n }\r\n\r\n int numIslands(vector<vector<char>>& grid) {\r\n for(int i = 0; i < grid.size(); i++) {\r\n for(int j = 0; j < grid[0].size(); j++) {\r\n if(grid[i][j] == '1') {\r\n res_++;\r\n DFS(grid, i ,j);\r\n }\r\n }\r\n }\r\n return res_;\r\n }\r\n};", "memory": "20248" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
2
{ "code": "class Solution {\r\n\r\n int res_{0};\r\npublic:\r\n\r\n vector<pair<int, int>> GetVaildNeighbors(vector<vector<char>>& grid, int x, int y) {\r\n int m = grid.size();\r\n int n = grid[0].size();\r\n vector<pair<int,int>> neighbors;\r\n if(x > 0 and grid[x-1][y] == '1') {\r\n neighbors.emplace_back(x-1, y);\r\n }\r\n if(y > 0 and grid[x][y-1] == '1') {\r\n neighbors.emplace_back(x, y-1);\r\n }\r\n if(x < m-1 and grid[x+1][y] == '1') {\r\n neighbors.emplace_back(x+1, y);\r\n } \r\n if(y < n-1 and grid[x][y+1] == '1') {\r\n neighbors.emplace_back(x, y+1);\r\n }\r\n return neighbors;\r\n }\r\n\r\n void DFS(vector<vector<char>>& grid, int x, int y) {\r\n grid[x][y] = '0';\r\n auto neighbor = GetVaildNeighbors(grid, x, y);\r\n for(auto [n_x, n_y] : neighbor) {\r\n DFS(grid, n_x, n_y);\r\n }\r\n }\r\n\r\n int numIslands(vector<vector<char>>& grid) {\r\n for(int i = 0; i < grid.size(); i++) {\r\n for(int j = 0; j < grid[0].size(); j++) {\r\n if(grid[i][j] == '1') {\r\n res_++;\r\n DFS(grid, i ,j);\r\n }\r\n }\r\n }\r\n return res_;\r\n }\r\n};", "memory": "20248" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
2
{ "code": "class Solution {\npublic:\n void func(vector<vector<char>>& grid, list<pair<int,int>>& que, int i, int j) {\n int n = grid.size(); int m = grid[0].size();\n pair<int,int> p = make_pair(i,j);\n que.push_back(p);\n grid[i][j] = '0';\n while(!que.empty()) {\n pair<int,int> ind = que.front();\n int r = ind.first, c = ind.second;\n que.pop_front();\n\n if(0 <= r+1 && r+1 < n && 0 <= c && c < m && grid[r+1][c] == '1') {\n p.first = r+1; p.second = c;\n que.push_back(p);\n grid[r+1][c] = '0';\n }\n if(0 <= r-1 && r-1 < n && 0 <= c && c < m && grid[r-1][c] == '1') {\n p.first = r-1; p.second = c;\n que.push_back(p);\n grid[r-1][c] = '0';\n }\n if(0 <= r && r < n && 0 <= c+1 && c+1 < m && grid[r][c+1] == '1') {\n p.first = r; p.second = c+1;\n que.push_back(p);\n grid[r][c+1] = '0';\n }\n if(0 <= r && r < n && 0 <= c-1 && c-1 < m && grid[r][c-1] == '1') {\n p.first = r; p.second = c-1;\n que.push_back(p);\n grid[r][c-1] = '0';\n }\n }\n }\n\n int numIslands(vector<vector<char>>& grid) {\n int n = grid.size(); int m = grid[0].size();\n list<pair<int,int>> que;\n int islands = 0;\n\n for(int i = 0; i < n; i++) {\n for(int j = 0; j < m; j++) {\n if(grid[i][j] == '1') {\n islands++;\n func(grid, que, i, j);\n }\n }\n }\n\n return islands;\n }\n};\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n// class Solution:\n// def numIslands(self, grid: List [List[str]l) →> int:\n// islands = 0\n// rows, cols = len(grid), len(grid [0])\n// 口\n// def bfs(r, c):\n// q = deque()\n// q-append ( (r, c))\n// grid[r] [c] = '0'\n// while q:\n// row, col = q-popleft()\n// directions = [[1,01, [-1,0], [0,1], [0,-1]]\n// for dr, de in directions:\n// r, c = row + dr, col + dc\n// if 0 r < rows and 0 = c < cols and grid[r][c] =\n// \"1\":\n// q-append ((r, c))\n// grid[r] [c] = '0'\n// for r in range(rows):\n// for c in range(cols):\n// if grid[r][c] = \"1\":\n// islands += 1\n// bfs(r, c)\n// return\n// islands", "memory": "20581" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
2
{ "code": "class Solution {\npublic:\n void dfs(vector<vector<char>>& grid, int i, int j) {\n grid[i][j] = '0';\n vector<pair<int, int>> dirs = {{1,0}, {-1,0}, {0,1}, {0,-1}};\n for(pair<int, int> dir: dirs) {\n int di = i + dir.first;\n int dj = j + dir.second;\n if(0 <= di && di < grid.size() && 0 <= dj && dj < grid[0].size() && grid[di][dj] == '1') {\n dfs(grid, di, dj);\n }\n }\n }\n int numIslands(vector<vector<char>>& grid) {\n int num_islands = 0;\n for(int i = 0; i < grid.size(); ++i) {\n for(int j = 0; j < grid[0].size(); ++j) {\n if(grid[i][j] == '1') {\n dfs(grid, i, j);\n ++num_islands;\n }\n }\n }\n return num_islands;\n }\n};", "memory": "20581" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int count = 0;\n for (int i = 0; i < grid.size(); i++)\n {\n for (int j = 0; j < grid[0].size(); j++)\n {\n if (grid[i][j] == '1')\n {\n count++;\n this->dfs(grid, i, j);\n }\n }\n }\n return count;\n }\nprivate:\n void dfs(vector<vector<char>>& grid, int i, int j)\n {\n vector<pair<int,int>> d = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};\n grid[i][j] = '0';\n for (auto [di, dj] : d)\n {\n if ( (i + di >= 0) &&\n (i + di < grid.size()) &&\n (j + dj >= 0) &&\n (j + dj < grid[0].size()) &&\n (grid[i + di][j + dj] == '1'))\n {\n this->dfs(grid, i+di, j+dj);\n }\n }\n }\n};", "memory": "20915" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
2
{ "code": "class Solution {\npublic:\n void check(vector<vector<char>>& grid, int x, int y){\n if(x < 0 || x >= grid.size() || y < 0 || y >= grid[0].size() || grid[x][y] == '0'){\n return ;\n }\n grid[x][y] = '0';\n vector<pair<int, int>> d = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};\n for(auto i: d){\n check(grid, x+i.first, y+i.second);\n }\n }\n int numIslands(vector<vector<char>>& grid) {\n int ret = 0;\n int m = grid.size(), n = grid[0].size();\n for(int i = 0; i < m; i++){\n for(int j = 0; j < n; j++){\n if(grid[i][j] == '1'){\n ret++;\n check(grid, i, j);\n }\n }\n }\n return ret;\n }\n};", "memory": "20915" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
2
{ "code": "// Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.\n\n// An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.\n \nclass Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\nint res = 0;\n\n if (grid.size() == 0 || grid[0].size() == 0) {\n return 0;\n }\n\n for (int i = 0; i < grid.size(); ++i) {\n for (int j = 0; j < grid[0].size(); ++j) {\n if (grid[i][j] == '1') {\n res += 1;\n vector<pair<int, int>> stack;\n stack.emplace_back(i, j);\n\n while (!stack.empty()) {\n pair<int, int> top = stack.back();\n stack.pop_back();\n\n if (top.first >= 0 && top.first < grid.size() && top.second >= 0 && top.second < grid[0].size() && grid[top.first][top.second] == '1') {\n grid[top.first][top.second] = '0';\n stack.emplace_back(top.first - 1, top.second);\n stack.emplace_back(top.first + 1, top.second);\n stack.emplace_back(top.first, top.second - 1);\n stack.emplace_back(top.first, top.second + 1);\n }\n \n }\n }\n }\n }\n return res;\n }\n\n};", "memory": "21249" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
2
{ "code": "// Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.\n\n// An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.\n \nclass Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\nint res = 0;\n\n if (grid.size() == 0 || grid[0].size() == 0) {\n return 0;\n }\n\n for (int i = 0; i < grid.size(); ++i) {\n for (int j = 0; j < grid[0].size(); ++j) {\n if (grid[i][j] == '1') {\n res += 1;\n vector<pair<int, int>> stack;\n stack.emplace_back(i, j);\n\n while (!stack.empty()) {\n pair<int, int> top = stack.back();\n stack.pop_back();\n\n if (top.first >= 0 && top.first < grid.size() && top.second >= 0 && top.second < grid[0].size() && grid[top.first][top.second] == '1') {\n grid[top.first][top.second] = '0';\n stack.emplace_back(top.first - 1, top.second);\n stack.emplace_back(top.first + 1, top.second);\n stack.emplace_back(top.first, top.second - 1);\n stack.emplace_back(top.first, top.second + 1);\n }\n \n }\n }\n }\n }\n return res;\n }\n\n};", "memory": "21249" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
2
{ "code": "class Solution\n{\n const std::array<int, 5> OFFSETS = {-1, 0, 1, 0, -1};\n\npublic:\n int numIslands(std::vector<std::vector<char>> &grid)\n {\n int M = grid.size(), N = grid[0].size();\n int count = 0;\n\n for (int r = 0; r < M; r++)\n {\n for (int c = 0; c < N; c++)\n {\n if (grid[r][c] == '0')\n continue;\n\n std::stack<std::pair<int, int>> stack;\n stack.push({r, c});\n\n while (!stack.empty())\n {\n auto [i, j] = stack.top();\n grid[i][j] = '0'; // mark as visited\n stack.pop();\n\n for (int k = 0; k < OFFSETS.size() - 1; k++)\n {\n int ni = i + OFFSETS[k], nj = j + OFFSETS[k + 1];\n if (ni >= 0 && ni < M && nj >= 0 && nj < N && grid[ni][nj] == '1')\n stack.push({ni, nj});\n }\n }\n\n count++;\n }\n }\n\n return count;\n }\n};\n", "memory": "21583" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
2
{ "code": "class Solution\n{\n const std::array<int, 5> OFFSETS = {-1, 0, 1, 0, -1};\n\npublic:\n int numIslands(std::vector<std::vector<char>> &grid)\n {\n int M = grid.size(), N = grid[0].size();\n int count = 0;\n\n for (int r = 0; r < M; r++)\n {\n for (int c = 0; c < N; c++)\n {\n if (grid[r][c] == '0')\n continue;\n\n std::stack<std::pair<int, int>> stack;\n stack.push({r, c});\n grid[r][c] = '0';\n\n while (!stack.empty())\n {\n auto [i, j] = stack.top();\n stack.pop();\n\n for (int k = 0; k < OFFSETS.size() - 1; k++)\n {\n int ni = i + OFFSETS[k], nj = j + OFFSETS[k + 1];\n if (ni >= 0 && ni < M && nj >= 0 && nj < N && grid[ni][nj] == '1')\n {\n stack.push({ni, nj});\n grid[ni][nj] = '0';\n }\n }\n }\n\n count++;\n }\n }\n\n return count;\n }\n};\n", "memory": "21583" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
2
{ "code": "class Solution {\npublic:\n \n int numIslands(vector<vector<char>>& grid) {\n int rows = grid.size();\n int cols = grid[0].size();\n int cc = 0;\n for(int r = 0;r<rows;r++){\n for(int c = 0;c<cols;c++){\n if(grid[r][c]=='0'){\n continue;\n } \n cc++;\n grid[r][c] = '0';\n queue<pair<int,int>> qu;\n qu.push({r,c});\n while(not qu.empty()){\n auto curr = qu.front();\n qu.pop();\n int currRow = curr.first;\n int currCol = curr.second;\n if(currRow-1>=0 and grid[currRow - 1][currCol]=='1'){\n qu.push({currRow - 1,currCol});\n grid[currRow - 1][currCol] = '0';\n }\n if(currRow+1<rows and grid[currRow + 1][currCol] =='1'){\n qu.push({currRow + 1,currCol});\n grid[currRow + 1][currCol] = '0';\n }\n if(currCol-1>=0 and grid[currRow][currCol-1]=='1'){\n qu.push({currRow,currCol-1});\n grid[currRow][currCol-1] = '0';\n }\n if(currCol+1<cols and grid[currRow][currCol+1]=='1'){\n qu.push({currRow,currCol+1});\n grid[currRow][currCol+1] = '0';\n }\n }\n }\n }\n return cc;\n }\n};", "memory": "21916" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
2
{ "code": "#include <vector>\n#include <deque>\n#include <string>\nclass Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int count = 0;\n int rows = grid.size();\n if (rows == 0) return 0;\n int cols = grid[0].size();\n \n // Build the array of explored and unexplored nodes\n vector<vector<bool>> E(rows, vector<bool>(cols, false));\n \n for (int i = 0; i < rows; ++i) {\n for (int j = 0; j < cols; ++j) {\n if (grid[i][j] == '1' && !E[i][j]) {\n count++;\n \n // Perform BFS on the node\n deque<pair<int, int>> q;\n q.push_back({i, j});\n \n while (!q.empty()) {\n pair<int, int> curr = q.back();\n q.pop_back();\n E[curr.first][curr.second] = true;\n \n // Explore neighboring cells\n if (curr.first - 1 >= 0 && grid[curr.first - 1][curr.second] == '1' && !E[curr.first - 1][curr.second]) {\n q.push_back({curr.first - 1, curr.second});\n }\n if (curr.second - 1 >= 0 && grid[curr.first][curr.second - 1] == '1' && !E[curr.first][curr.second - 1]) {\n q.push_back({curr.first, curr.second - 1});\n }\n if (curr.first + 1 < rows && grid[curr.first + 1][curr.second] == '1' && !E[curr.first + 1][curr.second]) {\n q.push_back({curr.first + 1, curr.second});\n }\n if (curr.second + 1 < cols && grid[curr.first][curr.second + 1] == '1' && !E[curr.first][curr.second + 1]) {\n q.push_back({curr.first, curr.second + 1});\n }\n }\n }\n }\n }\n \n return count;\n }\n};", "memory": "21916" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\n void dfs(int r, int c, vector<vector<int>> &vis, vector<vector<char>> &grid) {\n int m = grid.size();\n int n = grid[0].size();\n vis[r][c] = 1;\n vector<int> dr = {-1, 0, 1, 0};\n vector<int> dc = {0, 1, 0, -1};\n for(int i = 0; i < 4; i++) {\n int nr = r + dr[i];\n int nc = c + dc[i];\n if(nr >= 0 && nr < m && nc >= 0 && nc < n && !vis[nr][nc] && grid[nr][nc] == '1') {\n dfs(nr, nc, vis, grid);\n }\n }\n }\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n vector<vector<int>> vis(m, vector<int>(n, 0));\n int ans = 0;\n for(int i = 0; i < m; i++) {\n for(int j = 0; j < n; j++) {\n if(!vis[i][j] && grid[i][j] == '1') {\n dfs(i, j, vis, grid);\n ans++;\n }\n }\n }\n return ans;\n }\n};", "memory": "24253" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\n void bfs(vector<vector<char>>& adj, int i, int j, vector<vector<int>> &vis){\n int n = adj.size();\n int m = adj[0].size();\n queue<pair<int,int>> q;\n q.push({i,j});\n vis[i][j] = 1;\n \n while(!q.empty()){\n int row = q.front().first;\n int col = q.front().second;\n q.pop();\n \n // Explore 4 directions: up, down, left, right\n for(int delrow = -1; delrow <= 1; delrow++){\n for(int delcol = -1; delcol <= 1; delcol++){\n // Ensure only horizontal and vertical moves\n if(abs(delrow) + abs(delcol) == 1){\n int nrow = row + delrow;\n int ncol = col + delcol;\n \n // Check boundaries and visit status\n if(nrow >= 0 && nrow < n && ncol >= 0 && ncol < m && !vis[nrow][ncol] && adj[nrow][ncol] == '1'){\n q.push({nrow, ncol});\n vis[nrow][ncol] = 1;\n }\n }\n }\n }\n }\n }\n\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int count = 0;\n vector<vector<int>> vis(grid.size(), vector<int>(grid[0].size(), 0));\n \n for(int i = 0; i < grid.size(); i++){\n for(int j = 0; j < grid[i].size(); j++){\n if(!vis[i][j] && grid[i][j] == '1'){\n count++;\n bfs(grid, i, j, vis);\n }\n }\n }\n \n return count;\n }\n};\n", "memory": "24253" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int n=grid.size(),m=grid[0].size(),cnt=0,i,j;\n vector<vector<int>>vis(n,vector<int>(m,0));\n for(i=0;i<n;i++)\n {\n for(j=0;j<m;j++)\n {\n if(grid[i][j]=='1' && !vis[i][j])\n {\n cnt++;\n bfs(i,j,vis,grid,n,m);\n }\n }\n }\n return cnt;\n }\n void bfs(int row,int col,vector<vector<int>>&vis,vector<vector<char>>&grid,int n,int m)\n {\n queue<pair<int,int>>q;\n vis[row][col]=1;\n q.push({row,col});\n vector<pair<int, int>> directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\n while(!q.empty())\n {\n int row=q.front().first,col=q.front().second;q.pop();\n for(auto it:directions)\n {\n int nrow=row+it.first,ncol=col+it.second;\n if(nrow>=0&&ncol>=0&&nrow<n&&ncol<m&&grid[nrow][ncol]=='1'&&!vis[nrow][ncol])\n {\n q.push({nrow,ncol});\n vis[nrow][ncol]=1;\n }\n\n }\n }\n }\n};", "memory": "24586" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int n=grid.size(),m=grid[0].size(),cnt=0,i,j;\n vector<vector<int>>vis(n,vector<int>(m,0));\n for(i=0;i<n;i++)\n {\n for(j=0;j<m;j++)\n {\n if(grid[i][j]=='1' && !vis[i][j])\n {\n cnt++;\n bfs(i,j,vis,grid,n,m);\n }\n }\n }\n return cnt;\n }\n void bfs(int row,int col,vector<vector<int>>&vis,vector<vector<char>>&grid,int n,int m)\n {\n queue<pair<int,int>>q;\n vis[row][col]=1;\n q.push({row,col});\n vector<pair<int, int>> directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\n while(!q.empty())\n {\n int row=q.front().first,col=q.front().second;q.pop();\n for(auto it:directions)\n {\n int nrow=row+it.first,ncol=col+it.second;\n if(nrow>=0&&ncol>=0&&nrow<n&&ncol<m&&grid[nrow][ncol]=='1'&&!vis[nrow][ncol])\n {\n q.push({nrow,ncol});\n vis[nrow][ncol]=1;\n }\n\n }\n }\n }\n};", "memory": "24586" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n if (grid.empty() || grid[0].empty()) return 0;\n\n int n = grid.size();\n int m = grid[0].size();\n int islands = 0;\n\n vector<vector<bool>> visited(n, vector<bool>(m, false));\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n if (grid[i][j] == '1' && !visited[i][j]) {\n islands++;\n dfs(grid, visited, i, j);\n }\n }\n }\n\n return islands;\n }\n\nprivate:\n void dfs(const vector<vector<char>>& grid, vector<vector<bool>>& visited, int i, int j) {\n int n = grid.size();\n int m = grid[0].size();\n\n stack<pair<int, int>> dfsStack;\n dfsStack.push({i, j});\n\n while (!dfsStack.empty()) {\n auto [x, y] = dfsStack.top();\n dfsStack.pop();\n\n if (x < 0 || x >= n || y < 0 || y >= m || grid[x][y] == '0' || visited[x][y]) {\n continue;\n }\n\n visited[x][y] = true;\n\n dfsStack.push({x-1, y});\n dfsStack.push({x+1, y});\n dfsStack.push({x, y-1});\n dfsStack.push({x, y+1});\n }\n }\n};", "memory": "24920" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "#include <iostream>\n#include <vector>\n#include <stack>\n\nusing namespace std;\n\nclass Solution\n{\npublic:\n void searchForConnectedLand(vector<vector<char>> &grid, int i, int j)\n {\n stack<pair<int, int>> landSquare;\n landSquare.push({i, j});\n\n // Search directions (up, down, left, right)\n vector<pair<int, int>> directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\n\n // Now we will check if the stack has any land('1')\n while (!landSquare.empty())\n {\n pair<int, int> currentSquare = landSquare.top();\n landSquare.pop();\n int x = currentSquare.first;\n int y = currentSquare.second;\n // We check first if we are out of bounds and if the coordinates are still an land.\n if (x < 0 || x >= grid.size() || y < 0 || y >= grid[0].size() || grid[x][y] != '1')\n {\n continue;\n }\n\n // Now we mark the square as visited(counted in the island)\n grid[x][y] = '0';\n\n // Now I add the squares around the current one to stack to be searched\n for (auto direction : directions)\n {\n int newSquareX = x + direction.first;\n int newSquareY = y + direction.second;\n\n landSquare.push({newSquareX, newSquareY});\n }\n }\n }\n\n int numIslands(vector<vector<char>> &grid)\n {\n if (grid.empty() || grid[0].empty())\n {\n return 0;\n }\n\n int numberOfIslands = 0;\n\n for (int i = 0; i < grid.size(); i++)\n {\n for (int j = 0; j < grid[0].size(); j++)\n {\n if (grid[i][j] == '1')\n {\n numberOfIslands++;\n searchForConnectedLand(grid, i, j);\n }\n }\n }\n\n return numberOfIslands;\n }\n};", "memory": "24920" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n void dfs(vector<vector<char>>& grid, int i, int j, int m, int n){\n queue<pair<int, int>> q;\n q.push(make_pair(i, j));\n \n\n while (!q.empty()) {\n pair<int, int> top = q.front();\n q.pop();\n\n int x = top.first;\n int y = top.second;\n\n if (x < 0 || x >= m || y < 0 || y >= n || grid[x][y] != '1') {\n continue;\n }\n\n grid[x][y] = '0'; // Mark the visited cell as '0'\n\n q.push(make_pair(x + 1, y));\n q.push(make_pair(x - 1, y));\n q.push(make_pair(x, y + 1));\n q.push(make_pair(x, y - 1));\n }\n \n }\n\n int numIslands(vector<vector<char>>& grid) {\n\n int m = grid.size(), n = grid[0].size(), islands = 0;\n\n for(int i = 0; i < m; i++){\n for(int j = 0; j < n; j++){\n if(grid[i][j] == '1'){\n dfs(grid, i, j, m, n);\n islands++;\n }\n }\n }\n return islands;\n }\n};", "memory": "25254" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n vector<vector<bool>> visited(m, vector<bool> (n, false));\n\n auto process = [&](queue<tuple<int, int>>& q, int _i, int _j){\n bool inbound = (_i >= 0 && _i < m && _j >= 0 && _j < n);\n if (inbound && !visited[_i][_j]) {\n visited[_i][_j] = true;\n if (grid[_i][_j] == '1') {\n q.push({_i+1, _j});\n q.push({_i, _j+1});\n q.push({_i-1, _j});\n q.push({_i, _j-1});\n }\n };\n };\n\n int count = 0;\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n if (!visited[i][j] && grid[i][j] == '1') {\n count++;\n queue<tuple<int, int>> q;\n process(q, i, j);\n while (!q.empty()) {\n auto [_i, _j] = q.front();\n q.pop();\n process(q, _i, _j);\n }\n }\n }\n }\n\n return count;\n }\n};", "memory": "25254" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int numIslands(vector<vector<char>>& grid) {\n vector<vector<bool>> visited;\n vector<pair<int, int>> directions;\n int ans = 0;\n directions.push_back(make_pair(1, 0));\n directions.push_back(make_pair(-1, 0));\n directions.push_back(make_pair(0, -1));\n directions.push_back(make_pair(0, 1));\n\n visited.resize(grid.size());\n for(int i = 0; i < visited.size(); i++){\n visited[i].resize(grid[0].size());\n }\n\n for(int i = 0; i < grid.size(); i++){\n for(int j = 0; j < grid[i].size(); j++){\n if(!visited[i][j] && grid[i][j] == '1'){\n BFS(i, j, grid, visited, directions);\n ans++;\n }\n }\n }\n return ans;\n }\n\n void BFS(int a, int b, vector<vector<char>>& grid, vector<vector<bool>>& visited, vector<pair<int, int>>& directions){\n queue<pair<int, int>> q;\n q.push(make_pair(a, b));\n while(!q.empty()){\n a = q.front().first;\n b = q.front().second; q.pop();\n if(a >= grid.size() || b >= grid[0].size() || a < 0 || b < 0 || grid[a][b] != '1' || visited[a][b]){\n continue;\n }\n visited[a][b] = true;\n for(int i = 0; i < directions.size(); i++){\n q.push(make_pair(directions[i].first + a, directions[i].second + b));\n }\n }\n }\n};", "memory": "25588" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n using Matrix = std::vector<std::vector<char>>;\n using Coordinate = std::pair<int64_t, int64_t>;\n\n std::stack<Coordinate> m_Stack;\n std::vector<std::vector<bool>> m_Visited;\n\n std::array<Coordinate, 4> getNeighbours(int64_t x, int64_t y) {\n std::array<Coordinate, 4> neighbours;\n\n constexpr int16_t adjX[] = {-1, 1, 0, 0};\n constexpr int16_t adjY[] = {0, 0, -1, 1};\n\n for (size_t i = 0; i < 4; i++) {\n neighbours[i] = Coordinate{x + adjX[i], y + adjY[i]};\n }\n\n return neighbours;\n }\n\n bool isValid(const Matrix& m, int64_t x, int64_t y) {\n assert(!m.empty());\n\n const size_t rows = m.size();\n const size_t cols = m[0].size();\n\n return x >= 0 && y >= 0 && x < cols && y < rows;\n }\n\n bool visitShape(const Matrix& m, int64_t x, int64_t y) {\n if (m[y][x] != '1')\n return false;\n\n if (m_Visited[y][x])\n return false;\n\n m_Visited[y][x] = true;\n std::queue<Coordinate> bfsQueue;\n bfsQueue.emplace(x, y);\n\n while (!bfsQueue.empty()) {\n const auto [currX, currY] = bfsQueue.front();\n bfsQueue.pop();\n\n const auto neighbours = getNeighbours(currX, currY);\n for (const auto& [adjX, adjY] : neighbours) {\n if (isValid(m, adjX, adjY) && !m_Visited.at(adjY).at(adjX)) {\n m_Visited[adjY][adjX] = true;\n if (m[adjY][adjX] == '1') {\n bfsQueue.emplace(adjX, adjY);\n } else {\n m_Stack.emplace(adjX, adjY);\n }\n }\n }\n }\n\n return true;\n }\n\n size_t numIslands(Matrix& grid) {\n if (grid.empty())\n return 0;\n\n size_t islandCount = 0;\n m_Visited = std::vector<std::vector<bool>>(\n grid.size(), std::vector<bool>(grid[0].size(), false));\n\n if (!visitShape(grid, 0, 0)) {\n m_Stack.emplace(0, 0);\n m_Visited[0][0] = true;\n } else {\n islandCount++;\n }\n\n while (!m_Stack.empty()) {\n const auto& [currX, currY] = m_Stack.top();\n m_Stack.pop();\n\n const auto neighbours = getNeighbours(currX, currY);\n for (const auto& [adjX, adjY] : neighbours) {\n if (isValid(grid, adjX, adjY) && !m_Visited[adjY][adjX]) {\n if (!visitShape(grid, adjX, adjY)) {\n m_Stack.emplace(adjX, adjY);\n m_Visited[adjY][adjX] = true;\n } else {\n islandCount++;\n }\n }\n }\n }\n\n return islandCount;\n }\n};", "memory": "25588" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int m;\n int n;\n int dx[4] = {1, -1, 0, 0};\n int dy[4] = {0, 0, 1, -1};\n\n bool isSafe(int i, int j) {\n return (i < m && i >= 0 && j < n && j >= 0);\n }\n\n void checkAdj(int i, int j, vector<vector<int>>& visited, vector<vector<char>>& grid) {\n queue<pair<int, int>> q;\n visited[i][j] = 1;\n q.push({i, j});\n while (!q.empty()) {\n int row = q.front().first;\n int col = q.front().second;\n q.pop();\n for (int k = 0; k < 4; k++) {\n int newx = row + dx[k];\n int newy = col + dy[k];\n if (isSafe(newx, newy) && !visited[newx][newy] && grid[newx][newy] == '1') {\n visited[newx][newy] = 1;\n q.push({newx, newy});\n }\n }\n }\n }\n\n int numIslands(vector<vector<char>>& grid) {\n if (grid.empty()) return 0; \n m = grid.size();\n n = grid[0].size();\n vector<vector<int>> visited(m, vector<int>(n, 0));\n vector<pair<int, int>> isLands;\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n if(grid[i][j] == '1'){\n isLands.push_back({i,j});\n }\n }\n }\n int V = isLands.size();\n int islands = 0;\n for (int k = 0; k < V; k++) {\n int i = isLands[k].first;\n int j = isLands[k].second;\n if(!visited[i][j]) {\n islands++;\n checkAdj(i, j, visited, grid);\n }\n }\n return islands;\n }\n};", "memory": "25921" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Coor{\npublic:\n int x, y;\n Coor(int x, int y){\n this->x = x;\n this->y = y;\n }\n};\nint deltaX[4] = {0, 1, -1, 0};\nint deltaY[4] = {1, 0, 0, -1};\n\nclass Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n if(grid.size() == 0 || grid[0].size() == 0) return 0;\n int islandCount = 0;\n int row = grid.size();\n int col = grid[0].size();\n\n vector<vector<bool>> visited(row, vector<bool> (col));\n for (int r = 0; r < row; r++){\n for (int c = 0; c < col; c++){\n if (grid[r][c] == '1' && !visited[r][c]){\n bfs(r, c, grid, visited);\n islandCount++;\n }\n }\n }\n\n return islandCount;\n }\nprivate:\n void bfs(int r, int c, vector<vector<char>>& grid, vector<vector<bool>>& visited){\n queue<Coor*> q;\n Coor* co = new Coor(r, c);\n visited[r][c] = true;\n q.push(co);\n while (!q.empty()){\n Coor* cur = q.front();\n q.pop();\n for (int dir = 0; dir < 4; dir++){\n int newX = cur->x + deltaX[dir];\n int newY = cur->y + deltaY[dir];\n\n //cout << newX << \" \" << newY << \" \" << isValid(newX, newY, grid, visited) << endl;\n if (isValid(newX, newY, grid, visited)){\n Coor* newCo = new Coor(newX, newY);\n q.push(newCo);\n visited[newX][newY] = true;\n }\n }\n\n }\n }\n\n bool isValid(int r, int c, vector<vector<char>>& grid, vector<vector<bool>>& visited){\n int row = grid.size();\n int col = grid[0].size();\n\n if (r < 0 || r >= row || c < 0 || c >= col) return false;\n if (visited[r][c] == true) return false;\n return grid[r][c] == '0' ? false : true;\n }\n\n};", "memory": "25921" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n void convert(vector<vector<char>> grid, vector<int> adj[]) {\n int m = grid.size();\n int n = grid[0].size();\n\n for (int i=0; i<m; i++) {\n for (int j=0; j<n; j++) {\n if (grid[i][j] == '0') {\n continue;\n }\n\n int node = i*n + j;\n int next;\n\n // left\n if (j-1>=0 && grid[i][j-1]=='1') {\n next = i*n + j-1;\n adj[node].push_back(next);\n }\n //right\n if (j+1<n && grid[i][j+1]=='1') {\n next = i*n + j+1;\n adj[node].push_back(next);\n }\n //top\n if (i-1>=0 && grid[i-1][j]=='1') {\n next = (i-1)*n + j;\n adj[node].push_back(next);\n }\n //bottom\n if(i+1<m && grid[i+1][j]=='1') {\n next = (i+1)*n + j;\n adj[node].push_back(next);\n }\n\n if (adj[node].size() == 0) {\n adj[node].push_back(node);\n }\n }\n }\n }\n\n void dfs(int node, vector<int> adj[], vector<int>& visited) {\n visited[node] = 1;\n cout << node << \" \";\n\n for (int i=0; i<adj[node].size(); i++) {\n int next = adj[node][i];\n if (!visited[next]) dfs(next, adj, visited);\n }\n }\n\n int numIslands(vector<vector<char>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n\n vector<int> adj[m*n];\n convert(grid, adj);\n\n for (int i=0; i<m*n; i++) {\n cout << i << \": \";\n for (int j=0; j<adj[i].size(); j++) {\n cout << adj[i][j] << \" \";\n }\n cout << endl;\n }\n\n vector<int> visited(m*n, 0);\n\n int count = 0;\n for (int i=0; i<m*n; i++) {\n if (!visited[i] && adj[i].size()) {\n count++;\n dfs(i, adj, visited);\n }\n }\n\n return count;\n }\n};", "memory": "26255" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n void convert(vector<vector<char>> grid, vector<int> adj[]) {\n int m = grid.size();\n int n = grid[0].size();\n\n for (int i=0; i<m; i++) {\n for (int j=0; j<n; j++) {\n if (grid[i][j] == '0') {\n continue;\n }\n\n int node = i*n + j;\n int next;\n\n for (int deli=-1; deli<=1; deli++) {\n for (int delj=-1; delj<=1; delj++) {\n int next = (i+deli)*n + (j+delj);\n \n if (i+deli>=0 && i+deli<m && j+delj>=0 && j+delj<n &&\n grid[i+deli][j+delj]=='1' && deli!=delj && deli!=-delj) {\n adj[node].push_back(next);\n }\n }\n }\n // // left\n // if (j-1>=0 && grid[i][j-1]=='1') {\n // next = i*n + j-1;\n // adj[node].push_back(next);\n // }\n // //right\n // if (j+1<n && grid[i][j+1]=='1') {\n // next = i*n + j+1;\n // adj[node].push_back(next);\n // }\n // //top\n // if (i-1>=0 && grid[i-1][j]=='1') {\n // next = (i-1)*n + j;\n // adj[node].push_back(next);\n // }\n // //bottom\n // if(i+1<m && grid[i+1][j]=='1') {\n // next = (i+1)*n + j;\n // adj[node].push_back(next);\n // }\n\n if (adj[node].size() == 0) {\n adj[node].push_back(node);\n }\n }\n }\n }\n\n void dfs(int node, vector<int> adj[], vector<int>& visited) {\n visited[node] = 1;\n cout << node << \" \";\n\n for (int i=0; i<adj[node].size(); i++) {\n int next = adj[node][i];\n if (!visited[next]) dfs(next, adj, visited);\n }\n }\n\n int numIslands(vector<vector<char>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n\n vector<int> adj[m*n];\n convert(grid, adj);\n\n for (int i=0; i<m*n; i++) {\n cout << i << \": \";\n for (int j=0; j<adj[i].size(); j++) {\n cout << adj[i][j] << \" \";\n }\n cout << endl;\n }\n\n vector<int> visited(m*n, 0);\n\n int count = 0;\n for (int i=0; i<m*n; i++) {\n if (!visited[i] && adj[i].size()) {\n count++;\n dfs(i, adj, visited);\n }\n }\n\n return count;\n }\n};", "memory": "26255" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n void visit(vector<vector<char>>& grid,vector<vector<bool>>& visited,int m,int n, int row, int col){\n queue<pair<int,int>> q;\n q.push(make_pair(m,n));\n visited[m][n]=true;\n vector<vector<int>> dir={{-1,0},{1,0},{0,-1},{0,1}};\n while(!q.empty()){\n int r=q.front().first;\n int c=q.front().second;\n q.pop();\n for(int i=0;i<4;i++){\n if(r+dir[i][0]>=0 && r+dir[i][0]<row && c+dir[i][1]>=0 && c+dir[i][1]<col && !visited[r+dir[i][0]][c+dir[i][1]] && grid[r+dir[i][0]][c+dir[i][1]]=='1'){\n q.push(make_pair(r+dir[i][0],c+dir[i][1]));\n visited[r+dir[i][0]][c+dir[i][1]]=true;\n }\n }\n }\n }\n int numIslands(vector<vector<char>>& grid) {\n int m=grid.size();\n int n=grid[0].size();\n vector<vector<bool>> visited(m,vector<bool>(n,false));\n int sum=0;\n for(int i=0;i<m;i++){\n for(int j=0;j<n;j++){\n if(grid[i][j]=='1' && visited[i][j]==false){\n visit(grid,visited,i,j,m,n);\n sum++;\n }\n }\n }\n return sum;\n }\n};", "memory": "26589" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\n void dfs (int v, vector<vector<int>>& vec, vector<bool>& used){\n used[v] = true;\n for(auto& it: vec[v]){\n if(!used[it]){\n dfs(it, vec, used);\n }\n }\n }\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n\n vector<vector<int>> vec(n*m);\n for(int i = 0; i <n; ++i){\n for(int j = 0; j< m; ++j){\n if(grid[i][j] == '0'){\n continue;\n }\n int number = i * m + j;\n if( j < m-1 && grid[i][j + 1] == '1'){\n vec[number].push_back(number + 1);\n\n }\n if(j > 0 && grid[i][j-1] == '1'){\n vec[number].push_back(number - 1);\n }\n if(i > 0 && grid[i-1][j] == '1'){\n vec[number].push_back(number - m);\n }\n if(i < n-1 && grid[i+1][j] == '1'){\n vec[number].push_back(number + m);\n }\n }\n }\n vector<bool> used(n*m, false);\n int ans = 0;\n for(int i = 0; i <n; ++i){\n for(int j = 0; j< m; ++j){\n int number = i*m + j;\n if(grid[i][j] == '1' && !used[number]){\n \n dfs(number, vec, used);\n ans++;\n }\n \n }\n }\n return ans;\n }\n};", "memory": "26589" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n #define F first\n #define S second\n int m,n;\n int cell[310][310];\n int vis[310][310];\n using state=pair<int,int>;\n int dx[4]={-1,0,1,0};\n int dy[4]={0,-1,0,1};\n\n bool valid(int x,int y){\n if(x<0 || x>=m || y<0 || y>=n || cell[x][y]==0) return false;\n return true;\n }\n\n vector<state> neigh(state node){\n vector<state> nodes;\n for(int k=0;k<4;k++){\n int nx=node.F+dx[k];\n int ny=node.S+dy[k];\n if(valid(nx,ny)){\n cell[nx][ny]=0;\n nodes.push_back({nx,ny});\n }\n }\n return nodes;\n }\n\n void bfs(int i,int j){\n queue<state> q;\n q.push({i,j});\n // vis[i][j]=1;\n cell[i][j]=0;\n\n while(!q.empty()){\n state node = q.front();q.pop();\n // int x=node.F,y=node.S;\n for(auto v:neigh(node)){\n // vis[v.F][v.S]=1;\n cell[v.F][v.S]=0;\n q.push(v);\n }\n }\n\n }\n int numIslands(vector<vector<char>>& grid) {\n // vector<vector<int>> cell(m,vector<int>(n,0));\n m=grid.size();\n n=grid[0].size();\n for(int i=0;i<m;i++){\n for(int j=0;j<n;j++){\n cell[i][j]=grid[i][j]-'0';\n // vis[i][j]=0;\n }\n }\n \n int comp=0;\n for(int i=0;i<m;i++){\n for(int j=0;j<n;j++){\n if(cell[i][j]==1 && !vis[i][j]){\n comp++;\n bfs(i,j);\n }\n }\n }\n return comp;\n\n }\n};", "memory": "26923" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n #define F first\n #define S second\n int m,n;\n int cell[310][310];\n int vis[310][310];\n using state=pair<int,int>;\n int dx[4]={-1,0,1,0};\n int dy[4]={0,-1,0,1};\n\n vector<state> neigh(state node){\n vector<state> nodes;\n for(int k=0;k<4;k++){\n int nx=node.F+dx[k];\n int ny=node.S+dy[k];\n if(nx<0 || nx>=m || ny<0 || ny>=n || cell[nx][ny]==0){\n continue;\n }else{\n cell[nx][ny]=0;\n nodes.push_back({nx,ny});\n }\n }\n return nodes;\n }\n\n void bfs(int i,int j){\n queue<state> q;\n q.push({i,j});\n // vis[i][j]=1;\n cell[i][j]=0;\n\n while(!q.empty()){\n state node = q.front();q.pop();\n // int x=node.F,y=node.S;\n for(auto v:neigh(node)){\n // vis[v.F][v.S]=1;\n cell[v.F][v.S]=0;\n q.push(v);\n }\n }\n\n }\n int numIslands(vector<vector<char>>& grid) {\n // vector<vector<int>> cell(m,vector<int>(n,0));\n m=grid.size();\n n=grid[0].size();\n for(int i=0;i<m;i++){\n for(int j=0;j<n;j++){\n cell[i][j]=grid[i][j]-'0';\n // vis[i][j]=0;\n }\n }\n \n int comp=0;\n for(int i=0;i<m;i++){\n for(int j=0;j<n;j++){\n if(cell[i][j]==1 && !vis[i][j]){\n comp++;\n bfs(i,j);\n }\n }\n }\n return comp;\n\n }\n};", "memory": "26923" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int m=grid.size();\n int n=grid[0].size();\n int ans=0;\n // vector<vector<bool>> vis(m,vector<bool>(n,false));\n for(int i=0; i<m; i++)\n {\n for(int j=0; j<n; j++)\n {\n if(grid[i][j]=='1')\n {\n ans++;\n queue<vector<int>> pos;\n pos.push({i,j});\n while(!pos.empty())\n {\n int row=pos.front()[0];\n int col=pos.front()[1];\n pos.pop();\n if(grid[row][col]=='0' )\n {\n continue;\n }\n grid[row][col]='0';\n if(row+1<m && grid[row+1][col]=='1') pos.push({row+1,col});\n if(col+1<n && grid[row][col+1]=='1') pos.push({row,col+1});\n if(row-1>=0 && grid[row-1][col]=='1') pos.push({row-1,col});\n if(col-1>=0 && grid[row][col-1]=='1') pos.push({row,col-1});\n }\n }\n }\n }\n\n return ans;\n \n }\n};\n\n\n// class Solution {\n// public:\n// int numIslands(vector<vector<char>>& grid) {\n// int m=grid.size();\n// int n=grid[0].size();\n// int ans=0;\n// vector<vector<bool>> vis(m,vector<bool>(n,false));\n// for(int i=0; i<m; i++)\n// {\n// for(int j=0; j<n; j++)\n// {\n// if(grid[i][j]=='1' && vis[i][j]==false)\n// {\n// ans++;\n// queue<vector<int>> pos;\n// pos.push({i,j});\n// while(!pos.empty())\n// {\n// int row=pos.front()[0];\n// int col=pos.front()[1];\n// pos.pop();\n// if(row<0 || row>=m || col<0 || col>=n || grid[row][col]=='0' || vis[row][col]==true)\n// {\n// continue;\n// }\n// vis[row][col]=true;\n// pos.push({row+1,col});\n// pos.push({row,col+1});\n// pos.push({row-1,col});\n// pos.push({row,col-1});\n// }\n// }\n// }\n// }\n\n// return ans;\n \n// }\n// };", "memory": "27256" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int numIslands(vector<vector<char>> &grid) {\n int n = grid.size(),\n m = grid[0].size(),\n num_islands = 0;\n \n const auto neighbors = [&](int a, int b) -> vector<pair<int, int>> {\n vector<pair<int, int>> res;\n \n for (const auto& [dx, dy] : DIRS) {\n int c = a+dx, \n d = b+dy;\n \n if (c < 0 || c >= n || d < 0 || d >= m) {\n continue;\n }\n \n res.push_back({c, d});\n }\n \n return res;\n };\n \n stack<pair<int, int>> S;\n \n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < m; ++j) {\n if (grid[i][j] == '0') {\n continue;\n }\n \n // dfs to visit all cells in island\n \n grid[i][j] = '0';\n S.push({i, j});\n ++num_islands;\n\n while (!S.empty()) {\n const auto& [a, b] = S.top();\n S.pop();\n\n for (const auto& [c, d] : neighbors(a,b)) {\n if (grid[c][d] == '0') {\n continue;\n }\n \n grid[c][d] = '0';\n S.push({c, d});\n }\n }\n }\n }\n \n return num_islands;\n }\nprivate:\n const vector<pair<int, int>> DIRS = {{-1,0}, {0,-1}, {1,0}, {0,1}};\n};\n", "memory": "27256" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n const size_t height = grid.size();\n const size_t width = grid.back().size();\n \n int count = 0;\n vector<vector<bool>> visited(height, vector<bool>(width));\n for (size_t row = 0; row < height; ++row) {\n for (size_t col = 0; col < width; ++col) {\n if (visited[row][col] || grid[row][col] == '0') {\n continue;\n }\n \n ++count;\n deque<pair<size_t, size_t>> queue{{row, col}};\n while (!queue.empty()) {\n auto [land_row, land_col] = queue.front();\n queue.pop_front();\n visited[land_row][land_col] = true;\n for (auto [dy, dx] : vector<pair<int, int>>{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}) {\n size_t next_row = land_row + dy;\n size_t next_col = land_col + dx;\n if (next_row >= height || next_col >= width) {\n continue;\n }\n if (visited[next_row][next_col]) {\n continue;\n }\n if (grid[next_row][next_col] == '0') {\n continue;\n }\n queue.emplace_back(next_row, next_col);\n visited[next_row][next_col] = true;\n }\n }\n }\n }\n \n return count;\n }\n};", "memory": "27590" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int r=grid.size(); \n int c=grid[0].size();\n vector<vector<int>> vis(r,vector<int>(c,0));\n int cnt=0;\n\n for(int i=0;i<r;i++){\n for(int j=0;j<c;j++){\n if(vis[i][j]==0 && grid[i][j]=='1') {\n vis[i][j]=1; cnt++;\n \n queue<vector<int>> que; que.push({i,j});\n \n while(!que.empty()){\n int rcur=que.front()[0];\n int ccur=que.front()[1]; que.pop(); \n\n for(int m=rcur-1;m<=rcur+1;m++){\n for(int n=ccur-1;n<=ccur+1;n++){\n\n if(m>=0 && n>=0 && m<r && n<c && abs(m-rcur)+abs(n-ccur)==1\n && vis[m][n]==0 && grid[m][n]=='1'){\n\n vis[m][n]=1; \n que.push({m,n});\n }\n }\n }\n \n }\n }\n }\n }\n return cnt;\n \n \n \n }\n};", "memory": "27590" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n void bfs(vector<vector<char>>& grid, vector<vector<bool>>& visited, int row, int col, int n, int m) {\n visited[row][col] = true;\n\n queue<pair<int, int>> q;\n q.push({row, col});\n\n while (!q.empty()) {\n auto front = q.front();\n q.pop();\n\n int r = front.first;\n int c = front.second;\n\n vector<pair<int, int>> directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\n\n for (auto dir : directions) {\n int newRow = r + dir.first;\n int newCol = c + dir.second;\n\n if (newRow >= 0 && newRow < n && newCol >= 0 && newCol < m && grid[newRow][newCol] == '1' && !visited[newRow][newCol]) \n {\n visited[newRow][newCol] = true;\n q.push({newRow, newCol});\n }\n }\n }\n }\n\n int numIslands(vector<vector<char>>& grid) {\n int row = grid.size();\n if (row == 0) return 0;\n int col = grid[0].size();\n\n vector<vector<bool>> visited(row, vector<bool>(col, false));\n\n int count = 0;\n\n for (int i = 0; i < row; i++) {\n for (int j = 0; j < col; j++) {\n if (grid[i][j] == '1' && !visited[i][j]) {\n bfs(grid, visited, i, j, row, col);\n count++;\n }\n }\n }\n\n return count;\n }\n}; ", "memory": "27924" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n void bfs(int row, int col, vector<vector<bool>> &visited, vector<vector<char>>& grid) {\n // mark the node as the visited\n visited[row][col] = true;\n\n // m rows, n columns\n int m = grid.size(), n = grid[0].size();\n\n // queue for BFS traversal\n queue<pair<int, int>> q;\n q.push({row, col});\n\n while(!q.empty()) {\n int curr_row = q.front().first;\n int curr_col = q.front().second;\n q.pop();\n\n // directions in which row & col neighbours to find\n vector<pair<int, int>> directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\n\n for(auto it: directions) {\n // neighbour row & col indices\n int new_row = curr_row + it.first;\n int new_col = curr_col + it.second;\n \n if (new_row >=0 && new_row < m // if row index is valid\n && new_col >=0 && new_col < n // if col index is valid\n && grid[new_row][new_col] == '1' // if node is island\n && !visited[new_row][new_col]) { // if node is not visited\n\n // mark the node as visited & add to queue\n visited[new_row][new_col] = true;\n q.push({new_row, new_col});\n }\n }\n }\n }\n\n int numIslands(vector<vector<char>>& grid) {\n // m rows, n columns\n int m = grid.size(), n = grid[0].size();\n \n // visited matrix for each node in grid\n vector<vector<bool>> visited(m, vector<bool> (n, false));\n\n // number of starting nodes for traversal\n int cnt = 0;\n\n // for each node\n for(int i=0; i<m; i++) {\n for(int j=0; j<n; j++) {\n if(!visited[i][j] && grid[i][j] == '1') {\n cnt++;\n bfs(i, j, visited, grid);\n }\n }\n }\n\n return cnt;\n }\n};", "memory": "27924" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int x[4] = {-1, 0, 1, 0};\n int y[4] = {0, 1, 0, -1};\n\n map<pair<int, int>, bool> visited;\n\n\n int m,n;\n\n void dfs(int i, int j, vector<vector<char>>& grid) {\n visited[pair(i, j)] = true;\n\n for (int k = 0; k < 4; k++) {\n int r = i + x[k];\n int c = j + y[k];\n\n if (r >= 0 && r < m && c >= 0 && c < n && grid[r][c] == '1' && !visited[pair(r,c)]){\n dfs(r,c, grid);\n }\n }\n }\n int numIslands(vector<vector<char>>& grid) {\n m = grid.size();\n n = grid[0].size();\n\n int numOfLand = 0;\n int ans = 0;\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n visited.insert(pair(make_pair(i, j), false));\n if (grid[i][j] == '1') {\n numOfLand++;\n }\n }\n }\n if (numOfLand == 0)\n return 0;\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n if (grid[i][j] == '1' && !visited[pair(i, j)]) {\n ans++;\n dfs(i, j, grid);\n }\n //cout<<\"visited[\"<<i<<\"][\"<<j<<\"] = \"<<visited[pair(i, j)]<<\" \";\n }\n }\n \n return ans;\n }\n};", "memory": "28258" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int x[4] = {-1, 0, 1, 0};\n int y[4] = {0, 1, 0, -1};\n\n map<pair<int, int>, bool> visited;\n\n\n int m,n;\n\n void dfs(int i, int j, vector<vector<char>>& grid) {\n visited[pair(i, j)] = true;\n\n for (int k = 0; k < 4; k++) {\n int r = i + x[k];\n int c = j + y[k];\n\n if (r >= 0 && r < m && c >= 0 && c < n && grid[r][c] == '1' && !visited[pair(r,c)]){\n dfs(r,c, grid);\n }\n }\n }\n int numIslands(vector<vector<char>>& grid) {\n m = grid.size();\n n = grid[0].size();\n\n int numOfLand = 0;\n int ans = 0;\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n visited.insert(pair(make_pair(i, j), false));\n if (grid[i][j] == '1') {\n numOfLand++;\n }\n }\n }\n if (numOfLand == 0)\n return 0;\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n if (grid[i][j] == '1' && !visited[pair(i, j)]) {\n ans++;\n dfs(i, j, grid);\n }\n //cout<<\"visited[\"<<i<<\"][\"<<j<<\"] = \"<<visited[pair(i, j)]<<\" \";\n\n }\n cout<<endl;\n }\n \n return ans;\n }\n};", "memory": "28258" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\n private:\n void bfs(int row, int col, vector<vector<int>> &vis, vector<vector<char>>&grid ){\n int m = grid.size();\n int n = grid[0].size();\n vis[row][col]=1;\n queue<pair<int, int>>q;\n q.push({row, col});\n\n while(!q.empty()){\n int row = q.front().first;\n int col = q.front().second;\n q.pop();\n\n vector<pair<int, int>> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};\n for (auto [dr, dc] : directions) {\n int nr = row + dr;\n int nc = col + dc;\n if(nr>=0 && nr<m && nc>=0 && nc<n && grid[nr][nc]=='1' && !vis[nr][nc]){\n vis[nr][nc]=1;\n q.push({nr,nc});\n }\n }\n }\n }\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n vector<vector<int>>vis(m, vector<int>(n,0));\n int cnt =0;\n for(int i=0; i<m; i++){\n for(int j=0;j<n; j++){\n if(!vis[i][j] && grid[i][j] =='1'){\n cnt++;\n bfs(i,j,vis,grid);\n }\n }\n }\n return cnt;\n \n }\n};", "memory": "28591" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "\nclass Solution {\nprivate:\n void bfs(int sRow, int sCol, vector<vector<int>>& visited, vector<vector<char>>& grid){\n visited[sRow][sCol] = 1;\n queue<pair<int, int>> q;\n q.push({sRow, sCol});\n int rows = grid.size();\n int cols = grid[0].size();\n\n while (!q.empty()) {\n int row = q.front().first;\n int col = q.front().second;\n q.pop();\n\n // Define valid directions for exploring neighbors (up, down, left, right)\n vector<pair<int, int>> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};\n\n for (auto dir : directions) {\n int nRow = row + dir.first;\n int nCol = col + dir.second;\n if (nRow >= 0 && nRow < rows && nCol >= 0 && nCol < cols &&\n grid[nRow][nCol] == '1' && !visited[nRow][nCol]) {\n visited[nRow][nCol] = 1;\n q.push({nRow, nCol});\n }\n }\n }\n }\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int rows = grid.size();\n int cols = grid[0].size();\n\n vector<vector<int>> visited(rows, vector<int>(cols, 0));\n int islandCount = 0;\n for (int row = 0; row < rows; row++) {\n for (int col = 0; col < cols; col++) {\n if (!visited[row][col] && grid[row][col] == '1') {\n islandCount++;\n bfs(row, col, visited, grid);\n }\n }\n }\n return islandCount;\n }\n};\n", "memory": "28591" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n void bfs(vector<vector<char>>&grid, int row, int col)\n {\n queue<pair<char,pair<int,int>>>q;\n char current=grid[row][col];\n grid[row][col]='0';\n q.push({current, {row,col}});\n //map to store visited\n map<pair<int,int>,bool>visited;\n while(!q.empty())\n {\n \n auto front=q.front();\n q.pop();\n int row=front.second.first;\n int col=front.second.second;\n\n //now we need to check for 4 directions \n //checking for up\n int newrow=row-1;\n int newcol=col+0;\n //now check if it is in bounds\n if(newrow>=0 && newrow<grid.size() && newcol>=0 && newcol<grid[0].size() && grid[newrow][newcol]=='1' &&!visited[{newrow,newcol}])\n {\n //push and mark as visited\n q.push({grid[newrow][newcol],{newrow,newcol}});\n visited[{newrow,newcol}]=true;\n //also turn this to 0 so we don't need to do BFS on this now again\n grid[newrow][newcol]='0';\n\n }\n\n //checking for down\n newrow=row+1;\n newcol=col+0;\n if(newrow>=0 && newrow<grid.size() && newcol>=0 && newcol<grid[0].size() && grid[newrow][newcol]=='1' &&!visited[{newrow,newcol}])\n {\n //push and mark as visited\n q.push({grid[newrow][newcol],{newrow,newcol}});\n visited[{newrow,newcol}]=true;\n //also turn this to 0 so we don't need to do BFS on this now again\n grid[newrow][newcol]='0';\n\n }\n\n //checking for right\n newrow=row+0;\n newcol=col+1;\n if(newrow>=0 && newrow<grid.size()&& newcol>=0 && newcol<grid[0].size() && grid[newrow][newcol]=='1' &&!visited[{newrow,newcol}])\n {\n //push and mark as visited\n q.push({grid[newrow][newcol],{newrow,newcol}});\n visited[{newrow,newcol}]=true;\n //also turn this to 0 so we don't need to do BFS on this now again\n grid[newrow][newcol]='0';\n\n }\n\n //checking for left\n newrow=row+0;\n newcol=col-1;\n if(newrow>=0 && newrow<grid.size() && newcol>=0 && newcol<grid[0].size() && grid[newrow][newcol]=='1' &&!visited[{newrow,newcol}])\n {\n //push and mark as visited\n q.push({grid[newrow][newcol],{newrow,newcol}});\n visited[{newrow,newcol}]=true;\n //also turn this to 0 so we don't need to do BFS on this now again\n grid[newrow][newcol]='0';\n\n }\n\n }\n }\n int numIslands(vector<vector<char>>& grid)\n {\n int rows=grid.size();\n int column=grid[0].size();\n int numberofislands=0; \n //the logic is we will call bfs for 1 and we will find all group of 1's thats connected and \n //surrounded by 0, we will also convert them to 0's so they don't come again in out count \n for(int i=0;i<rows;i++)\n {\n for(int j=0;j<column;j++)\n {\n if(grid[i][j]=='1')\n {\n numberofislands+=1;\n bfs(grid,i,j); \n }\n \n }\n }\n return numberofislands;\n }\n};", "memory": "28925" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\nprivate:\n // up, right, down, left\n vector<pair<int,int>> dirs = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};\n set<pair<int,int>> visited;\n\n void bfs(int i, int j, vector<vector<char>>& grid) {\n\n queue<pair<int,int>> q;\n visited.insert({i,j});\n q.push({i,j});\n while (!q.empty()) {\n pair<int,int> cur = q.front();\n q.pop();\n\n for (auto d : dirs) {\n int x = cur.first + d.first;\n int y = cur.second + d.second;\n \n if (x < 0 || x >= grid.size() || y < 0 || y >= grid[0].size()) continue;\n\n if (grid[x][y] == '1' && !visited.contains({x,y})) {\n q.push({x,y});\n visited.insert({x,y});\n }\n }\n }\n }\npublic:\n int numIslands(vector<vector<char>>& grid) {\n \n int n = grid.size();\n int m = grid[0].size();\n int res = 0;\n\n // run bfs from each starting point\n for (int i=0; i<n; i++) {\n for (int j=0; j<m; j++) {\n if (grid[i][j] == '1' && !visited.contains({i,j})) {\n bfs(i, j, grid);\n res++;\n }\n }\n }\n\n return res;\n }\n};", "memory": "28925" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n vector<vector<bool>> isVisited(grid.size(), vector<bool>(grid[0].size()));\n int count = 0;\n for(int i = 0; i < grid.size(); i ++){\n for(int j = 0; j < grid[0].size(); j ++){\n if(grid[i][j] == '1' && !isVisited[i][j]){\n // cout << \"Found new island at \" << i << \" \" << j << endl; \n count ++;\n bfs(i,j, grid, isVisited);\n }\n }\n }\n return count;\n }\n\n void bfs(int i, int j, vector<vector<char>>& grid, vector<vector<bool>> &isVisited){\n queue<vector<int>> q;\n q.push({i, j});\n isVisited[i][j] = true;\n while(!q.empty()){\n vector<int> index = q.front();\n i = index[0];\n j = index[1];\n q.pop();\n\n // cout << \"visiting index \" << i << \" \" << j << endl;\n // printGraph(isVisited);\n \n if(i + 1 < grid.size() && grid[i+1][j] == '1' && !isVisited[i+1][j]){\n isVisited[i+1][j] = true;\n q.push({i+1, j});\n }\n if(j + 1 < grid[0].size() && grid[i][j+1] == '1' && !isVisited[i][j+1]){\n isVisited[i][j+1] = true;\n q.push({i, j+1});\n }\n if(i - 1 >= 0 && grid[i-1][j] == '1' && !isVisited[i-1][j]){\n isVisited[i-1][j] = true;\n q.push({i-1, j});\n }\n if(j - 1 >= 0 && grid[i][j-1] == '1' && !isVisited[i][j-1]){\n isVisited[i][j-1] = true;\n q.push({i, j-1});\n }\n }\n }\n\n void printGraph(vector<vector<bool>> &isVisited){\n cout << \"isVisited:\" << endl;\n for(int i = 0; i < isVisited.size(); i ++){\n for(int j = 0; j < isVisited[0].size(); j ++){\n cout << isVisited[i][j] << \" \";\n }\n cout << endl;\n }\n }\n};", "memory": "31261" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution \n{\n // Steps:\n // 1. move in all four directions from all nodes to valid (land) nodes\n // 2. count number of connected components\n\npublic:\n // Input grid\n vector<vector<char>> grid;\n int m;\n int n;\n // Seen matrix\n vector<vector<bool>> seen;\n // Direction deltas\n vector<vector<int>> directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};\n\n // Method to find the number of islands (connected components)\n int numIslands(vector<vector<char>>& grid)\n {\n // Initialize the grid\n this->grid = grid;\n m = grid.size();\n n = grid[0].size();\n // Initialize seen matrix\n seen = vector<vector<bool>>(m, vector<bool>(n, false));\n\n // Determine connected components using DFS\n int components = 0;\n for(int row = 0; row < m; row++)\n {\n for(int col = 0; col < n; col++)\n {\n if(grid[row][col] == '1' && !seen[row][col])\n {\n components++;\n seen[row][col] = true;\n dfs(row, col);\n }\n }\n }\n\n return components;\n }\n\nprivate:\n // Helper method for DFS\n void dfs(int row, int col)\n {\n // Base cases:\n\n\n // Recurssion:\n for(auto direction : directions)\n {\n int nextRow = row + direction[0];\n int nextCol = col + direction[1];\n if(isValid(nextRow, nextCol) && !seen[nextRow][nextCol])\n {\n\n seen[nextRow][nextCol] = true;\n dfs(nextRow, nextCol);\n \n }\n }\n }\n\n // Helper method to check if the cell is valid\n bool isValid(int row, int col)\n {\n return (0 <= row && row < m) && (0 <= col && col < n) && (grid[row][col] == '1');\n }\n};", "memory": "31261" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n void dfs(int i,int j, vector<vector<char>> &grid, map<pair<int,int>,int> &vis, int m,int n)\n {\n \n if(i+1<m && grid[i+1][j]=='1' && vis[{i+1,j}]==0)\n {\n vis[{i+1,j}]=1;\n dfs(i+1,j,grid,vis,m,n);\n }\n if(i-1>=0 && grid[i-1][j]=='1' && vis[{i-1,j}]==0) \n {\n vis[{i-1,j}]=1;\n dfs(i-1,j,grid,vis,m,n);\n }\n if(j+1<n && grid[i][j+1]=='1' && vis[{i,j+1}]==0) \n {\n vis[{i,j+1}]=1;\n dfs(i,j+1,grid,vis,m,n);\n }\n if(j-1>=0 && grid[i][j-1]=='1' && vis[{i,j-1}]==0) \n {\n vis[{i,j-1}]=1;\n dfs(i,j-1,grid,vis,m,n);\n }\n else return;\n }\n\n int numIslands(vector<vector<char>>& grid) \n {\n int c=0;\n int m=grid.size();\n int n=grid[0].size();\n map<pair<int,int>,int> vis;\n for(int i=0;i<m;i++)\n {\n for(int j=0;j<n;j++)\n {\n if(grid[i][j]=='1' && vis[{i,j}]==0)\n {\n vis[{i,j}]=1;\n c++;\n dfs(i,j,grid,vis,m,n);\n }\n }\n }\n return c;\n }\n};", "memory": "31595" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\nvoid dfs(map<pair<int , int> ,int>&visited , vector<vector<char>>&grid, int i , int j){\n visited[{i ,j}]=1;\n if(i+1<grid.size() && j<grid[i+1].size()){\n if(grid[i+1][j]=='1' && !visited[{i+1 , j}]){\n dfs(visited , grid , i+1 ,j);\n }\n }\n if(i-1>=0 && j<grid[i-1].size()){\n if(grid[i-1][j]=='1' && !visited[{i-1 , j}]){\n dfs(visited , grid , i-1 ,j);\n }\n }\n if(j+1<grid[i].size()){\n if(grid[i][j+1]=='1' && !visited[{i , j+1}]){\n dfs(visited , grid , i,j+1);\n }\n }\n if(j-1>=0){\n if(grid[i][j-1]=='1' && !visited[{i , j-1}]){\n dfs(visited , grid , i,j-1);\n }\n }\n\n}\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int cnt=0;\n map<pair<int,int> ,int>visited;\n for(int i =0 ; i<grid.size() ; i++){\n for(int j =0 ; j<grid[i].size() ; j++){\n if(grid[i][j] == '1' && !visited[{i ,j}]){\n cnt++;\n dfs(visited , grid , i ,j);\n }\n }\n }\n return cnt;\n }\n};", "memory": "31595" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "struct Hash {\n size_t operator() (const std::pair<size_t, size_t> &p) const {\n return std::hash<size_t>()(p.first) ^ std::hash<size_t>()(p.second);\n }\n};\n\nclass Solution {\n std::unordered_set<std::pair<size_t, size_t>, Hash> visited;\n\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int result = 0;\n for (size_t i = 0; i < grid.size(); ++i)\n {\n for (size_t j = 0; j < grid[0].size(); ++j)\n {\n if (grid[i][j] == '0')\n continue;\n if (visited.contains({i, j}))\n continue;\n explore(i, j, grid);\n ++result;\n }\n }\n return result;\n }\n\n void explore(size_t i, size_t j, const vector<vector<char>>& grid)\n {\n if (i >= grid.size() || j >= grid[0].size())\n return;\n if (grid[i][j] == '0')\n return;\n if (visited.contains({i, j}))\n return;\n visited.insert({i, j});\n\n // explore(i - 1, j - 1, grid);\n explore(i, j - 1, grid);\n // explore(i + 1, j - 1, grid);\n\n explore(i - 1, j, grid);\n explore(i + 1, j, grid);\n\n // explore(i - 1, j + 1, grid);\n explore(i, j + 1, grid);\n // explore(i + 1, j + 1, grid);\n }\n};", "memory": "31929" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<pair<int, int>> get_unseen_neighbors(pair<int, int> next, vector<vector<char>> &grid, unordered_set<int>& islandsSeen){\n vector<pair<int, int>> v;\n\n if(next.first != 0){\n if(grid[next.first - 1][next.second] == '1'){\n if(islandsSeen.find((next.first - 1) * grid[0].size()+next.second) == islandsSeen.end()){\n v.push_back(pair (next.first - 1, next.second));\n }\n }\n }\n if(next.first != grid.size() - 1){\n if(grid[next.first + 1][next.second] == '1'){\n if(islandsSeen.find((next.first + 1) * grid[0].size()+ next.second) == islandsSeen.end()){\n v.push_back(pair (next.first + 1, next.second));\n }\n }\n }\n if(next.second != 0){\n if(grid[next.first][next.second - 1] == '1'){\n if(islandsSeen.find((next.first * grid[0].size()) + next.second - 1) == islandsSeen.end()){\n v.push_back(pair (next.first, next.second - 1));\n }\n }\n }\n if(next.second != grid[0].size() - 1){\n if(grid[next.first][next.second + 1] == '1'){\n if(islandsSeen.find(next.first * grid[0].size() + next.second + 1) == islandsSeen.end()){\n v.push_back(pair (next.first, next.second + 1));\n }\n }\n }\n return v; \n }\n\n int numIslands(vector<vector<char>>& grid) {\n int numIslandsFound = 0;\n unordered_set<int> islandsSeen {}; \n for(int row = 0; row < grid.size(); row++){\n for(int column = 0; column < grid[0].size(); column++){\n if(grid[row][column] == '1'){\n if(islandsSeen.find(row * grid[0].size() + column) == islandsSeen.end()){\n numIslandsFound += 1;\n islandsSeen.insert(row * grid[0].size() + column);\n queue<pair<int, int>> islands;\n islands.push(pair<int, int> (row, column));\n while(!islands.empty()){\n pair<int, int> next = islands.front();\n islands.pop();\n vector<pair<int, int>> neighbors = get_unseen_neighbors(next, grid, islandsSeen); \n for(pair<int, int> neighbor: neighbors){\n islandsSeen.insert(neighbor.first * grid[0].size() + neighbor.second);\n islands.push(neighbor); \n }\n }\n } \n }\n \n \n }\n }\n\n return numIslandsFound; \n\n }\n};", "memory": "31929" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int numIslands(const vector<vector<char>>& grid) {\n const int R = grid.size();\n const int C = grid[0].size();\n\n auto cell = [&](int r, int c) -> char { return grid[r][c]; };\n\n int islands = 0;\n struct hash_func { size_t operator()(const std::pair<int, int>& p) const { return p.first ^ p.second; } };\n std::unordered_set<std::pair<int, int>, hash_func> visited;\n\n auto visit = [&](int r, int c, auto&& visit) {\n if (r < 0 || r >= R) return;\n if (c < 0 || c >= C) return;\n if (visited.contains({r, c})) return;\n if (cell(r, c) == '0') return;\n\n visited.emplace(r, c);\n visit(r-1, c, visit);\n visit(r+1, c, visit);\n visit(r, c-1, visit);\n visit(r, c+1, visit);\n };\n\n for (int r = 0; r < R; ++r)\n {\n for (int c = 0; c < C; ++c)\n {\n if (cell(r, c) == '0' || visited.contains({r, c}))\n continue;\n\n islands++;\n // mark all the parts of the island\n visit(r, c, visit);\n visited.emplace(r, c);\n }\n }\n\n return islands;\n }\n};", "memory": "32263" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int islands = 0;\n int rows = grid.size();\n int cols = grid[0].size();\n unordered_set<string> visited;\n\n vector<pair<int, int>> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};\n\n for (int r = 0; r < rows; r++) {\n for (int c = 0; c < cols; c++) {\n if (grid[r][c] == '1' && visited.find(to_string(r) + \",\" + to_string(c)) == visited.end()) {\n islands++;\n bfs(grid, r, c, visited, directions, rows, cols);\n }\n }\n }\n\n return islands; \n }\n\nprivate:\n void bfs(vector<vector<char>>& grid, int r, int c, unordered_set<string>& visited, vector<pair<int, int>>& directions, int rows, int cols) {\n queue<pair<int, int>> q;\n visited.insert(to_string(r) + \",\" + to_string(c));\n q.push({r, c});\n\n while (!q.empty()) {\n auto [row, col] = q.front();\n q.pop();\n\n for (auto [dr, dc] : directions) {\n int nr = row + dr;\n int nc = col + dc;\n if (nr >= 0 && nr < rows && nc >= 0 && nc < cols && grid[nr][nc] == '1' && visited.find(to_string(nr) + \",\" + to_string(nc)) == visited.end()) {\n q.push({nr, nc});\n visited.insert(to_string(nr) + \",\" + to_string(nc));\n }\n }\n }\n }\n}; ", "memory": "32263" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int islands = 0;\n int rows = grid.size();\n int cols = grid[0].size();\n unordered_set<string> visited;\n\n vector<pair<int, int>> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};\n\n for (int r = 0; r < rows; r++) {\n for (int c = 0; c < cols; c++) {\n if (grid[r][c] == '1' && visited.find(to_string(r) + \",\" + to_string(c)) == visited.end()) {\n islands++;\n bfs(grid, r, c, visited, directions, rows, cols);\n }\n }\n }\n\n return islands; \n }\n\nprivate:\n void bfs(vector<vector<char>>& grid, int r, int c, unordered_set<string>& visited, vector<pair<int, int>>& directions, int rows, int cols) {\n queue<pair<int, int>> q;\n visited.insert(to_string(r) + \",\" + to_string(c));\n q.push({r, c});\n\n while (!q.empty()) {\n auto [row, col] = q.front();\n q.pop();\n\n for (auto [dr, dc] : directions) {\n int nr = row + dr;\n int nc = col + dc;\n if (nr >= 0 && nr < rows && nc >= 0 && nc < cols && grid[nr][nc] == '1' && visited.find(to_string(nr) + \",\" + to_string(nc)) == visited.end()) {\n q.push({nr, nc});\n visited.insert(to_string(nr) + \",\" + to_string(nc));\n }\n }\n }\n }\n};", "memory": "32596" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int islands = 0;\n int rows = grid.size();\n int cols = grid[0].size();\n unordered_set<string> visited;\n\n vector<pair<int, int>> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};\n\n for (int r = 0; r < rows; r++) {\n for (int c = 0; c < cols; c++) {\n if (grid[r][c] == '1' && visited.find(to_string(r) + \",\" + to_string(c)) == visited.end()) {\n islands++;\n bfs(grid, r, c, visited, directions, rows, cols);\n }\n }\n }\n\n return islands; \n }\n\nprivate:\n void bfs(vector<vector<char>>& grid, int r, int c, unordered_set<string>& visited, vector<pair<int, int>>& directions, int rows, int cols) {\n queue<pair<int, int>> q;\n visited.insert(to_string(r) + \",\" + to_string(c));\n q.push({r, c});\n\n while (!q.empty()) {\n auto [row, col] = q.front();\n q.pop();\n\n for (auto [dr, dc] : directions) {\n int nr = row + dr;\n int nc = col + dc;\n if (nr >= 0 && nr < rows && nc >= 0 && nc < cols && grid[nr][nc] == '1' && visited.find(to_string(nr) + \",\" + to_string(nc)) == visited.end()) {\n q.push({nr, nc});\n visited.insert(to_string(nr) + \",\" + to_string(nc));\n }\n }\n }\n }\n}; ", "memory": "32596" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int numIslands(vector<vector<char>>& grid) {\n if(grid.size() == 0)return 0;\n vector<vector<int>>dirs = {{0,1},{0,-1},{1,0},{-1,0}};\n int m = grid.size();\n //cols = rows ? grid[0].size() : 0;\n int n = grid[0].size();\n int count = 0;\n queue<pair<int,int>>q;\n for(int i=0;i<m;i++){\n for(int j=0;j<n;j++){\n if(grid[i][j] == '1'){\n count++;\n q.push({i,j});\n while(!q.empty()){\n auto pos = q.front();\n q.pop();\n int u = pos.first;\n int v = pos.second;\n if(grid[u][v] == '1'){\n grid[u][v] = '0';\n for(auto dir : dirs){\n int a = u + dir[0];\n int b = v + dir[1];\n if(a >= 0 && b >= 0 && a < m && b <n)\n q.push({a,b});\n }\n }\n }\n }\n }\n }\n return count;\n }\n \n};", "memory": "32930" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n\n int islands = 0;\n int rows = grid.size();\n int cols = grid[0].size();\n unordered_set<string> visited;\n\n vector<pair<int, int>> directions = {{1,0}, {-1,0}, {0,1},{0,-1}};\n\n for(int i = 0; i < rows; i++){\n for(int j = 0; j < cols; j++){\n if(grid[i][j] == '1' && visited.find(to_string(i)+\",\"+to_string(j)) == visited.end()){\n islands++;\n bfs(grid, i, j, visited, directions, rows, cols);\n }\n }\n }\n return islands;\n }\n\nprivate :\n void bfs(vector<vector<char>>& grid, int r, int c, unordered_set<string>& visited ,vector<pair<int,int>> directions, int rows, int cols){\n queue<pair<int,int>> q;\n visited.insert(to_string(r)+\",\"+to_string(c));\n q.push({r,c});\n\n\n while(!q.empty()){\n auto [row, col] = q.front();\n q.pop();\n\n for(auto [dr, dc] : directions){\n int nr = row + dr;\n int nc = col + dc;\n\n if(nr >=0 && nr < rows && nc >= 0 && nc < cols && grid[nr][nc] == '1' && visited.find(to_string(nr)+\",\"+to_string(nc)) == visited.end()){\n q.push({nr,nc});\n visited.insert(to_string(nr)+\",\"+to_string(nc));\n }\n }\n }\n }\n};", "memory": "32930" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "\nclass Solution {\npublic:\nint numIslands(vector<vector<char>>& grid) {\n int m=grid.size();\n int n=grid[0].size();\n function<int(int)> Find;\n unordered_map<int,int> Parents;\n unordered_map<int,int> Rank;\n Find=[&grid,&Find,&Parents](int i)->int{\n return Parents[i]==i?i:Find(Parents[i]);\n };\n int cnt=0;\n auto Initialize=[&](){\n for(int i=0;i<grid.size();i++)\n for(int j=0;j<grid[0].size();j++)\n {\n if(grid[i][j]=='1')\n {\n Parents[i*n+j]=i*n+j;\n cnt++;\n }\n Rank[i*n+j]=1;\n }\n };\n Initialize();\n auto Union=[&](int x, int y){\n int rootX=Find(x);\n int rootY=Find(y);\n if(rootX!=rootY){\n if(Rank[rootX]>Rank[rootY])\n Parents[rootY]=rootX;\n else if(Rank[rootX]<Rank[rootY])\n Parents[rootX]=rootY;\n else\n {\n Parents[rootY]=rootX;\n Rank[rootX]++;\n }\n cnt--;\n }\n };\n for(int i=0;i<grid.size();i++)\n for(int j=0;j<grid[0].size();j++)\n {\n if(grid[i][j]=='1')\n {\n if(i+1<m&&grid[i+1][j]=='1')\n {\n Union(n*i+j,n*(i+1)+j);\n }\n if(j+1<n&&grid[i][j+1]=='1')\n {\n Union(n*i+j,n*i+j+1);\n }\n }\n }\n return cnt;\n}\n};", "memory": "33264" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n// Converts a 2D grid of chars to an adjacency list of node indices\nvector<vector<int>> convertGridTOAdjList(vector<vector<char>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n vector<vector<int>> adjList(m * n); // adjacency list\n\n auto toIndex = [&](int r, int c) {\n return r * n + c;\n };\n\n // Directions for the 4 possible movements: up, down, left, right\n vector<pair<int, int>> directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\n\n for (int i = 0; i < m; ++i) {\n for (int j = 0; j < n; ++j) {\n if (grid[i][j] == '1') { // Only consider land cells\n int currentIndex = toIndex(i, j);\n for (auto& dir : directions) {\n int ni = i + dir.first;\n int nj = j + dir.second;\n if (ni >= 0 && ni < m && nj >= 0 && nj < n && grid[ni][nj] == '1') {\n int neighborIndex = toIndex(ni, nj);\n adjList[currentIndex].push_back(neighborIndex);\n }\n }\n }\n }\n }\n\n return adjList;\n }\n\n int numIslands(vector<vector<char>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n vector<bool> visited(m * n, false);\n vector<vector<int>> adj = convertGridTOAdjList(grid);\n int count = 0;\n\n for (int i = 0; i < m * n; ++i) {\n if (!visited[i] && grid[i / n][i % n] == '1') {\n count++;\n dfs(i, adj, visited);\n }\n }\n \n return count;\n }\n\n void dfs(int start, const vector<vector<int>>& adj, vector<bool>& visited) {\n stack<int> st;\n st.push(start);\n visited[start] = true;\n\n while (!st.empty()) {\n int curr = st.top();\n st.pop();\n\n for (int i : adj[curr]) {\n if (!visited[i]) {\n visited[i] = true;\n st.push(i);\n }\n }\n }\n }\n};\n", "memory": "33264" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n void islandDFS(pair<int,int> index, vector<vector<char>>& grid, set<pair<int, int>>& seen){\n stack<pair<int,int>> s;\n pair<int,int> curr;\n\n s.push(index);\n\n while (!s.empty()){\n curr = s.top();\n s.pop();\n \n if (seen.count(curr)) continue;\n seen.insert(curr);\n\n // Check all four possible directions\n vector<pair<int, int>> directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};\n for (auto dir : directions) {\n int newRow = curr.first + dir.first;\n int newCol = curr.second + dir.second;\n if (newRow >= 0 && newRow < grid.size() && newCol >= 0 && newCol < grid[0].size() && grid[newRow][newCol] == '1' && !seen.count({newRow, newCol})) {\n s.push({newRow, newCol});\n }\n }\n }\n}\n\n\nint numIslands(vector<vector<char>>& grid) {\n set<pair<int,int>> seen;\n int island_count = 0;\n\n for (int i = 0; i < grid.size(); ++i){\n for (int j = 0; j < grid[0].size(); ++j){\n if (grid[i][j] == '1' && !seen.count({i, j})){\n islandDFS({i, j}, grid, seen);\n island_count++;\n }\n }\n }\n\n return island_count;\n}\n};", "memory": "33598" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n void dfs(vector<vector<char>>& grid, int r, int c) {\n if(r < 0 || c < 0 || r >= grid.size() || c >= grid[0].size() || grid[r][c] == '0') {\n return;\n }\n grid[r][c] = '0';\n\n vector<pair<int,int>> offset{ {1, 0}, {-1, 0}, {0, 1}, {0, -1} };\n for(int i = 0; i < 4; ++i) {\n dfs(grid, r + offset[i].first, c + offset[i].second);\n }\n }\n\n int numIslands(vector<vector<char>>& grid) {\n int ans = 0;\n \n for(int r = 0; r < grid.size(); ++r) {\n for(int c = 0; c < grid[0].size(); ++c) {\n if(grid[r][c] == '1') {\n dfs(grid, r, c);\n ++ans;\n }\n }\n }\n return ans;\n }\n};", "memory": "33598" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n const int m = grid.size();\n const int n = grid[0].size();\n // create graph\n // vector<vector<int>> adj_list(m * n, vector<int>());\n // for (int i = 0; i < m; i++) {\n // for (int j = 0; j < n; j++) {\n // if (grid[i][j] == '0') {\n // continue;\n // }\n // const int node_id = n * i + j;\n // if (i > 0 && grid[i - 1][j] == '1') {\n // adj_list[node_id].push_back(node_id - n);\n // }\n // if (i < grid.size() - 1 && grid[i + 1][j] == '1') {\n // adj_list[node_id].push_back(node_id + n);\n // }\n // if (j > 0 && grid[i][j - 1] == '1') {\n // adj_list[node_id].push_back(node_id - 1);\n // }\n // if (j < grid[i].size() - 1 && grid[i][j + 1] == '1') {\n // adj_list[node_id].push_back(node_id + 1);\n // }\n // }\n // }\n // initialize visited set\n unordered_set<int> visited;\n int nr_cc = 0;\n // iterate over all 1-nodes:\n // * if current node in visited set, continue\n // * find full connected component -> updates visited set\n // * inc nr_cc\n for (int n_id = 0; n_id < m * n; n_id++) {\n const int i = n_id / n;\n const int j = n_id - n * i;\n if (grid[i][j] == '0' || visited.contains(n_id)) {\n continue;\n }\n deque<int> queue{n_id};\n while(!queue.empty()) {\n const int curr_node_id = queue.front();\n queue.pop_front();\n if (visited.contains(curr_node_id)) {\n continue;\n }\n visited.insert(curr_node_id);\n\n vector<int> neighbours;\n const int i_c = curr_node_id / n;\n const int j_c = curr_node_id - n * i_c;\n if (i_c > 0 && grid[i_c - 1][j_c] == '1') {\n neighbours.push_back(curr_node_id - n);\n }\n if (i_c < grid.size() - 1 && grid[i_c + 1][j_c] == '1') {\n neighbours.push_back(curr_node_id + n);\n }\n if (j_c > 0 && grid[i_c][j_c - 1] == '1') {\n neighbours.push_back(curr_node_id - 1);\n }\n if (j_c < grid[i_c].size() - 1 && grid[i_c][j_c + 1] == '1') {\n neighbours.push_back(curr_node_id + 1);\n }\n\n for (const int neighbour_node_id : neighbours) {\n if (!visited.contains(neighbour_node_id)) {\n queue.push_back(neighbour_node_id);\n }\n }\n }\n ++nr_cc;\n }\n return nr_cc;\n }\n};", "memory": "33931" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n const int m = grid.size();\n const int n = grid[0].size();\n // create graph\n // vector<vector<int>> adj_list(m * n, vector<int>());\n // for (int i = 0; i < m; i++) {\n // for (int j = 0; j < n; j++) {\n // if (grid[i][j] == '0') {\n // continue;\n // }\n // const int node_id = n * i + j;\n // if (i > 0 && grid[i - 1][j] == '1') {\n // adj_list[node_id].push_back(node_id - n);\n // }\n // if (i < grid.size() - 1 && grid[i + 1][j] == '1') {\n // adj_list[node_id].push_back(node_id + n);\n // }\n // if (j > 0 && grid[i][j - 1] == '1') {\n // adj_list[node_id].push_back(node_id - 1);\n // }\n // if (j < grid[i].size() - 1 && grid[i][j + 1] == '1') {\n // adj_list[node_id].push_back(node_id + 1);\n // }\n // }\n // }\n // initialize visited set\n unordered_set<int> visited;\n int nr_cc = 0;\n // iterate over all 1-nodes:\n // * if current node in visited set, continue\n // * find full connected component -> updates visited set\n // * inc nr_cc\n for (int n_id = 0; n_id < m * n; n_id++) {\n const int i = n_id / n;\n const int j = n_id - n * i;\n if (grid[i][j] == '0' || visited.contains(n_id)) {\n continue;\n }\n deque<int> queue{n_id};\n while(!queue.empty()) {\n const int curr_node_id = queue.front();\n queue.pop_front();\n // if (visited.contains(curr_node_id)) {\n // continue;\n // }\n visited.insert(curr_node_id);\n\n vector<int> neighbours;\n const int i_c = curr_node_id / n;\n const int j_c = curr_node_id - n * i_c;\n if (i_c > 0 && grid[i_c - 1][j_c] == '1') {\n neighbours.push_back(curr_node_id - n);\n }\n if (i_c < grid.size() - 1 && grid[i_c + 1][j_c] == '1') {\n neighbours.push_back(curr_node_id + n);\n }\n if (j_c > 0 && grid[i_c][j_c - 1] == '1') {\n neighbours.push_back(curr_node_id - 1);\n }\n if (j_c < grid[i_c].size() - 1 && grid[i_c][j_c + 1] == '1') {\n neighbours.push_back(curr_node_id + 1);\n }\n\n for (const int neighbour_node_id : neighbours) {\n if (!visited.contains(neighbour_node_id)) {\n queue.push_back(neighbour_node_id);\n visited.insert(neighbour_node_id);\n }\n }\n }\n ++nr_cc;\n }\n return nr_cc;\n }\n};", "memory": "33931" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n unordered_set<string> visited;\n int numIslands(vector<vector<char>>& grid) {\n const int ROWS = grid.size();\n const int COLS = grid[0].size();\n \n int count = 0;\n for (int i=0; i<ROWS; ++i) {\n for (int j=0; j<COLS; ++j) {\n string str = to_string(i) + \",\" + to_string(j);\n if (grid[i][j] == '1' && visited.find(str) == visited.end()) {\n dfs(i, j, grid);\n count++;\n }\n }\n }\n for (const auto &x : visited)\n cout << x << endl;\n return count;\n }\n\n void dfs(const int x, const int y, vector<vector<char>>& grid) {\n const int ROWS = grid.size();\n const int COLS = grid[0].size();\n string str = to_string(x) + \",\" + to_string(y);\n if (x>=ROWS || \n y>=COLS || \n x<0 || y<0 || visited.find(str) != visited.end() || grid[x][y]=='0') {\n return;\n }\n visited.insert(str);\n dfs(x+1, y, grid);\n dfs(x-1, y, grid);\n dfs(x, y+1, grid);\n dfs(x, y-1, grid);\n }\n};", "memory": "34265" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n unordered_set<string> visited;\n int ROWS;\n int COLS;\n int numIslands(vector<vector<char>>& grid) {\n ROWS = grid.size();\n COLS = grid[0].size();\n \n int count = 0;\n for (int i=0; i<ROWS; ++i) {\n for (int j=0; j<COLS; ++j) {\n string str = to_string(i) + \",\" + to_string(j);\n if (grid[i][j] == '1' && visited.find(str) == visited.end()) {\n dfs(i, j, grid);\n count++;\n }\n }\n }\n for (const auto &x : visited)\n cout << x << endl;\n return count;\n }\n\n void dfs(const int x, const int y, vector<vector<char>>& grid) {\n string str = to_string(x) + \",\" + to_string(y);\n if (x>=ROWS || \n y>=COLS || \n x<0 || y<0 || visited.find(str) != visited.end() || grid[x][y]=='0') {\n return;\n }\n visited.insert(str);\n dfs(x+1, y, grid);\n dfs(x-1, y, grid);\n dfs(x, y+1, grid);\n dfs(x, y-1, grid);\n }\n};", "memory": "34265" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n bool isValid(int i , int j, int r, int c,vector<vector<char>>& grid)\n {\n return i>=0 && i<r && j>=0 && j<c;\n }\n\n int doBFS(vector<vector<bool>>& visited, vector<vector<char>>& grid,int i, int j)\n {\n int r = grid.size();\n int c = grid[0].size();\n vector<vector<int>> dir = {{0,1},{1,0},{0,-1},{-1,0}};\n\n queue<vector<int>> q;\n q.push({i,j});\n visited[i][j] = true;\n while(!q.empty())\n {\n vector<int> curr = q.front();\n q.pop();\n for(int i =0;i<4;i++)\n {\n int next_x = curr[0] + dir[i][0];\n int next_y = curr[1] + dir[i][1];\n if(isValid(next_x,next_y,r,c,grid) && !visited[next_x][next_y] && grid[next_x][next_y] == '1')\n {\n //cout<<\"pos_x : \"<<next_x<<\"pos_y: \"<<next_y<<endl;\n visited[next_x][next_y] = true;\n q.push({next_x,next_y});\n }\n }\n }\n\n return 1;\n }\n\n \n int numIslands(vector<vector<char>>& grid) {\n int r = grid.size();\n int c = grid[0].size();\n vector<vector<bool>> visited(r,vector<bool>(c,false));\n\n int res =0;\n for(int i =0;i<r;i++)\n {\n for(int j =0;j<c;j++)\n {\n if(!visited[i][j] && grid[i][j] == '1')\n {\n res += doBFS(visited,grid,i,j);\n }\n }\n }\n\n return res;\n\n\n \n }\n};", "memory": "34599" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n void bfsCoordinate(vector<vector<char>>& grid, vector<vector<bool>>& visited, int row, int column) {\n queue<vector<int>> toVisit;\n toVisit.push(vector<int>{row, column});\n vector<vector<int>> directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};\n while (!toVisit.empty()) {\n vector<int> coordinates = toVisit.front();\n toVisit.pop();\n int r = coordinates[0];\n int c = coordinates[1];\n for (auto& diff : directions) {\n int dr = diff[0];\n int dc = diff[1];\n int nextR = r + dr;\n int nextC = c + dc;\n\n if (nextR >= 0 && nextC >= 0 && nextR < grid.size() && nextC < grid[0].size() && !visited[nextR][nextC] && grid[nextR][nextC] == '1') {\n visited[nextR][nextC] = true;\n toVisit.push(vector<int>{nextR, nextC});\n }\n }\n }\n }\n int numIslands(vector<vector<char>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n vector<vector<bool>> visited = vector<vector<bool>>(m, vector<bool>(n, false));\n int islands = 0;\n\n for (int r = 0; r < m; r++) {\n for (int c = 0; c < n; c++) {\n if (!visited[r][c] && grid[r][c] == '1') {\n visited[r][c] = true;\n bfsCoordinate(grid, visited, r, c);\n islands++;\n }\n }\n }\n return islands;\n }\n};", "memory": "34599" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int getKey(vector<int> V) {\n return V[0]*1000 + V[1];\n }\n \n vector<int> getCell(int x) {\n return { x/1000, x%1000 };\n }\n \n unordered_map<int, int> parents;\n int find(int x) {\n return parents[x] == x ? x: parents[x] = find(parents[x]);\n }\n \n void makeParent(int a, int b) {\n int pA = find(a);\n int pB = find(b);\n if (pA != pB) {\n parents[pA] = pB;\n }\n }\n \n int numIslands(vector<vector<char>>& A) {\n int n = A.size(), m = A[0].size();\n \n for (int i = 0; i < n; i++)\n for (int j = 0; j < m; j++) {\n if (A[i][j] == '1') {\n parents[getKey({i, j})] = getKey({i, j});\n }\n }\n \n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n if (A[i][j] == '1') {\n if (i+1 < n && A[i+1][j] == '1') \n makeParent(getKey({i, j}), getKey({i+1, j}));\n if (j+1 < m && A[i][j+1] == '1') \n makeParent(getKey({i, j}), getKey({i, j+1}));\n }\n }\n }\n \n \n int ans = 0;\n for (auto [k, v]: parents) {\n if (k == v) ans++;\n }\n \n return ans;\n }\n};", "memory": "34933" }
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n void bfs(map<pair<int,int> , bool>& visited,int row , int col,vector<vector<char>>& grid ){\n queue<pair<int,int>> q;\n\n q.push({row,col});\n visited[{row,col}] = true;\n\n while(!q.empty()){\n pair<int,int> fnode = q.front();\n q.pop();\n int x = fnode.first;\n int y = fnode.second;\n\n int dx[] = {-1,0,1,0};\n int dy[] = {0,1,0,-1};\n for(int i = 0 ; i<4 ;i++){\n int newX = x + dx[i];\n int newY = y + dy[i];\n if(newX >= 0 && newX < grid.size() && newY < grid[0].size() && \n !visited[{newX,newY}] && grid[newX][newY] == '1'){\n q.push({newX, newY});\n visited[{newX,newY}] = true;\n }\n }\n }\n\n }\n\n\n int numIslands(vector<vector<char>>& grid) {\n // applying bfs \n // queue<pair<int,int>> q;\n map<pair<int,int> , bool> visited;\n int count = 0;\n\n // now the traversal of the matrix \n for(int row = 0 ; row < grid.size();row++){\n int n = grid[row].size();\n for(int col = 0 ; col < n ; col++){\n if(!visited[{row,col}] && grid[row][col] == '1'){\n bfs(visited,row,col,grid);\n count++;\n }\n }\n }\n return count;\n \n }\n};", "memory": "34933" }
206
<p>Given the <code>head</code> of a singly linked list, reverse the list, and return <em>the reversed list</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg" style="width: 542px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2,3,4,5] <strong>Output:</strong> [5,4,3,2,1] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex2.jpg" style="width: 182px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2] <strong>Output:</strong> [2,1] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is the range <code>[0, 5000]</code>.</li> <li><code>-5000 &lt;= Node.val &lt;= 5000</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> A linked list can be reversed either iteratively or recursively. Could you implement both?</p>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n\n\ninline bool isDigit(char ch) {\n return (ch >= '0') && (ch <= '9');\n}\n\nvoid reverseListAndPrint(const std::string& s, std::ofstream& out) {\n if (s.size() == 2) {\n out << \"[]\\n\";\n return;\n }\n std::stack<int> st;\n int accumulator = 0;\n bool is_negative = false;\n for (int i = 1; i < s.size(); ++i) {\n if (s[i] == '-') {\n is_negative = true;\n } else if (isDigit(s[i])) {\n accumulator = accumulator * 10 + (s[i] - '0');\n } else {\n if (is_negative) {\n accumulator = -accumulator;\n }\n st.push(accumulator);\n accumulator = 0;\n is_negative = false;\n }\n }\n out << \"[\";\n while (st.size() > 1) {\n out << st.top() << \",\";\n st.pop();\n }\n if (!st.empty()) {\n out << st.top();\n }\n out << \"]\\n\";\n}\n\nbool Solve = [](){\n std::ofstream out(\"user.out\");\n for (std::string s; std::getline(std::cin, s);) {\n reverseListAndPrint(s, out);\n }\n out.flush();\n exit(0);\n return true;\n}();\n\n\nclass Solution {\npublic:\n ListNode* reverseList(ListNode* head) {\n ListNode* back = nullptr;\n while(head){\n ListNode* temp = head->next;\n head->next = back;\n back = head;\n head = temp;\n }\n return back;\n }\n};", "memory": "7700" }
206
<p>Given the <code>head</code> of a singly linked list, reverse the list, and return <em>the reversed list</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg" style="width: 542px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2,3,4,5] <strong>Output:</strong> [5,4,3,2,1] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex2.jpg" style="width: 182px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2] <strong>Output:</strong> [2,1] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is the range <code>[0, 5000]</code>.</li> <li><code>-5000 &lt;= Node.val &lt;= 5000</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> A linked list can be reversed either iteratively or recursively. Could you implement both?</p>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n\n\ninline bool isDigit(char ch) {\n return (ch >= '0') && (ch <= '9');\n}\n\nvoid reverseListAndPrint(const string& s, ofstream& out) {\n if (s.size() == 2) {\n out << \"[]\\n\";\n return;\n }\n stack<int> st;\n int accumulator = 0;\n bool is_negative = false;\n for (int i = 1; i < s.size(); ++i) {\n if (s[i] == '-') {\n is_negative = true;\n } else if (isDigit(s[i])) {\n accumulator = accumulator * 10 + (s[i] - '0');\n } else {\n if (is_negative) {\n accumulator = -accumulator;\n }\n st.push(accumulator);\n accumulator = 0;\n is_negative = false;\n }\n }\n out << \"[\";\n while (st.size() > 1) {\n out << st.top() << \",\";\n st.pop();\n }\n if (!st.empty()) {\n out << st.top();\n }\n out << \"]\\n\";\n}\n\nbool Solve = [](){\n ofstream out(\"user.out\");\n for (string s; getline(std::cin, s);) {\n reverseListAndPrint(s, out);\n }\n out.flush();\n exit(0);\n return true;\n}();\n\n\nclass Solution {\npublic:\n ListNode* reverseList(ListNode* head) {\n ListNode* back = nullptr;\n while(head){\n ListNode* temp = head->next;\n head->next = back;\n back = head;\n head = temp;\n }\n return back;\n }\n};", "memory": "7700" }
206
<p>Given the <code>head</code> of a singly linked list, reverse the list, and return <em>the reversed list</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg" style="width: 542px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2,3,4,5] <strong>Output:</strong> [5,4,3,2,1] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex2.jpg" style="width: 182px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2] <strong>Output:</strong> [2,1] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is the range <code>[0, 5000]</code>.</li> <li><code>-5000 &lt;= Node.val &lt;= 5000</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> A linked list can be reversed either iteratively or recursively. Could you implement both?</p>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n static const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cout.tie(nullptr);\n std::cin.tie(nullptr);\n return true;\n}();\n\ninline bool isDigit(char ch) {\n return (ch >= '0') && (ch <= '9');\n}\n\nvoid reverseListAndPrint(const std::string& s, std::ofstream& out) {\n if (s.size() == 2) {\n out << \"[]\\n\";\n return;\n }\n std::stack<int> st;\n int accumulator = 0;\n bool is_negative = false;\n for (int i = 1; i < s.size(); ++i) {\n if (s[i] == '-') {\n is_negative = true;\n } else if (isDigit(s[i])) {\n accumulator = accumulator * 10 + (s[i] - '0');\n } else {\n if (is_negative) {\n accumulator = -accumulator;\n }\n st.push(accumulator);\n accumulator = 0;\n is_negative = false;\n }\n }\n out << \"[\";\n while (st.size() > 1) {\n out << st.top() << \",\";\n st.pop();\n }\n if (!st.empty()) {\n out << st.top();\n }\n out << \"]\\n\";\n}\n\nbool Solve = [](){\n std::ofstream out(\"user.out\");\n for (std::string s; std::getline(std::cin, s);) {\n reverseListAndPrint(s, out);\n }\n out.flush();\n exit(0);\n return true;\n}();\n\nclass Solution {\npublic:\n ListNode* reverseList(ListNode* head) {\n if(head == NULL)return NULL;\n struct ListNode *dest;\n struct ListNode *src;\n dest = head->next;\n src = head;\n head->next = NULL;\n while(dest!=NULL){\n head = dest;\n dest = head->next;\n head->next = src;\n src = head;\n }\n return head;\n }\n};", "memory": "7800" }
206
<p>Given the <code>head</code> of a singly linked list, reverse the list, and return <em>the reversed list</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg" style="width: 542px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2,3,4,5] <strong>Output:</strong> [5,4,3,2,1] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex2.jpg" style="width: 182px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2] <strong>Output:</strong> [2,1] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is the range <code>[0, 5000]</code>.</li> <li><code>-5000 &lt;= Node.val &lt;= 5000</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> A linked list can be reversed either iteratively or recursively. Could you implement both?</p>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n static const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cout.tie(nullptr);\n std::cin.tie(nullptr);\n return true;\n}();\n\ninline bool isDigit(char ch) {\n return (ch >= '0') && (ch <= '9');\n}\n\nvoid reverseListAndPrint(const std::string& s, std::ofstream& out) {\n if (s.size() == 2) {\n out << \"[]\\n\";\n return;\n }\n std::stack<int> st;\n int accumulator = 0;\n bool is_negative = false;\n for (int i = 1; i < s.size(); ++i) {\n if (s[i] == '-') {\n is_negative = true;\n } else if (isDigit(s[i])) {\n accumulator = accumulator * 10 + (s[i] - '0');\n } else {\n if (is_negative) {\n accumulator = -accumulator;\n }\n st.push(accumulator);\n accumulator = 0;\n is_negative = false;\n }\n }\n out << \"[\";\n while (st.size() > 1) {\n out << st.top() << \",\";\n st.pop();\n }\n if (!st.empty()) {\n out << st.top();\n }\n out << \"]\\n\";\n}\n\nbool Solve = [](){\n std::ofstream out(\"user.out\");\n for (std::string s; std::getline(std::cin, s);) {\n reverseListAndPrint(s, out);\n }\n out.flush();\n exit(0);\n return true;\n}();\n\nclass Solution {\npublic:\n ListNode* reverseList(ListNode* head) {\n if(head == NULL)return NULL;\n struct ListNode *dest;\n struct ListNode *src;\n dest = head->next;\n src = head;\n head->next = NULL;\n while(dest!=NULL){\n head = dest;\n dest = head->next;\n head->next = src;\n src = head;\n }\n return head;\n }\n};", "memory": "7800" }
206
<p>Given the <code>head</code> of a singly linked list, reverse the list, and return <em>the reversed list</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg" style="width: 542px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2,3,4,5] <strong>Output:</strong> [5,4,3,2,1] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex2.jpg" style="width: 182px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2] <strong>Output:</strong> [2,1] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is the range <code>[0, 5000]</code>.</li> <li><code>-5000 &lt;= Node.val &lt;= 5000</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> A linked list can be reversed either iteratively or recursively. Could you implement both?</p>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n static const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cout.tie(nullptr);\n std::cin.tie(nullptr);\n return true;\n}();\n\ninline bool isDigit(char ch) {\n return (ch >= '0') && (ch <= '9');\n}\n\nvoid reverseListAndPrint(const std::string& s, std::ofstream& out) {\n if (s.size() == 2) {\n out << \"[]\\n\";\n return;\n }\n std::stack<int> st;\n int accumulator = 0;\n bool is_negative = false;\n for (int i = 1; i < s.size(); ++i) {\n if (s[i] == '-') {\n is_negative = true;\n } else if (isDigit(s[i])) {\n accumulator = accumulator * 10 + (s[i] - '0');\n } else {\n if (is_negative) {\n accumulator = -accumulator;\n }\n st.push(accumulator);\n accumulator = 0;\n is_negative = false;\n }\n }\n out << \"[\";\n while (st.size() > 1) {\n out << st.top() << \",\";\n st.pop();\n }\n if (!st.empty()) {\n out << st.top();\n }\n out << \"]\\n\";\n}\n\nbool Solve = [](){\n std::ofstream out(\"user.out\");\n for (std::string s; std::getline(std::cin, s);) {\n reverseListAndPrint(s, out);\n }\n out.flush();\n exit(0);\n return true;\n}();\n\nclass Solution {\npublic:\n ListNode* reverseList(ListNode* head) {\n if(head == NULL)return NULL;\n struct ListNode *dest;\n struct ListNode *src;\n dest = head->next;\n src = head;\n head->next = NULL;\n while(dest!=NULL){\n head = dest;\n dest = head->next;\n head->next = src;\n src = head;\n }\n return head;\n }\n};", "memory": "7900" }
206
<p>Given the <code>head</code> of a singly linked list, reverse the list, and return <em>the reversed list</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg" style="width: 542px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2,3,4,5] <strong>Output:</strong> [5,4,3,2,1] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex2.jpg" style="width: 182px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2] <strong>Output:</strong> [2,1] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is the range <code>[0, 5000]</code>.</li> <li><code>-5000 &lt;= Node.val &lt;= 5000</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> A linked list can be reversed either iteratively or recursively. Could you implement both?</p>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n static const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cout.tie(nullptr);\n std::cin.tie(nullptr);\n return true;\n}();\n\ninline bool isDigit(char ch) {\n return (ch >= '0') && (ch <= '9');\n}\n\nvoid reverseListAndPrint(const std::string& s, std::ofstream& out) {\n if (s.size() == 2) {\n out << \"[]\\n\";\n return;\n }\n std::stack<int> st;\n int accumulator = 0;\n bool is_negative = false;\n for (int i = 1; i < s.size(); ++i) {\n if (s[i] == '-') {\n is_negative = true;\n } else if (isDigit(s[i])) {\n accumulator = accumulator * 10 + (s[i] - '0');\n } else {\n if (is_negative) {\n accumulator = -accumulator;\n }\n st.push(accumulator);\n accumulator = 0;\n is_negative = false;\n }\n }\n out << \"[\";\n while (st.size() > 1) {\n out << st.top() << \",\";\n st.pop();\n }\n if (!st.empty()) {\n out << st.top();\n }\n out << \"]\\n\";\n}\n\nbool Solve = [](){\n std::ofstream out(\"user.out\");\n for (std::string s; std::getline(std::cin, s);) {\n reverseListAndPrint(s, out);\n }\n out.flush();\n exit(0);\n return true;\n}();\n\nclass Solution {\npublic:\n ListNode* reverseIt(ListNode* head, ListNode* src, ListNode *dest){\n if(dest==NULL)return head;\n head = dest;\n dest = head->next;\n head->next = src;\n src = head;\n reverseIt(head, src, dest);\n return head;\n }\n ListNode* reverseList(ListNode* head) {\n if(head == NULL)return NULL;\n struct ListNode *dest;\n struct ListNode *src;\n dest = head->next;\n src = head;\n head->next = NULL;\n head = reverseIt(head,src,dest);\n // while(dest!=NULL){\n // head = dest;\n // dest = head->next;\n // head->next = src;\n // src = head;\n // }\n return head;\n }\n};", "memory": "7900" }
206
<p>Given the <code>head</code> of a singly linked list, reverse the list, and return <em>the reversed list</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg" style="width: 542px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2,3,4,5] <strong>Output:</strong> [5,4,3,2,1] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex2.jpg" style="width: 182px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2] <strong>Output:</strong> [2,1] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is the range <code>[0, 5000]</code>.</li> <li><code>-5000 &lt;= Node.val &lt;= 5000</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> A linked list can be reversed either iteratively or recursively. Could you implement both?</p>
0
{ "code": "static const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cout.tie(nullptr);\n std::cin.tie(nullptr);\n return true;\n}();\n\ninline bool isDigit(char ch) {\n return (ch >= '0') && (ch <= '9');\n}\n\nvoid reverseListAndPrint(const std::string& s, std::ofstream& out) {\n if (s.size() == 2) {\n out << \"[]\\n\";\n return;\n }\n std::stack<int> st;\n int accumulator = 0;\n bool is_negative = false;\n for (int i = 1; i < s.size(); ++i) {\n if (s[i] == '-') {\n is_negative = true;\n } else if (isDigit(s[i])) {\n accumulator = accumulator * 10 + (s[i] - '0');\n } else {\n if (is_negative) {\n accumulator = -accumulator;\n }\n st.push(accumulator);\n accumulator = 0;\n is_negative = false;\n }\n }\n out << \"[\";\n while (st.size() > 1) {\n out << st.top() << \",\";\n st.pop();\n }\n if (!st.empty()) {\n out << st.top();\n }\n out << \"]\\n\";\n}\n\nbool Solve = [](){\n std::ofstream out(\"user.out\");\n for (std::string s; std::getline(std::cin, s);) {\n reverseListAndPrint(s, out);\n }\n out.flush();\n exit(0);\n return true;\n}();\n\nclass Solution {\npublic:\n ListNode* reverseList(ListNode* head) {\n ListNode* prev = nullptr;\n ListNode* curr = head;\n while (curr) {\n ListNode* tmp = curr->next;\n curr->next = prev;\n prev = curr;\n curr = tmp;\n }\n return prev;\n }\n};", "memory": "8000" }
206
<p>Given the <code>head</code> of a singly linked list, reverse the list, and return <em>the reversed list</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg" style="width: 542px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2,3,4,5] <strong>Output:</strong> [5,4,3,2,1] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex2.jpg" style="width: 182px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2] <strong>Output:</strong> [2,1] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is the range <code>[0, 5000]</code>.</li> <li><code>-5000 &lt;= Node.val &lt;= 5000</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> A linked list can be reversed either iteratively or recursively. Could you implement both?</p>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* reverseList(ListNode* head) {\n ListNode * prev=NULL;\n ListNode * curr=NULL;\n while(head!=NULL){\n curr=head->next;\n head->next=prev;\n prev=head;\n head=curr;\n\n }\n return prev;\n\n }\n};", "memory": "12600" }
206
<p>Given the <code>head</code> of a singly linked list, reverse the list, and return <em>the reversed list</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg" style="width: 542px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2,3,4,5] <strong>Output:</strong> [5,4,3,2,1] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex2.jpg" style="width: 182px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2] <strong>Output:</strong> [2,1] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is the range <code>[0, 5000]</code>.</li> <li><code>-5000 &lt;= Node.val &lt;= 5000</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> A linked list can be reversed either iteratively or recursively. Could you implement both?</p>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* reverseList(ListNode* head) {\n if (head == nullptr)\n {\n return nullptr;\n }\n\n ListNode* previous = head;\n ListNode* iter = head->next;\n while (iter != nullptr)\n {\n ListNode* temp = iter->next;\n iter->next = previous;\n previous = iter;\n iter = temp;\n }\n head->next = nullptr;\n return previous;\n }\n};", "memory": "12700" }
206
<p>Given the <code>head</code> of a singly linked list, reverse the list, and return <em>the reversed list</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg" style="width: 542px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2,3,4,5] <strong>Output:</strong> [5,4,3,2,1] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex2.jpg" style="width: 182px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2] <strong>Output:</strong> [2,1] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is the range <code>[0, 5000]</code>.</li> <li><code>-5000 &lt;= Node.val &lt;= 5000</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> A linked list can be reversed either iteratively or recursively. Could you implement both?</p>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* reverseList(ListNode* head) {\n if(head == NULL || head->next == NULL) return head;\n\n ListNode* p = head;\n ListNode* c = head->next;\n ListNode* n = NULL;\n p->next = NULL;\n while(c->next != NULL){\n n = c->next;\n c->next = p;\n p = c;\n c = n;\n\n }\n c->next = p;\n return c;\n }\n};", "memory": "12700" }
206
<p>Given the <code>head</code> of a singly linked list, reverse the list, and return <em>the reversed list</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg" style="width: 542px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2,3,4,5] <strong>Output:</strong> [5,4,3,2,1] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex2.jpg" style="width: 182px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2] <strong>Output:</strong> [2,1] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is the range <code>[0, 5000]</code>.</li> <li><code>-5000 &lt;= Node.val &lt;= 5000</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> A linked list can be reversed either iteratively or recursively. Could you implement both?</p>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* reverseList(ListNode* head) {\n ListNode* curr_ = NULL;\n ListNode* prev = head;\n ListNode* ans_head = head;\n if(head!=NULL && head->next!=NULL){\n curr_ = head->next;\n }\n bool flag = false;\n ListNode* temp_ = NULL;\n while(curr_!=NULL){\n if(flag==false){\n temp_ = prev;\n flag = true;\n }\n ListNode* nxt_ = curr_->next;\n if(curr_->next==NULL){\n ans_head = curr_;\n ans_head->next = prev;\n // cout<<\"AMAN\"<<endl;\n // cout<<ans_head->val<<endl;\n break;\n } \n curr_->next = prev;\n prev = curr_;\n // cout<<curr_->val<<\" \"<<curr_->next->val<<endl;\n curr_ = nxt_; \n // cout<<curr_->val<<\" \"<<curr_->next->val<<endl;\n // cout<<curr_->val<<endl; \n\n }\n \n if(temp_!=NULL && temp_->next!=NULL){\n temp_->next = NULL;\n }\n \n return ans_head; \n }\n};", "memory": "12800" }
206
<p>Given the <code>head</code> of a singly linked list, reverse the list, and return <em>the reversed list</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg" style="width: 542px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2,3,4,5] <strong>Output:</strong> [5,4,3,2,1] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex2.jpg" style="width: 182px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2] <strong>Output:</strong> [2,1] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is the range <code>[0, 5000]</code>.</li> <li><code>-5000 &lt;= Node.val &lt;= 5000</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> A linked list can be reversed either iteratively or recursively. Could you implement both?</p>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* reverseList(ListNode* head) {\n if(head == NULL || head -> next == NULL){\n return head;\n }\n\n ListNode* prev = NULL;\n ListNode* curr = head;\n ListNode* forward = NULL;\n\n while(curr != NULL){\n forward = curr -> next;\n curr -> next = prev;\n prev = curr;\n curr = forward;\n\n\n }\n return prev;\n \n }\n};", "memory": "12800" }
206
<p>Given the <code>head</code> of a singly linked list, reverse the list, and return <em>the reversed list</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg" style="width: 542px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2,3,4,5] <strong>Output:</strong> [5,4,3,2,1] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex2.jpg" style="width: 182px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2] <strong>Output:</strong> [2,1] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is the range <code>[0, 5000]</code>.</li> <li><code>-5000 &lt;= Node.val &lt;= 5000</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> A linked list can be reversed either iteratively or recursively. Could you implement both?</p>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nvoid reverse(ListNode* & cur,ListNode* & prev){\n if(cur==NULL) return;\n ListNode* temp=cur->next;\n cur->next=prev;\n prev=cur;\n cur=temp;\n reverse(cur,prev);\n}\n\nclass Solution {\npublic:\n ListNode* reverseList(ListNode* head) {\n ListNode* prev=NULL;\n reverse(head,prev);\n return prev;\n }\n};", "memory": "12900" }
206
<p>Given the <code>head</code> of a singly linked list, reverse the list, and return <em>the reversed list</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg" style="width: 542px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2,3,4,5] <strong>Output:</strong> [5,4,3,2,1] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex2.jpg" style="width: 182px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2] <strong>Output:</strong> [2,1] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is the range <code>[0, 5000]</code>.</li> <li><code>-5000 &lt;= Node.val &lt;= 5000</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> A linked list can be reversed either iteratively or recursively. Could you implement both?</p>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* reverseList(ListNode* head) {\n ListNode *prev = NULL;\n ListNode *curr = head;\n ListNode *next = NULL;\n\n while(curr!=NULL){\n next = curr -> next;\n curr -> next = prev;\n prev=curr;\n curr=next;\n }\n return prev;\n }\n};", "memory": "12900" }
206
<p>Given the <code>head</code> of a singly linked list, reverse the list, and return <em>the reversed list</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg" style="width: 542px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2,3,4,5] <strong>Output:</strong> [5,4,3,2,1] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex2.jpg" style="width: 182px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2] <strong>Output:</strong> [2,1] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is the range <code>[0, 5000]</code>.</li> <li><code>-5000 &lt;= Node.val &lt;= 5000</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> A linked list can be reversed either iteratively or recursively. Could you implement both?</p>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* reverseList(ListNode* head) {\n ListNode* prev=NULL;\n ListNode* curr=head;\n while(curr!=NULL){\n ListNode* next=curr->next;\n curr->next=prev;\n prev=curr;\n curr=next;\n }\n\n ListNode* newnode=prev;\n return newnode;\n }\n \n};", "memory": "13000" }