id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
1,371 | <p>Given a string <font face="monospace">s</font> of <code>'('</code> , <code>')'</code> and lowercase English characters.</p>
<p>Your task is to remove the minimum number of parentheses ( <code>'('</code> or <code>')'</code>, in any positions ) so that the resulting <em>parentheses str... | 3 | {
"code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n stack<pair<char, int>> st;\n int i = 0;\n set<int> rem;\n for(auto x: s){\n if(x=='('){\n st.push({x, i});\n }else if(x==')'){\n if(st.empty()) {\n ... |
1,371 | <p>Given a string <font face="monospace">s</font> of <code>'('</code> , <code>')'</code> and lowercase English characters.</p>
<p>Your task is to remove the minimum number of parentheses ( <code>'('</code> or <code>')'</code>, in any positions ) so that the resulting <em>parentheses str... | 3 | {
"code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n std::stack<pair<char, int>> parens;\n std::set<int> removeIdx;\n for (int i = 0; i < s.size(); i++) {\n char c = s[i];\n if (c == '(') {\n parens.push(std::make_pair(c, i));\n ... |
1,371 | <p>Given a string <font face="monospace">s</font> of <code>'('</code> , <code>')'</code> and lowercase English characters.</p>
<p>Your task is to remove the minimum number of parentheses ( <code>'('</code> or <code>')'</code>, in any positions ) so that the resulting <em>parentheses str... | 3 | {
"code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n string ans=\"\";\n int n=s.size();\n stack<int> st;\n for(int i=0;i<n;i++) {\n if(s[i]>='a' && s[i]<='z') ans+=s[i];\n else if(s[i]=='(') {\n ans+=s[i];\n ... |
1,371 | <p>Given a string <font face="monospace">s</font> of <code>'('</code> , <code>')'</code> and lowercase English characters.</p>
<p>Your task is to remove the minimum number of parentheses ( <code>'('</code> or <code>')'</code>, in any positions ) so that the resulting <em>parentheses str... | 3 | {
"code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n using p = pair<int, char>;\n int n = s.size();\n stack<p> stk;\n for (int i = 0; i < n; i++) {\n if (s[i] == ')' && !stk.empty() && stk.top().second == '(') {\n stk.pop();\n ... |
1,371 | <p>Given a string <font face="monospace">s</font> of <code>'('</code> , <code>')'</code> and lowercase English characters.</p>
<p>Your task is to remove the minimum number of parentheses ( <code>'('</code> or <code>')'</code>, in any positions ) so that the resulting <em>parentheses str... | 3 | {
"code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n stack<pair<char,int>> st;\n for(int i=0;i<s.size();i++)\n {\n char top='$';\n if(!st.empty())\n top = st.top().first;\n if(s[i]==')' && top=='(')\n {\n ... |
1,371 | <p>Given a string <font face="monospace">s</font> of <code>'('</code> , <code>')'</code> and lowercase English characters.</p>
<p>Your task is to remove the minimum number of parentheses ( <code>'('</code> or <code>')'</code>, in any positions ) so that the resulting <em>parentheses str... | 3 | {
"code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n stack<pair<int, char>> st;\n for(int i=0;i<s.size();i++) {\n if (s[i] == '(') {\n st.push({i, s[i]});\n } else if (s[i] == ')') {\n if (st.size() == 0) { \n ... |
1,371 | <p>Given a string <font face="monospace">s</font> of <code>'('</code> , <code>')'</code> and lowercase English characters.</p>
<p>Your task is to remove the minimum number of parentheses ( <code>'('</code> or <code>')'</code>, in any positions ) so that the resulting <em>parentheses str... | 3 | {
"code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n stack<pair<int, char>> st;\n for(int i=0;i<s.size();i++) {\n if (s[i] == '(') {\n st.push({i, s[i]});\n } else if (s[i] == ')') {\n if (st.size() == 0) { \n ... |
1,371 | <p>Given a string <font face="monospace">s</font> of <code>'('</code> , <code>')'</code> and lowercase English characters.</p>
<p>Your task is to remove the minimum number of parentheses ( <code>'('</code> or <code>')'</code>, in any positions ) so that the resulting <em>parentheses str... | 3 | {
"code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n\n stack<pair<char, int>> st; \n int n = s.size(); \n string finalString = \"\"; \n\n for(int i = 0; i < n; i++) {\n if(s[i] == '(') {\n // Push inside stack \n st.pu... |
1,371 | <p>Given a string <font face="monospace">s</font> of <code>'('</code> , <code>')'</code> and lowercase English characters.</p>
<p>Your task is to remove the minimum number of parentheses ( <code>'('</code> or <code>')'</code>, in any positions ) so that the resulting <em>parentheses str... | 3 | {
"code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n \n stack<pair<char,int>>st;\n \n string ans=\"\";\n \n set<int>Intset;\n \n for(int i=0;i<s.size();i++){\n \n if(s[i]==')' || s[i]=='('){\n ... |
1,371 | <p>Given a string <font face="monospace">s</font> of <code>'('</code> , <code>')'</code> and lowercase English characters.</p>
<p>Your task is to remove the minimum number of parentheses ( <code>'('</code> or <code>')'</code>, in any positions ) so that the resulting <em>parentheses str... | 3 | {
"code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n stack<pair<char, int>> st;\n for (int i = 0; i < s.length(); i++) {\n if (s[i] == '(') {\n st.push({'(', i});\n }\n if (s[i] == ')') {\n if (!st.empty() && s... |
1,371 | <p>Given a string <font face="monospace">s</font> of <code>'('</code> , <code>')'</code> and lowercase English characters.</p>
<p>Your task is to remove the minimum number of parentheses ( <code>'('</code> or <code>')'</code>, in any positions ) so that the resulting <em>parentheses str... | 3 | {
"code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n vector<int> pl;\n vector<int> pr;\n\n for(int i=0;i<s.length();i++){\n if(s[i]=='('){\n pl.push_back(i);\n ... |
1,371 | <p>Given a string <font face="monospace">s</font> of <code>'('</code> , <code>')'</code> and lowercase English characters.</p>
<p>Your task is to remove the minimum number of parentheses ( <code>'('</code> or <code>')'</code>, in any positions ) so that the resulting <em>parentheses str... | 3 | {
"code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n stringstream result;\n stack<pair<char, int>> st;\n unordered_set<int> indexes_to_rem_store;\n for (auto i=0; i<s.size(); i++) {\n if (s[i] == '(') {\n st.push({s[i], i});\n ... |
1,371 | <p>Given a string <font face="monospace">s</font> of <code>'('</code> , <code>')'</code> and lowercase English characters.</p>
<p>Your task is to remove the minimum number of parentheses ( <code>'('</code> or <code>')'</code>, in any positions ) so that the resulting <em>parentheses str... | 3 | {
"code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n if(s.empty()) return s;\n string res = \"\";\n stack<pair<char, int>> stk;\n for(int i = 0; i < s.length(); i++){\n if(s[i] != ')' && s[i] != '('){\n res += s[i];\n }\n ... |
1,371 | <p>Given a string <font face="monospace">s</font> of <code>'('</code> , <code>')'</code> and lowercase English characters.</p>
<p>Your task is to remove the minimum number of parentheses ( <code>'('</code> or <code>')'</code>, in any positions ) so that the resulting <em>parentheses str... | 3 | {
"code": "class Solution {\n public:\n\n string minRemoveToMakeValid(string s) {\n\n std::vector<bool> to_remove(s.size(), false);\n std::vector<int> index(s.size(), -1);\n\n\n std:stack<int> paren;\n\n for (size_t i=0;i< s.size();i++ ) {\n const char c = s[i];\n ... |
1,371 | <p>Given a string <font face="monospace">s</font> of <code>'('</code> , <code>')'</code> and lowercase English characters.</p>
<p>Your task is to remove the minimum number of parentheses ( <code>'('</code> or <code>')'</code>, in any positions ) so that the resulting <em>parentheses str... | 3 | {
"code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n stack<pair<char, size_t>> br_stack;\n for(int i=0; i<s.size(); i++) {\n if( s[i] == '(') {\n br_stack.push(make_pair(s[i], i));\n }\n else if(s[i] == ')') {\n ... |
1,371 | <p>Given a string <font face="monospace">s</font> of <code>'('</code> , <code>')'</code> and lowercase English characters.</p>
<p>Your task is to remove the minimum number of parentheses ( <code>'('</code> or <code>')'</code>, in any positions ) so that the resulting <em>parentheses str... | 3 | {
"code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n \n int n=s.size();\n string ref=\"\";\n for(int i=0;i<n;i++)\n {\n if(s[i]==')'||s[i]=='(')\n {\n ref+=s[i];\n }\n }\n\n stack<char> st;\... |
1,371 | <p>Given a string <font face="monospace">s</font> of <code>'('</code> , <code>')'</code> and lowercase English characters.</p>
<p>Your task is to remove the minimum number of parentheses ( <code>'('</code> or <code>')'</code>, in any positions ) so that the resulting <em>parentheses str... | 3 | {
"code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n int n=s.length();\n stack<pair<char,int>>st;\n vector<char>temp;\n for(int i=0;i<n;i++){\n temp.push_back(s[i]);\n }\n for(int i=0;i<n;i++){\n if(s[i]==')'){\n if(!st.empty() && st.to... |
1,371 | <p>Given a string <font face="monospace">s</font> of <code>'('</code> , <code>')'</code> and lowercase English characters.</p>
<p>Your task is to remove the minimum number of parentheses ( <code>'('</code> or <code>')'</code>, in any positions ) so that the resulting <em>parentheses str... | 3 | {
"code": "struct cell {\n char s;\n int pos;\n};\n\nclass Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n stack<cell> st;\n for (int i = 0; i < s.size(); i++) {\n char c = s[i];\n if (c == ')') {\n if (!st.empty() && st.top().s == '(') {\n ... |
1,371 | <p>Given a string <font face="monospace">s</font> of <code>'('</code> , <code>')'</code> and lowercase English characters.</p>
<p>Your task is to remove the minimum number of parentheses ( <code>'('</code> or <code>')'</code>, in any positions ) so that the resulting <em>parentheses str... | 3 | {
"code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n vector<int> validChar(s.length(),0);\n stack<pair<char,int>> stk;\n\n for(int i=0; i<s.length(); i++) {\n if(s[i] == '(') {\n stk.push({s[i],i});\n }\n else if(s[i] ... |
1,371 | <p>Given a string <font face="monospace">s</font> of <code>'('</code> , <code>')'</code> and lowercase English characters.</p>
<p>Your task is to remove the minimum number of parentheses ( <code>'('</code> or <code>')'</code>, in any positions ) so that the resulting <em>parentheses str... | 3 | {
"code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n stack<pair<char,int>> stk;\n vector<int> valid(s.length(),0);\n\n for(int i=0; i<s.length(); i++) {\n if(s[i] == '(') {\n stk.push({s[i],i});\n }\n else if(s[i] == '... |
1,371 | <p>Given a string <font face="monospace">s</font> of <code>'('</code> , <code>')'</code> and lowercase English characters.</p>
<p>Your task is to remove the minimum number of parentheses ( <code>'('</code> or <code>')'</code>, in any positions ) so that the resulting <em>parentheses str... | 3 | {
"code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) \n {\n vector<int> openIndeces;\n vector<int> closeIndeces;\n\n int open = 0;\n int close = 0;\n\n for(int i=0;i<s.length();i++)\n {\n if(s[i] == '(')\n {\n ... |
1,371 | <p>Given a string <font face="monospace">s</font> of <code>'('</code> , <code>')'</code> and lowercase English characters.</p>
<p>Your task is to remove the minimum number of parentheses ( <code>'('</code> or <code>')'</code>, in any positions ) so that the resulting <em>parentheses str... | 3 | {
"code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n stack<pair<char,int>>s1;\n vector<int>visit(s.size(),1);\n for(int i=0;i<s.size();i++){\n if(s[i]==')'){\n while(s1.size() && s1.top().first!='('){\n s1.pop();\n ... |
1,371 | <p>Given a string <font face="monospace">s</font> of <code>'('</code> , <code>')'</code> and lowercase English characters.</p>
<p>Your task is to remove the minimum number of parentheses ( <code>'('</code> or <code>')'</code>, in any positions ) so that the resulting <em>parentheses str... | 3 | {
"code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n stack<pair<char,int>> stk;\n vector<int> vec(s.size(),0);\n for(int i=0;i<s.size();i++){\n if(!stk.empty() && stk.top().first == '(' && s[i] == ')'){\n int num = stk.top().second; \n ... |
1,371 | <p>Given a string <font face="monospace">s</font> of <code>'('</code> , <code>')'</code> and lowercase English characters.</p>
<p>Your task is to remove the minimum number of parentheses ( <code>'('</code> or <code>')'</code>, in any positions ) so that the resulting <em>parentheses str... | 3 | {
"code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) \n {\n vector<int> openIndeces;\n vector<int> closeIndeces;\n\n int open = 0;\n int close = 0;\n\n for(int i=0;i<s.length();i++)\n {\n if(s[i] == '(')\n {\n ... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 0 | {
"code": "class Solution {\npublic:\n bool helper(vector<vector<int>>& grid, int x, int y) {\n int r = grid.size();\n int c = grid[0].size();\n\n if (x < 0 || y < 0 || x >= r || y >= c) {\n return false;\n }\n\n if (grid[x][y] == 1) {\n return true;\n ... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 0 | {
"code": "class Solution {\npublic:\n bool dfs(vector<vector<int>>&grid, int i, int j){\n int n = grid.size();\n int m = grid[0].size();\n\n if(i<0 || j <0 || i>=n || j>=m){\n return false;\n }\n\n if(grid[i][j] == 1){\n return true;\n }\n gri... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 0 | {
"code": "class Solution {\npublic:\n bool vis[101][101];\n bool dfs(vector<vector<int>>& grid,int i,int j){\n int n = grid.size();\n int m = grid[0].size();\n if(i < 0 || i >= n || j < 0 || j >= m){\n return false;\n }else if(grid[i][j] == 1){\n return true;\n... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 1 | {
"code": "class Solution {\npublic:\n vector<pair<int,int>> dir = {{0,1}, {1,0}, {0,-1}, {-1,0}};\n int n,m;\n void dfs(int x, int y, vector<vector<int>>& grid, vector<vector<int>>&vis){\n vis[x][y] = 1;\n for(auto [dx,dy] : dir){\n int nx = x + dx;\n int ny = y + dy;\n ... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 1 | {
"code": "class Solution {\npublic:\n vector<int>rows{-1,1,0,0};\n vector<int>cols{0,0,-1,1};\n bool isSafe(int r,int c,int m,int n){\n if(r<0 || r>=m || c<0 || c>=n){\n return false;\n }\n return true;\n }\n void dfs(vector<vector<int>>& grid,int r,int c,int m,int n,ve... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 2 | {
"code": "class Solution {\npublic:\nint n,m;\nbool vis[105][105];\nvector<pair<int,int>> d={{0,1},{0,-1},{-1,0},{1,0}};\n bool valid(int ci,int cj)\n {\n if(ci>=0 && ci<n && cj>=0 && cj<m) return true;\n else return false;\n }\n bool flag;\n void dfs(int si,int sj,vector<vector<int>>& g... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 2 | {
"code": "class Solution {\npublic:\n int n, m;\n int ans = 0;\n bool flag;\n bool vis[105][105];\n vector<pair<int, int>> d = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};\n bool valid(int ci, int cj) {\n if (ci >= 0 && ci < n && cj >= 0 && cj < m)\n return true;\n else\n ... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 2 | {
"code": "class Solution {\npublic:\n int n, m;\n bool vis[105][105];\n vector<pair<int, int>> d = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};\n bool valid(int ci, int cj) {\n if (ci >= 0 && ci < n && cj >= 0 && cj < m) {\n return true;\n } else {\n return false;\n }\n ... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 2 | {
"code": "class Solution {\npublic:\n int n, m;\n bool vis[101][101];\n vector<pair<int, int>> d = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};\n\n bool valid(int ci, int cj) {\n if (ci >= 0 && ci < n && cj >= 0 && cj < m)\n return true;\n else\n return false;\n }\n\n boo... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 2 | {
"code": "class Solution {\npublic:\n int arr[100][100];\n bool vis[100][100];\n int n,m;\n bool ans;\n bool valid(int i,int j){\n if(i<0 || i>=n || j<0 || j>=m)return false;\n else return true;\n }\n vector<pair<int,int>>d={{0,1},{0,-1},{1,0},{-1,0}};\n void count(int s1,int s2... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 2 | {
"code": "class Solution {\npublic:\n int closedIsland(vector<vector<int>>& grid) {\n int m = grid.size(), n = grid[0].size();\n int ans = 0;\n int dirs[5] = {-1, 0, 1, 0, -1};\n function<int(int, int)> dfs = [&](int i, int j) -> int {\n int res = i > 0 && i < m - 1 && j > 0... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 2 | {
"code": "class Solution {\npublic:\n int n, m;\n bool mark[110][110];\n vector<vector<int>> g;\n \n\n bool free(int x, int y){\n return x>=0 && x<n && y>=0 && y<m && !mark[x][y] && g[x][y]==0;\n }\n\n void dfs(int x, int y){\n mark[x][y] = true;\n if(free(x+1,y)) dfs(x+1,y);\n... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 2 | {
"code": " \n\nclass Solution {\npublic:\nint n, m;\nint a[505][505];\nbool vis[505][505];\n bool ans= false;\n vector<pair<int, int>> v = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};\n\nbool isValid(int i, int j)\n{\n if (i >= 0 && i < n && j >= 0 && j < m )\n {\n return true;\n }\n\n else\n {\n ... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 2 | {
"code": "class Solution {\npublic:\n int n, m;\n bool mark[110][110];\n vector<vector<int>> g;\n \n\n bool free(int x, int y){\n return x>=0 && x<n && y>=0 && y<m && !mark[x][y] && g[x][y]==0;\n }\n\n void dfs(int x, int y){\n mark[x][y] = true;\n if(free(x+1,y)) dfs(x+1,y);\n... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 2 | {
"code": "class Solution {\n public:\n int closedIsland(vector<vector<int>>&grid) {\n const int row = grid.size();\n const int col = grid[0].size();\n int res = 0;\n for (int i = 0; i < row; ++i) {\n for (int j = 0; j < col; ++j) {\n if (grid[i][j] == 0 && checkClosed(grid, i,... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 2 | {
"code": "class DisjointSet{\n public:\n vector<int>size,parent;\n DisjointSet(int n){\n size.resize(n+1,1);\n parent.resize(n+1);\n for(int i=0;i<=n;i++)parent[i]=i;\n }\n int findUpar(int node){\n if(node==parent[node])return node;\n return parent[node]=findUpar(pa... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 2 | {
"code": "class DisjointSet{\n public:\n vector<int>size,parent;\n DisjointSet(int n){\n size.resize(n+1,1);\n parent.resize(n+1);\n for(int i=0;i<=n;i++)parent[i]=i;\n }\n int findUpar(int node){\n if(node==parent[node])return node;\n return parent[node]=findUpar(pa... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 2 | {
"code": "class DisjointSet{\n public:\n vector<int>size,parent;\n DisjointSet(int n){\n size.resize(n+1,1);\n parent.resize(n+1);\n for(int i=0;i<=n;i++)parent[i]=i;\n }\n int findUpar(int node){\n if(node==parent[node])return node;\n return parent[node]=findUpar(pa... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 2 | {
"code": "class Solution {\npublic:\n int closedIsland(vector<vector<int>>& grid) {\n int count = 0;\n int m = grid.size(), n = grid[0].size();\n\n std::function<void(int, int, bool&)> is_island = [&](int i, int j, bool& legit) {\n if (i < 0 || i >= m || j < 0 || j >= n) {\n legit =... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 2 | {
"code": "class Solution {\npublic:\n void dfs(int i , int j ,vector<vector<int>>& grid){\n int row = grid.size() ; \n int col = grid[0].size();\n grid[i][j] = 1 ;\n\n vector<pair<int,int>>dirs = {{1 , 0} , {0 , 1} , {-1 , 0} , {0,-1}};\n\n for(pair<int,int>p : dirs){\n ... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 2 | {
"code": "class Solution {\npublic:\n void dfs(int row, int col, vector<vector<int>>& grid){\n int m = grid.size();\n int n = grid[0].size();\n\n grid[row][col] = 1;\n\n vector<pair<int,int>> direction = {{1,0},{-1,0},{0,-1},{0,1}};\n\n for(auto it : direction){\n int... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 2 | {
"code": "class Solution {\npublic:\n int dirx[4] = {0, 0, -1, 1};\n int diry[4] = {1, -1, 0, 0};\n int closedIsland(vector<vector<int>>& grid) {\n\n int n = grid.size(), m = grid[0].size();\n int count = 0;\n vector<vector<int>> vis(n, vector<int>(m, 0));\n\n for (int i = 0; i <... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 2 | {
"code": "#include<bits/stdc++.h>\nusing namespace std;\n\nclass disjoint {\n int* parent;\n int* size;\npublic:\n disjoint(int n) {\n parent = new int[n];\n size = new int[n];\n for (int i = 0; i < n; i++) {\n parent[i] = i;\n size[i] = 1;\n }\n }\n\n ... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 2 | {
"code": "class Solution {\npublic:\nvector<int> p;\n vector<int> r;\n\n int findp(int i, vector<int>& p) {\n if (i == p[i])\n return i;\n return p[i] = findp(p[i], p);\n }\n\n void uni(int i, int j, vector<int>& p, vector<int>& r) {\n int ip = findp(i, p);\n int jp... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 2 | {
"code": "class Solution {\npublic:\nvector<int> p;\n vector<int> r;\n\n int findp(int i, vector<int>& p) {\n if (i == p[i])\n return i;\n return p[i] = findp(p[i], p);\n }\n\n void uni(int i, int j, vector<int>& p, vector<int>& r) {\n int ip = findp(i, p);\n int jp... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 2 | {
"code": "class Solution {\npublic:\n void bfs(vector<vector<int>>&grid, queue<pair<int, int>>&q){\n int n = grid.size();\n int m = grid[0].size();\n while(!q.empty()){\n pair<int, int> top = q.front();\n int x = top.first;\n int y = top.second;\n g... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 2 | {
"code": "#include <bits/stdc++.h>\nusing namespace std;\nclass DisjointSet {\n vector<int> rank, parent, size; \npublic: \n DisjointSet(int n) {\n rank.resize(n+1, 0); \n parent.resize(n+1);\n size.resize(n+1); \n for(int i = 0;i<=n;i++) {\n parent[i] = i; \n ... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 2 | {
"code": "class Solution {\npublic:\n bool landsearch(int i, int j, int m, int n, vector<vector<int>>& grid){\n if(i<=0 || j<=0 || i>=m-1 || j>=n-1){\n return false;\n }\n grid[i][j]=1;\n bool isenclosed=true;\n vector<int> dx={1,-1,0,0};\n vector<int> dy={0,0,... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 2 | {
"code": "class Solution {\npublic:\n int closedIsland(vector<vector<int>>& grid) {\n init(grid);\n return traverseGrid(grid);\n }\n\nprivate:\n const int LAND = 0, OCEAN = 1;\n int row, col;\n \n\n void init(vector<vector<int>>& grid) {\n row = grid.size();\n col = grid... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 2 | {
"code": "class Solution {\npublic:\n int closedIsland(vector<vector<int>>& grid) {\n init(grid);\n return traverseGrid(grid);\n }\n\nprivate:\n const int LAND = 0, OCEAN = 1;\n int row, col;\n \n\n void init(vector<vector<int>>& grid) {\n row = grid.size();\n col = grid... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 2 | {
"code": "class Solution {\n private:\n void dfs(int row,int col,vector<vector<int>>&vis,vector<vector<int>>&grid){\n vis[row][col]=1;\n vector<pair<int,int>>dir={{1,0},{0,1},{-1,0},{0,-1}};\n for(int i=0;i<4;++i){\n int nrow = row+dir[i].first;\n int ncol = col+dir[i... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 2 | {
"code": "class Solution {\npublic:\nbool dfs(int i,int j,vector<vector<int>>& grid,vector<vector<int>>&vis){\n if(i==0 || j==0 || i==grid.size()-1 || j==grid[0].size()-1){\n return false;\n }\n vis[i][j]=1;\n bool isClosed = true;\n\n vector<int>dr={0,-1,0,1};\n vector<int>dc={-1,0,1,0};\n ... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 2 | {
"code": "class Solution {\npublic:\n int closedIsland(vector<vector<int>>& grid) \n {\n int m = grid.size(); // rows\n int n = grid[0].size(); // columns\n int count = 0;\n vector<vector<bool>> visit(m, vector<bool>(n, false)); // Correct dimension and initialization\n\n ... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 2 | {
"code": "class Solution {\n void dfs(int row,int col, vector<vector<int>>&visited,vector<vector<int>>& grid){\n int n = grid.size();\n int m = grid[0].size();\n visited[row][col] = 1;\n vector<pair<int,int>>directions= {{-1,0},{0,1},{1,0},{0,-1}};\n for(auto &dir:directions){\n... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 2 | {
"code": "class Solution {\npublic:\n void dfs(vector<vector<int>>& grid, int n, int m, int i, int j, vector<vector<bool>>& vis)\n {\n if (i < 0 || i >= n || j < 0 || j >= m || grid[i][j] == 1 || vis[i][j])\n {\n return;\n }\n vis[i][j] = true;\n vector<int> d = {-... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 2 | {
"code": "class Solution {\npublic:\n void dfs(vector<vector<int>>& grid, int n, int m, int i, int j, vector<vector<bool>>& vis)\n {\n if (i < 0 || i >= n || j < 0 || j >= m || grid[i][j] == 1 || vis[i][j])\n {\n return;\n }\n vis[i][j] = true;\n vector<int> d = {-... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "class Solution {\npublic:\n // directions 4 connected\n const vector<pair<int, int>> dirVec = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\n\n void bfs(queue<pair<int,int>>&q, vector<vector<int>>& grid) {\n while (q.size()) {\n auto curr = q.front();\n q.pop();\n\n f... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "class Solution {\npublic:\n bool isValid(int x, int y, int n, int m){\n return x>=0 && x<n && y>=0 && y<m;\n }\n void dfs(int x, int y, vector<vector<int>>& grid){\n int n = grid.size();\n int m = grid[0].size();\n grid[x][y]=1;\n vector<int> dx = {-1, 0, 1, 0};\... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "class Solution {\npublic:\n int closedIsland(vector<vector<int>>& grid) \n {\n if(grid.size() == 0)\n return 0;\n \n int num_closed_islands = 0;\n int num_rows = grid.size(), num_columns = grid[0].size(), r1, c1, r, c, ele;\n \n for(r = 0; r < num_rows... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "class Solution {\npublic:\n int closedIsland(vector<vector<int>>& grid) \n {\n if(grid.size() == 0)\n return 0;\n \n int num_closed_islands = 0;\n int num_rows = grid.size(), num_columns = grid[0].size(), r1, c1, r, c, ele;\n \n for(r = 0; r < num_rows... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "class Solution {\n vector<pair<int, int>> dir = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};\npublic:\n int closedIsland(vector<vector<int>>& grid) {\n int n = grid.size(), m = grid[0].size();\n\n int cnt = 0;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n ... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "class Solution {\npublic:\n void DFS(int i, int j, vector<vector<int>>& grid,\n vector<vector<int>>& visited, vector<int> row, vector<int> col,bool &flag) {\n if(i==0||j==0||i==grid.size()-1||j==grid[0].size()-1)\n flag=true;\n visited[i][j] = 1;\n\n for (int k = ... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "class Solution {\npublic:\n\n void dfs(int r,int c,vector<vector<int>>&grid,vector<vector<int>>&vis)\n {\n int n=grid.size();\n int m=grid[0].size();\n\n vector<int>row={0,0,-1,1};\n vector<int>col={1,-1,0,0};\n\n vis[r][c]=1;\n\n for(int i=0;i<4;i++)\n ... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "class Solution {\npublic:\n void dfs(int row,int col,vector<vector<int>>& grid,vector<vector<int>> &vis){\n vis[row][col]=1;\n vector<int> drow={-1,0,1,0};\n vector<int> dcol={0,1,0,-1};\n for(int i=0;i<4;++i){\n int n_r=row+drow[i];\n int n_c=col+dcol[i... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "class Solution {\npublic:\n int closedIsland(vector<vector<int>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n int count = 0;\n int si[] = {0,-1,0,1}; \n int sj[] = {-1,0,1,0}; \n vector<vector<bool>> vis(n,vector(m,false));\n\n for(int i ... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "class Solution {\npublic:\n void dfsUtil(vector<vector<int>>& grid,vector<vector<int>>&vis,vector<int>delRow,vector<int>delCol,int row,int col,int n,int m)\n {\n vis[row][col] = 1;\n\n for(int i = 0;i<4;i++)\n {\n int nrow = row + delRow[i];\n int ncol = col... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "class Solution {\npublic:\n void dfs(vector<vector<int>>& grid,vector<vector<int>>&vis,vector<int>delRow,vector<int>delCol,int row,int col,int n,int m)\n {\n vis[row][col] = 1;\n\n for(int i = 0;i<4;i++)\n {\n int nrow = row + delRow[i];\n int ncol = col + d... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>>dir= {{1,0},{-1,0},{0,1},{0,-1}};\n bool dfs(int i, int j,vector<vector<int>>& grid, int n , int m)\n {\n if(i<0 || i>=n || j<0 || j>=m ) return false;\n if(grid[i][j]!=0) return true;\n grid[i][j]= 2;\n bool flag=true;\n... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "class Solution {\npublic:\n vector<pair<int,int>> dir = {{0,1}, {1,0}, {0,-1}, {-1,0}};\n int n,m;\n void dfs(int x, int y, vector<vector<int>>& grid, vector<vector<int>>&vis){\n vis[x][y] = 1;\n for(auto [dx,dy] : dir){\n int nx = x + dx;\n int ny = y + dy;\n ... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "class Solution {\npublic:\n void dfs(vector<vector<int>>& grid, int m, int n, int i, int j, bool& isBoundary) {\n grid[i][j] = -1;\n if (!isBoundary && i == 0 || j == 0 || i == m - 1 || j == n - 1)\n isBoundary = true;\n if (i + 1 < m && !grid[i+1][j])\n dfs(gr... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "class Solution {\npublic:\n void bfs(vector<vector<int>>& grid,int i,int j,vector<vector<int>>&vis,int n,int m) {\n queue<pair<int,int>>q;\n vis[i][j]=1;\n int x[]={-1,0,1,0};int y[]={0,1,0,-1};\n q.push({i,j});\n while(!q.empty()) {\n int r=q.front().first,... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "class Solution {\npublic:\n\nvoid bfs(vector<vector<int>>& grid,vector<vector<int>>&visited,int x,int y)\n{\n int n=grid.size();\n int m=grid[0].size();\n queue<pair<int,int>>q;\n q.push({x,y});\n visited[x][y]=1;\n while(!q.empty())\n {\n int cr=q.front().first;\n int cc... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "\nclass Solution {\npublic:\n int n;\n int m;\n \n bool bfs(int r, int c, vector<vector<int>>& grid) {\n // Directions for moving up, down, left, right\n vector<pair<int, int>> directions = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};\n queue<pair<int, int>> q;\n q.push({r, c... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "class Solution {\npublic:\n\n bool count(vector<vector<int>>& grid,int i,int j,vector<vector<bool>>& vis){\n queue<pair<int,int>> q;\n int n=grid.size();\n int m=grid[0].size();\n\n q.push(make_pair(i,j));\n vis[i][j]=true;\n\n bool ans=true;\n\n while(!q... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "using namespace std;\n\nclass Solution {\npublic:\n int rows,columns;\n\n/* bool dfs(vector<vector<int>>& grid, int r, int c){\n \n if(r < 0 || r >= rows || c < 0 || c >= columns){\n return false;\n }\n\n if(grid[r][c] == 1){\n return true;\n }... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "using namespace std;\n\nclass Solution {\npublic:\n int rows,columns;\n\n/* bool dfs(vector<vector<int>>& grid, int r, int c){\n \n if(r < 0 || r >= rows || c < 0 || c >= columns){\n return false;\n }\n\n if(grid[r][c] == 1){\n return true;\n }... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "using namespace std;\n\nclass Solution {\npublic:\n int rows,columns;\n\n/* bool dfs(vector<vector<int>>& grid, int r, int c){\n \n if(r < 0 || r >= rows || c < 0 || c >= columns){\n return false;\n }\n\n if(grid[r][c] == 1){\n return true;\n }... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "class Solution {\npublic:\n int closedIsland(vector<vector<int>>& grid) {\n int dx[4]={1,0,-1,0};\n int dy[4]={0,1,0,-1};\n vector<vector<int>> visit(grid.size(), vector<int>(grid[0].size(),0));\n int n = grid.size();int m= grid[0].size();\n if(n==1 || m==1)return 0;\n... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "class Solution {\n void bfs(vector<vector<int>> &vis,vector<vector<int>> &grid,int I,int J){\n int m=grid.size();\n int n=grid[0].size();\n int di[]={-1,0,1,0};\n int dj[]={0,1,0,-1};\n queue<pair<int,int>> q;\n q.push({I,J});\n while (!q.empty()){\n ... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "class Solution {\npublic:\n bool bfs(int x, int y, int numRows, int numCols, vector<vector<int>>& grid, vector<vector<bool>> &visited) {\n queue<pair<int, int>> bfsQueue;\n bfsQueue.push({x, y});\n visited[x][y] = true;\n bool isClosed = true;\n vector<pair<int, int>> ... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "class Solution {\npublic:\n int n, m;\n vector<pair<int, int>> d = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};\n bool flag;\n bool vis[105][105];\n bool valid(int si, int sj) {\n if (si >= 0 && si < n && sj >= 0 && sj < m)\n return true;\n return false;\n };\n void bfs... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "class Solution {\npublic:\n void bfs(int row,int col,vector<vector<int>>& visited,vector<vector<int>>& grid,int& flag){\n vector<int> dx={1,0,-1,0};\n vector<int> dy={0,1,0,-1};\n int n=grid.size();\n int m=grid[0].size();\n queue<pair<int,int>> q;\n q.push({row... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "class Solution {\npublic:\n int n, m;\n vector<pair<int, int>> d = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};\n bool valid(int i, int j) {\n if (i >= 0 && i < n && j >= 0 && j < m)\n return true;\n else\n return false;\n }\n\n bool vis[105][105];\n int lavel[1... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> dir{{0,-1},{0,1},{-1,0},{1,0}};\n int closedIsland(vector<vector<int>>& grid) {\n vector<vector<bool>> visited(grid.size(), vector<bool>(grid[0].size(),false));\n int count{0};\n for(int i = 0; i < grid.size(); i++)\n {\n ... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "class Solution {\npublic:\n int cnt=0;\n bool vis[1005][1005]={false};\n int flag=0;\n\n bool valid(int i, int j,int row, int col){\n if (i >= 0 && i<row && j>=0 && j<col)return true;\n else return false;\n }\n\n void bfs(vector<vector<int>>& grid,int si, int sj){\n int row =... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "class Solution {\npublic:\n void bfs(int i,int j,vector<vector<int>>& vis,vector<vector<int>>& grid){\n queue<pair<int,int>>q;\n int n=grid.size(),m=grid[0].size();\n vis[i][j]=1;\n q.push({i,j});\n int delrow[]={-1,0,+1,0};\n int delcol[]={0,+1,0,-1};\n ... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "class Solution {\npublic:\n int cnt=0;\n bool vis[1005][1005]={false};\n int flag=0;\n\n bool valid(int i, int j,int row, int col){\n if (i >= 0 && i<row && j>=0 && j<col)return true;\n else return false;\n }\n\n void bfs(vector<vector<int>>& grid,int si, int sj){\n int row =... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "class Solution {\npublic:\n int cnt=0;\n bool vis[1005][1005]={false};\n int flag=0;\n\n bool valid(int i, int j,int row, int col){\n if (i >= 0 && i<row && j>=0 && j<col)return true;\n else return false;\n }\n\n void bfs(vector<vector<int>>& grid,int si, int sj){\n int row =... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "class Solution {\n set<pair<int, int>> visited;\n\npublic:\n int dfs(vector<vector<int>>& grid, int row, int col, int n, int m){\n if(row < 0 || col < 0 || row == n || col == m) return 0;\n\n if(visited.find({row, col}) != visited.end() || grid[row][col] == 1) return 1;\n vi... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "class Solution {\npublic:\n int closedIsland(vector<vector<int>>& grid) {\n int rows = grid.size();\n int cols = grid[0].size();\n int count = 0;\n set<pair<int,int>> visited;\n\n for(int r = 0; r < rows; r++)\n {\n for(int c = 0; c < cols; c++)\n ... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "class Solution {\npublic:\n bool bfs(vector<vector<int>>& grid,vector<vector<int>> &vis,\n int n,int m,int i,int j){\n bool ans=true;\n vis[i][j]=1;\n queue<pair<int,int>> q;\n q.push({i,j});\n while(!q.empty()){\n int row=q.front().first;\n in... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "class Solution {\n set<pair<int, int>> visited;\n vector<vector<int>> ans;\n\npublic:\n int dfs(vector<vector<int>>& grid, int row, int col, int n, int m){\n if(row < 0 || col < 0 || row == n || col == m) return 0;\n\n if(visited.find({row, col}) != visited.end() || grid[row... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "class Solution {\n vector<vector<int>> memo;\n set<pair<int, int>> visited;\n int ans;\n int dfs(vector<vector<int>>& grid, int row, int col, int n, int m){\n if(row > n-1 || col > m -1 || row < 0 || col < 0) return 0;\n if(grid[row][col] == 1 || visited.find({row, col}) != visite... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "class Solution {\npublic:\n void bfs(int row,int col,vector<vector<int>>& grid,vector<vector<int>>&visited){\n visited[row][col]=1;\n int n=grid.size();\n int m=grid[0].size();\n queue<pair<int,int>>q;\n q.push({row,col});\n while(!q.empty()){\n int r... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "class Solution {\npublic:\n int closedIsland(vector<vector<int>>& grid) {\n\n int ans = 0;\n int n =grid.size();\n int m=grid[0].size();\n\n for(int i=0;i<m;i++) {\n if(grid[0][i]==0)\n dfs(grid,0,i);\n }\n for(int i=0;i<n;i++) {\n ... |
1,380 | <p>Given a 2D <code>grid</code> consists of <code>0s</code> (land) and <code>1s</code> (water). An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em> is an island <strong>totally</strong> (all left, top, ri... | 3 | {
"code": "class Solution {\npublic:\n int closedIsland(vector<vector<int>>& grid) {\n int rows = grid.size(), cols = grid[0].size();\n set<pair<int,int>> visit;\n\n function<int(int,int)> dfs = [&](int r, int c){\n if(min(r, c) <0 ||\n r >= rows || c >= cols){\n ... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.