id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
459 | <p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abab"
<strong>Output:</strong> true
<strong>Explanation:</strong> It is the substring "ab" twice.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aba"
<strong>Output:</strong> false
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "abcabcabcabc"
<strong>Output:</strong> true
<strong>Explanation:</strong> It is the substring "abc" four times or the substring "abcabc" twice.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n string temp=\"\";\n\n for(int i=0;i<s.length();i++){\n temp+=s[i];\n int j=i+1;\n bool flag=true;\n while(j<s.length()){\n int b=temp.length();\n string k = s.substr(j,b);\n \n if(k!=temp) { flag=false; break;}\n j+=temp.length();\n }\n if(flag==true && i+1<s.length()) return true;\n }\n\n return false;\n }\n};",
"memory": "433848"
} |
459 | <p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abab"
<strong>Output:</strong> true
<strong>Explanation:</strong> It is the substring "ab" twice.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aba"
<strong>Output:</strong> false
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "abcabcabcabc"
<strong>Output:</strong> true
<strong>Explanation:</strong> It is the substring "abc" four times or the substring "abcabc" twice.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n string sub;\n for (int i = 0; i < s.size() - 1; ++i) {\n sub += s[i];\n bool flag = true;\n for (int j = i + 1; j < s.size(); j += sub.size()) {\n if (sub != s.substr(j, sub.size())) {\n flag = false;\n break;\n } \n }\n if (flag) {\n return true;\n }\n }\n return false;\n }\n};\n",
"memory": "433848"
} |
459 | <p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abab"
<strong>Output:</strong> true
<strong>Explanation:</strong> It is the substring "ab" twice.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aba"
<strong>Output:</strong> false
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "abcabcabcabc"
<strong>Output:</strong> true
<strong>Explanation:</strong> It is the substring "abc" four times or the substring "abcabc" twice.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n for(int i=1; i<=s.size()/2; ++i){\n string sub = s.substr(0, i);\n string newstr;\n while(newstr.size()<s.size()){\n newstr+=sub;\n }\n if(newstr == s) return true;\n }\n return false;\n }\n};",
"memory": "440321"
} |
459 | <p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abab"
<strong>Output:</strong> true
<strong>Explanation:</strong> It is the substring "ab" twice.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aba"
<strong>Output:</strong> false
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "abcabcabcabc"
<strong>Output:</strong> true
<strong>Explanation:</strong> It is the substring "abc" four times or the substring "abcabc" twice.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n\n // queue<int> q;\n\n // for(int i=0;i<s.size();i++){\n // if(s[i]!=q.front()){\n // q.push(s[i]);\n // }\n // else{\n // q.pop();\n // }\n // }\n\n // if(q.empty()|| q.size()%2==0){\n // return true;\n // }\n\n // return false;\n int n = s.size();\n\n for (int i = 1; i <= n / 2; i++) {\n\n string temp = s.substr(0, i);\n string result = \"\";\n\n while (result.size() < s.size()) {\n result += temp;\n }\n if (result == s) {\n return true;\n }\n }\n\n return false;\n }\n};",
"memory": "440321"
} |
459 | <p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abab"
<strong>Output:</strong> true
<strong>Explanation:</strong> It is the substring "ab" twice.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aba"
<strong>Output:</strong> false
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "abcabcabcabc"
<strong>Output:</strong> true
<strong>Explanation:</strong> It is the substring "abc" four times or the substring "abcabc" twice.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool sysmetric(string s) {\n return string(s.begin(), s.begin() + s.size() / 2) == string(s.begin() + s.size() / 2, s.end());\n }\n bool repeatedSubstringPattern(string s) {\n if(sysmetric(s)) return true;\n for(int i = 1; i <= s.length() / 3; i++) {\n string sub_front(s.begin() + i, s.end());\n string sub_back(s.begin(), s.end() - i);\n if(sysmetric(sub_front) && sysmetric(sub_back) && sub_front == sub_back) return true;\n }\n return false;\n }\n};",
"memory": "446793"
} |
459 | <p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abab"
<strong>Output:</strong> true
<strong>Explanation:</strong> It is the substring "ab" twice.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aba"
<strong>Output:</strong> false
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "abcabcabcabc"
<strong>Output:</strong> true
<strong>Explanation:</strong> It is the substring "abc" four times or the substring "abcabc" twice.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n int n=s.length();\n // if(n%2) return false;\n \n vector<string>vec;\n string tmp;\n // all possible substring patterns, 1st letter vala char toh always include krna hoga, else bich mein se possible nhi \n for(int i=0;i<n;i++){\n tmp+=s[i];\n if(tmp.length()>n/2) break;\n vec.push_back(tmp);\n }\n for(auto &sub:vec){\n // cout<<sub<<endl;\n int len=sub.length();\n int i=0;\n // bool found=false;\n while(i+len<=n){\n string str=s.substr(i, len);\n if(str!=sub) break;\n i+=len;\n } \n if(i==n) return true;\n }\n return false;\n }\n};",
"memory": "446793"
} |
459 | <p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abab"
<strong>Output:</strong> true
<strong>Explanation:</strong> It is the substring "ab" twice.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aba"
<strong>Output:</strong> false
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "abcabcabcabc"
<strong>Output:</strong> true
<strong>Explanation:</strong> It is the substring "abc" four times or the substring "abcabc" twice.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n int i=1;\n int t=i;\n while(i<=s.size()/2){\n if (s.substr(0,i)==s.substr(t,i)){\n if (t+i==s.size()) return true;\n t=t+i;\n continue;\n }\n i++;\n t=i;\n }\n return false;\n }\n};",
"memory": "453266"
} |
459 | <p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abab"
<strong>Output:</strong> true
<strong>Explanation:</strong> It is the substring "ab" twice.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aba"
<strong>Output:</strong> false
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "abcabcabcabc"
<strong>Output:</strong> true
<strong>Explanation:</strong> It is the substring "abc" four times or the substring "abcabc" twice.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n // sliding window approach\n\n int w = 1;\n int max_size = s.size() / 2;\n while(w <= max_size)\n {\n // std::cout << w << \" \" << max_size << endl;\n for(int i = 0; i+w <= s.size();i= i+w)\n {\n // std::cout << w << \" \" << max_size << endl;\n if(s.substr(i, w) == s.substr(i+w, w) )\n {\n if(i + 2*w == s.size())\n {\n return true;\n }\n std::cout << i << \" \" << w << endl;\n }\n else\n {\n break;\n }\n }\n w++;\n }\n return false;\n\n }\n};",
"memory": "459738"
} |
459 | <p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abab"
<strong>Output:</strong> true
<strong>Explanation:</strong> It is the substring "ab" twice.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aba"
<strong>Output:</strong> false
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "abcabcabcabc"
<strong>Output:</strong> true
<strong>Explanation:</strong> It is the substring "abc" four times or the substring "abcabc" twice.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool repeatedSubstringPattern(std::string s) {\n int len = s.size();\n int j {1};\n int counter {0};\n while (j < (len / 2) + 1){\n \n for(int i {0}; i < len ; i = i + j){\n // std::cout<< \"s.substr(\" << i << \" , \" << j << \"): \" << s.substr(i , j) << std::endl;\n // std::cout<< \"s.substr(\" << i + j << \", \" << j << \"): \" << s.substr(i + j , j) << std::endl;\n\n if (s.substr(i , j) == s.substr(i + j , j)){\n counter++;\n // std::cout<< \"counter: \" << counter << std::endl;\n if (counter == std::ceil((double(len) / double(j)) - 1)){\n return true;\n }\n }else{\n j++;\n counter = 0; \n break;\n }\n }\n\n }\n return false;\n }\n};",
"memory": "459738"
} |
459 | <p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abab"
<strong>Output:</strong> true
<strong>Explanation:</strong> It is the substring "ab" twice.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aba"
<strong>Output:</strong> false
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "abcabcabcabc"
<strong>Output:</strong> true
<strong>Explanation:</strong> It is the substring "abc" four times or the substring "abcabc" twice.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool solve(string s1, string s2){\n int i = 0, j = 0;\n string str = \"\";\n while(str.size() < s2.size()){\n str+= s1;\n }\n if(str == s2){\n return true;\n }\n else{\n return false;\n }\n }\n \n bool repeatedSubstringPattern(string s) {\n int i = 0, j = 1;\n while(i<j && j<=s.size()/2){\n bool ans = solve(s.substr(0, i+1), s.substr(j, s.size()-j));\n if(ans)\n return true;\n else{\n i++;\n j++;\n }\n }\n return false;\n }\n};",
"memory": "466211"
} |
459 | <p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abab"
<strong>Output:</strong> true
<strong>Explanation:</strong> It is the substring "ab" twice.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aba"
<strong>Output:</strong> false
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "abcabcabcabc"
<strong>Output:</strong> true
<strong>Explanation:</strong> It is the substring "abc" four times or the substring "abcabc" twice.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool repeatedSubstringPattern(std::string s) {\n \n int j {1};\n int counter {0};\n while (j < s.size() + 1){\n \n for(int i {0}; i < s.size(); i = i + j){\n // std::cout<< \"s.substr(\" << i << \" , \" << j << \"): \" << s.substr(i , j) << std::endl;\n // std::cout<< \"s.substr(\" << i + j << \", \" << j << \"): \" << s.substr(i + j , j) << std::endl;\n if ( s.substr(i , j).size() > s.substr(i + j , j).size() ){\n return false;\n }\n if (s.substr(i , j) == s.substr(i + j , j)){\n counter++;\n // std::cout<< \"counter: \" << counter << std::endl;\n if (counter == std::ceil((double(s.size()) / double(j)) - 1)){\n return true;\n }\n }else{\n j++;\n counter = 0;\n break;\n }\n }\n\n }\n return false;\n }\n};",
"memory": "472683"
} |
459 | <p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abab"
<strong>Output:</strong> true
<strong>Explanation:</strong> It is the substring "ab" twice.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aba"
<strong>Output:</strong> false
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "abcabcabcabc"
<strong>Output:</strong> true
<strong>Explanation:</strong> It is the substring "abc" four times or the substring "abcabc" twice.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n int n = s.length();\n\n string ss = s.substr(1, n - 1) + s[0];\n for(int i=0;i<n/2;i++){\n if(ss == s)\n return true;\n ss = ss.substr(1, n - 1) + ss[0];\n }\n\n return false;\n }\n};",
"memory": "505046"
} |
459 | <p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abab"
<strong>Output:</strong> true
<strong>Explanation:</strong> It is the substring "ab" twice.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aba"
<strong>Output:</strong> false
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "abcabcabcabc"
<strong>Output:</strong> true
<strong>Explanation:</strong> It is the substring "abc" four times or the substring "abcabc" twice.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n int size = s.size();\n for (int i = 1; i < size; i++) {\n string sub = s.substr(0,i);\n int size2 = sub.size();\n string fullstring;\n for (int j = 0; j < size; j += size2 ) {\n fullstring += sub;\n }\n if (fullstring == s) {\n return true;\n }\n //size/i find when there is an integer multiple \n }\n return false;\n }\n};",
"memory": "511518"
} |
459 | <p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abab"
<strong>Output:</strong> true
<strong>Explanation:</strong> It is the substring "ab" twice.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aba"
<strong>Output:</strong> false
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "abcabcabcabc"
<strong>Output:</strong> true
<strong>Explanation:</strong> It is the substring "abc" four times or the substring "abcabc" twice.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool repeatPattern(string& pattern, string& s) {\n string repeated;\n while (repeated.size() < s.size()) {\n repeated += pattern;\n }\n return repeated == s;\n }\n\n bool repeatedSubstringPattern(string s) {\n for (int i = 1; i < s.size(); i++) {\n string sub = s.substr(0, i);\n if (repeatPattern(sub, s)) {\n return true;\n }\n }\n return false;\n }\n};",
"memory": "517991"
} |
459 | <p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abab"
<strong>Output:</strong> true
<strong>Explanation:</strong> It is the substring "ab" twice.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aba"
<strong>Output:</strong> false
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "abcabcabcabc"
<strong>Output:</strong> true
<strong>Explanation:</strong> It is the substring "abc" four times or the substring "abcabc" twice.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n for(int i=1;i<s.length();i++){\n string substr=s.substr(0,i);\n string str=substr;\n while(str.length()<s.length()){\n str+=substr;\n }\n if(str==s){ \n return true;\n } \n }\n\n return false;\n }\n};",
"memory": "517991"
} |
459 | <p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abab"
<strong>Output:</strong> true
<strong>Explanation:</strong> It is the substring "ab" twice.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aba"
<strong>Output:</strong> false
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "abcabcabcabc"
<strong>Output:</strong> true
<strong>Explanation:</strong> It is the substring "abc" four times or the substring "abcabc" twice.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s)\n {\n for (int i = 1 ; i <= s.length() / 2; i++)\n {\n string temp = s.substr(i) + s.substr(0, i);\n if (s == temp)\n return true;\n }\n return false;\n }\n};",
"memory": "524463"
} |
459 | <p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abab"
<strong>Output:</strong> true
<strong>Explanation:</strong> It is the substring "ab" twice.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aba"
<strong>Output:</strong> false
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "abcabcabcabc"
<strong>Output:</strong> true
<strong>Explanation:</strong> It is the substring "abc" four times or the substring "abcabc" twice.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s)\n {\n for (int i = 1 ; i <= s.length() / 2; i++)\n {\n string temp = s.substr(i) + s.substr(0, i);\n if (s == temp)\n return true;\n }\n return false;\n }\n};",
"memory": "524463"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int islandPerimeter(vector<vector<int>>& grid) \n {\n int r=grid.size();\n int c=grid[0].size();\n int co=0;\n int row[4]={-1,0,1,0};\n int col[4]={0,-1,0,1};\n //int rn=0,cn=0;\n for(int i=0;i<r;i++)\n {\n for(int j=0;j<c;j++)\n {\n if(grid[i][j]==1)\n {\n if((i!=0&&grid[i+row[0]][j+col[0]]!=1)||i==0)\n {\n co++;\n }\n \n if((j!=0&&grid[i+row[1]][j+col[1]]!=1)||j==0)\n {\n co++;\n }\n if((i!=r-1&&grid[i+row[2]][j+col[2]]!=1)||i==r-1)\n {\n co++;\n }\n if((j!=c-1&&grid[i+row[3]][j+col[3]]!=1)||j==c-1)\n {\n co++;\n }\n \n\n }\n }\n }\n return co;\n\n \n }\n};",
"memory": "12182"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int islandPerimeter(vector<vector<int>>& grid) {\n int n = grid.size();\n\n int ans = 0;\n for(int i = 0; i < n; i++){\n for(int j = 0; j < grid[i].size(); j++){\n if(grid[i][j] == 1){\n if(i == 0) ans++; // if island is present at first row then \n else if(grid[i - 1][j] == 0) ans++;\n\n if(i == n - 1) ans++;\n else if(grid[i + 1][j] == 0) ans++;\n\n if(j == grid[i].size() - 1) ans++;\n else if(grid[i][j + 1] == 0) ans++;\n\n if(j == 0) ans++;\n else if(grid[i][j - 1] == 0) ans++; \n\n }\n }\n }\n\n return ans;\n\n }\n};",
"memory": "12182"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int islandPerimeter(vector<vector<int>>& grid) \n {\n int r=grid.size();\n int c=grid[0].size();\n int co=0;\n int row[4]={-1,0,1,0};\n int col[4]={0,-1,0,1};\n //int rn=0,cn=0;\n for(int i=0;i<r;i++)\n {\n for(int j=0;j<c;j++)\n {\n if(grid[i][j]==1)\n {\n if((i!=0&&grid[i+row[0]][j+col[0]]!=1)||i==0)\n {\n co++;\n }\n \n if((j!=0&&grid[i+row[1]][j+col[1]]!=1)||j==0)\n {\n co++;\n }\n if((i!=r-1&&grid[i+row[2]][j+col[2]]!=1)||i==r-1)\n {\n co++;\n }\n if((j!=c-1&&grid[i+row[3]][j+col[3]]!=1)||j==c-1)\n {\n co++;\n }\n \n\n }\n }\n }\n return co;\n\n \n }\n};",
"memory": "13577"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int islandPerimeter(vector<vector<int>>& grid) {\n int islands = 0;\n int neighbors = 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) {\n islands++;\n if (i - 1 >= 0 && grid[i - 1][j] == 1) {\n neighbors++;\n }\n if (j - 1 >= 0 && grid[i][j - 1] == 1) {\n neighbors++;\n }\n }\n }\n }\n\n return islands * 4 - neighbors * 2;\n }\n};",
"memory": "13577"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int islandPerimeter(vector<vector<int>>& grid) {\n\n\n\n\n vector<pair<int,int>> directions ={{1,0},{-1,0},{0,1},{0,-1}};\n int result=0;\n\n for(int i=0;i<grid.size();i++)\n {\n for(int j=0;j<grid[0].size();j++)\n {\n for(auto& d:directions)\n {\n if (grid[i][j]==0) continue;\n int newX = i+d.first;\n int newY = j+d.second;\n if (newX>=grid.size() || newX <0 || newY>=grid[0].size() || newY<0)\n {\n result+=1;\n continue;\n }\n\n if (grid[newX][newY] == 0) result+=1;\n }\n }\n }\n\n return result;\n \n }\n};",
"memory": "14972"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int dfs(int row, int column, vector<vector<int>>& grid, int ans){\n if(row < 0 || column < 0 || row >= grid.size() || column >=grid[0].size() || grid[row][column] == 0){\n return 1;\n }\n if(grid[row][column] == -1){\n return 0;\n }\n grid[row][column] = -1;\n ans = dfs(row+1,column,grid,ans);\n ans+=dfs(row-1,column,grid,ans);\n ans+=dfs(row,column-1,grid,ans);\n ans+=dfs(row,column+1,grid,ans);\n return ans;\n }\n int islandPerimeter(vector<vector<int>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n int ans = 0;\n for(int i = 0; i<m; i++){\n for(int j = 0; j<n; j++){\n if(grid[i][j] == 1){\n return dfs(i,j,grid,ans);\n }\n }\n }\n return 0;\n }\n};",
"memory": "14972"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int islandPerimeter(vector<vector<int>>& grid) {\n\n\n\n\n vector<pair<int,int>> directions ={{1,0},{-1,0},{0,1},{0,-1}};\n int result=0;\n\n for(int i=0;i<grid.size();i++)\n {\n for(int j=0;j<grid[0].size();j++)\n {\n for(auto& d:directions)\n {\n if (grid[i][j]==0) continue;\n int newX = i+d.first;\n int newY = j+d.second;\n if (newX>=grid.size() || newX <0 || newY>=grid[0].size() || newY<0)\n {\n result+=1;\n continue;\n }\n\n if (grid[newX][newY] == 0) result+=1;\n }\n }\n }\n\n return result;\n \n }\n};",
"memory": "16367"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int dfs(vector<vector<int>>& grid, int r, int c, int p) {\n if (r < 0 || c < 0 || r >= grid.size() || c >= grid[0].size() || grid[r][c] == 0) return 1;\n if (grid[r][c] == -1) return 0;\n grid[r][c] = -1;\n p = dfs(grid, r + 1, c, p);\n p += dfs(grid, r - 1, c, p);\n p += dfs(grid, r, c + 1, p);\n p += dfs(grid, r, c - 1, p);\n return p;\n }\n\n int islandPerimeter(vector<vector<int>>& grid) {\n int p = 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) return dfs(grid, i, j, p);\n }\n }\n return 0;\n }\n};",
"memory": "16367"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int m;\n int n;\n int perimeter;\n void dfs(vector<vector<int>>& grid, int i, int j){\n if(i<0 ||i>=m || j<0 ||j>=n || grid[i][j]==0) {//out of bound or if water present\n perimeter++;\n return;//imp or else wont go to the next possibility cell!!!!\n }\n if(grid[i][j]== -1 ){\n return;\n }\n grid[i][j]=-1;//mat=rk visited\n\n dfs(grid,i+1,j);\n dfs(grid,i-1,j);\n dfs(grid,i,j+1);\n dfs(grid,i,j-1);\n }\n int islandPerimeter(vector<vector<int>>& grid) {\n //best problem to get started and practice regularly for dfs and bfs !\n m=grid.size();\n n=grid[0].size();\n perimeter=0;\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);\n return perimeter;\n }\n \n }\n }\n return -1;\n }\n};",
"memory": "17762"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int n;\n int m;\n int peri;\n \n void DFS(vector<vector<int>>& grid,int i,int j)\n {\n if(i<0||i>=m||j<0||j>=n||grid[i][j]==0)\n {\n peri++;\n return;\n }\n if(grid[i][j]==-1)\n {\n return;\n }\n grid[i][j]=-1;//mark visited\n DFS(grid,i+1,j);\n DFS(grid,i-1,j);\n DFS(grid,i,j+1);\n DFS(grid,i,j-1);\n }\n int islandPerimeter(vector<vector<int>>& grid) \n {\n m=grid.size();//no of rows\n n=grid[0].size();//No of columns (assuming all rows have the same number of columns)\n peri=0;\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 DFS(grid,i,j);\n return peri;\n }\n }\n }\n return -1;\n }\n};",
"memory": "17762"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\n void dfs(vector<vector<int>>& grid,int i,int row,int j,int col,int &ans)\n {\n // Out of bounds or water cell, count as part of the perimeter\n if (i < 0 || j < 0 || i >= row || j >= col || grid[i][j] == 0) {\n ans++;\n return;\n }\n\n if (grid[i][j] == 2) {\n return;\n }\n\n grid[i][j] = 2;\n\n dfs(grid,i+1,row,j,col,ans);\n dfs(grid,i,row,j+1,col,ans);\n dfs(grid,i-1,row,j,col,ans);\n dfs(grid,i,row,j-1,col,ans);\n\n return;\n }\n int islandPerimeter(vector<vector<int>>& grid) {\n\n int row = grid.size();\n if(row == 0)\n return 0;\n \n int col = grid[0].size();\n\n int ans = 0;\n for(int i = 0; i<row; i++)\n {\n for(int j = 0; j<col; j++)\n {\n if(grid[i][j] ==1){\n dfs(grid,i,row,j,col,ans);\n }\n }\n }\n return ans;\n }\n};",
"memory": "19157"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int islandPerimeter(vector<vector<int>>& grid) {\n m = grid.size();\n n = grid.front().size();\n ans = 0;\n for (int ii = 0; ii < m; ++ii) {\n for (int jj = 0; jj < n; ++jj) {\n if (grid[ii][jj]==1) dfs(grid, ii, jj);\n }\n }\n return ans;\n }\n void dfs(vector<vector<int>>& grid, int x, int y) {\n grid[x][y] = -1;\n int diff_x[4] = {0,1,0,-1};\n int diff_y[4] = {1,0,-1,0};\n for (int ii = 0; ii < 4; ++ii) {\n int xx = x + diff_x[ii];\n int yy = y + diff_y[ii];\n if (xx>=0 && xx <m && yy>=0 && yy<n) {\n if (grid[xx][yy]==1) dfs(grid,xx,yy);\n else if (grid[xx][yy]==0) ans++;\n }\n else ans++;\n }\n return;\n }\nprivate:\n int m, n;\n int ans;\n};",
"memory": "20552"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int sum;\n bool check(vector<vector<int>>& grid, int i, int j){\n if(i>-1 && i<grid.size() && j >-1 &&j<grid[0].size() && grid[i][j]==1)\n return true;\n return false;\n \n }\n \n bool check2(vector<vector<int>>& grid, int i, int j){\n if(i>-1 && i<grid.size() && j >-1 &&j<grid[0].size() && grid[i][j])\n return true;\n return false;\n \n }\n \n \n \n void compute_sum(vector<vector<int>>& grid, int i, int j){\n sum++;\n grid[i][j]++;\n if(check(grid,i+1,j)){\n \n compute_sum(grid,i+1,j);\n }\n if(check(grid,i-1,j)){\n\n compute_sum(grid,i-1,j);\n }\n if(check(grid,i,j+1)){\n compute_sum(grid,i,j+1);\n }\n if(check(grid,i,j-1)){\n compute_sum(grid,i,j-1);\n }\n \n }\n \n \n int islandPerimeter(vector<vector<int>>& grid) {\n \n sum = 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){\n compute_sum(grid, i,j);\n }\n }\n }\n \n int ans = sum*4;\n int minus_ans = 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]){\n if(check2(grid,i,j-1))\n minus_ans++;\n if(check2(grid,i,j+1))\n minus_ans++;\n if(check2(grid,i-1,j))\n minus_ans++;\n if(check2(grid,i+1,j))\n minus_ans++;\n }\n }\n } \n \n \n return ans - minus_ans;\n \n \n \n \n \n \n \n \n \n \n \n \n }\n};",
"memory": "20552"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\nprivate:\n int ans = 0, row, col;\n int dir[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};\n bool vis[100][100] = {false};\n\n bool isValid(int i, int j) {\n return i >= 0 && i < row && j >= 0 && j < col;\n }\n int dfs(int si, int sj, vector<vector<int>>& grid) {\n vis[si][sj] = true;\n for (int i = 0; i < 4; i++) {\n int nrow = si + dir[i][0], ncol = sj + dir[i][1];\n if (!isValid(nrow, ncol) || grid[nrow][ncol] == 0) {\n ans++;\n }\n if (isValid(nrow, ncol) && !vis[nrow][ncol] &&\n grid[nrow][ncol] == 1) {\n dfs(nrow, ncol, grid);\n }\n }\n return ans;\n }\n\npublic:\n int islandPerimeter(vector<vector<int>>& grid) {\n row = grid.size(), col = grid[0].size();\n for (int i = 0; i < row; i++) {\n for (int j = 0; j < col; j++) {\n if (!vis[i][j] && grid[i][j] == 1) {\n dfs(i, j, grid);\n }\n }\n }\n return ans;\n }\n};",
"memory": "21947"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n void visitisland(vector<vector<int>>& grid,int r,int c,int row,int col,int *peri)\n {\n\n if(r >= row || c >= col || r<0 || c < 0 ||grid[r][c]!=1)\n return;\n\n grid[r][c] = 2;\n\n *peri += (r-1 < 0 || grid[r-1][c] == 0) +\n (r+1 >= row || grid[r+1][c]==0) +\n (c-1 < 0 || grid[r][c-1]==0)+\n (c+1 >= col || grid[r][c+1]==0);\n\n visitisland(grid,r-1,c,row,col,peri); //left\n visitisland(grid,r+1,c,row,col,peri); //right\n visitisland(grid,r,c-1,row,col,peri); //up\n visitisland(grid,r,c+1,row,col,peri); //down\n return;\n\n \n }\n int islandPerimeter(vector<vector<int>>& grid) {\n int col = grid[0].size();\n int row = grid.size();\n int peri =0;\n for(int r=0;r<row;r++)\n {\n for(int c=0;c<col;c++)\n {\n if(grid[r][c]==1)\n {\n visitisland(grid,r,c,row,col,&peri);\n return peri;\n }\n \n }\n }\n return 0;\n }\n};",
"memory": "21947"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n bool vis[105][1005];\n int ans = 0;\n int n,m;\n vector<pair<int,int>> d={{0,1},{0,-1},{1,0},{-1,0}};\n bool valid(int ci, int cj)\n {\n return ci >= 0 && ci < n && cj >= 0 && cj < m;\n }\n void dfs(int si, int sj, vector<vector<int>> &grid)\n {\n vis[si][sj] = true;\n for(int i=0; i < 4; i++)\n {\n int ci = si + d[i].first;\n int cj = sj + d[i].second;\n if(valid(ci,cj))\n {\n if(grid[ci][cj]==0)ans++;\n }\n else ans++;\n if(valid(ci,cj) && !vis[ci][cj] && grid[ci][cj] == 1)\n {\n dfs(ci,cj,grid);\n }\n }\n }\n int islandPerimeter(vector<vector<int>>& grid) {\n memset(vis, false, sizeof(vis));\n n = grid.size();\n m = grid[0].size();\n for(int i = 0; i < n; i++)\n {\n for(int j = 0; j < m; j++)\n {\n if(!vis[i][j] && grid[i][j] == 1)\n {\n dfs(i,j,grid);\n }\n }\n }\n return ans;\n }\n};",
"memory": "23342"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\nprivate:\n int directions[4][2] = {{-1,0},{1, 0},{0,-1},{0,1}};\n void dfs(vector<vector<int>>& grid, int i, int j, int &primeter ){\n grid[i][j] = -1;\n for (auto [di, dj]: directions){\n if (i+di<0 || i+di>= grid.size() || j+dj<0 || j+dj>=grid[0].size() || grid[i+di][j+dj]==0)\n primeter++; \n else if (grid[i+di][j+dj]==1){\n dfs(grid, i+di, dj+j,primeter);\n }\n }\n }\npublic:\n int islandPerimeter(vector<vector<int>>& grid) {\n int primeter = 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, primeter);\n return primeter; \n }\n }\n }\n return 0;\n }\n};",
"memory": "23342"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int islandPerimeter(vector<vector<int>>& grid) {\n int a = 0, d = 0;\n int f[105][105] = {0};\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)\n {\n a += 1;\n if(i-1 >= 0 && f[i-1][j] == 1) d += 1;\n if(i+1 < grid.size() && f[i+1][j] == 1) d += 1;\n if(j-1 >= 0 && f[i][j-1] == 1) d += 1;\n if(j+1 < grid[i].size() && f[i][j+1] == 1) d += 1;\n f[i][j] = 1;\n\n }\n }\n }\n int c = a*4-d*2;\n return c;\n }\n};",
"memory": "24737"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int perimeter = 0;\n // vector<vector<bool>> vis(100 , vector<bool>(100 , false));\n int vis[100][100] = {false};\n vector<int> delrow = {-1, 0 ,1 , 0};\n vector<int> delcol = {0 , 1 , 0 , -1};\n\n void dfs(int row , int col , vector<vector<int>>& grid){\n vis[row][col] = true;\n\n for(int i = 0 ; i < 4 ; i++){\n int nrow = row + delrow[i]; \n int ncol = col + delcol[i];\n\n if(nrow >= 0 && ncol >= 0 && nrow < grid.size() && ncol < grid[0].size() && grid[nrow][ncol] == 1 && !vis[nrow][ncol]){\n dfs(nrow , ncol , grid);\n }else if(!(nrow >= 0 && ncol >= 0 && nrow < grid.size() && ncol < grid[0].size())) perimeter++;\n else if(grid[nrow][ncol] == 0) perimeter++;\n }\n }\n\n int islandPerimeter(vector<vector<int>>& grid) {\n // vector<vector<bool>> vis(grid.size() , vector<bool>(grid[0].size() , false));\n\n for(int i = 0 ; i < grid.size() ; i++){\n for(int j = 0 ; j < grid[0].size() ; j++){\n if(!vis[i][j] && grid[i][j] == 1){\n dfs(i , j , grid);\n }\n }\n }\n\n return perimeter;\n }\n};",
"memory": "26132"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n bool vis[200][200];\n int cnt;\n int r,c;\n vector<pair<int,int>> d = {{1,0},{0,1},{-1,0},{0,-1}};\n bool valid(int ci, int cj)\n {\n if(ci>=r || ci<0 || cj>=c || cj<0)\n {\n return false;\n }\n return true;\n }\n\n void dfs(int si, int sj, vector<vector<int>>& grid)\n {\n vis[si][sj] = true;\n \n for(int i=0;i<4;i++)\n {\n int ci = si + d[i].first;\n int cj = sj + d[i].second;\n if(valid(ci,cj))\n {\n if(grid[ci][cj]==0)\n {\n cnt++;\n }\n }\n else cnt++;\n if(valid(ci,cj)==true && !vis[ci][cj] && vis[ci][cj]==1)\n {\n dfs(ci,cj,grid);\n }\n }\n\n }\n int islandPerimeter(vector<vector<int>>& grid) {\n r = grid.size();\n c = grid[0].size();\n memset(vis,false,sizeof(vis));\n cnt = 0;\n for(int i=0;i<r;i++)\n {\n for(int j=0;j<c;j++)\n {\n if(!vis[i][j] && grid[i][j]==1)\n {\n dfs(i,j,grid);\n }\n }\n }\n return cnt;\n }\n};",
"memory": "27527"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\nint ans=0;\nvector<pair<int,int>>idx = {{-1,0}, {0,-1}, {1,0}, {0,1}};\nvoid dfs(vector<vector<int>>& grid, int i, int j){\n grid[i][j]=-1;\n ans+=4;\n for(int k=0;k<idx.size();k++){\n int x = i+idx[k].first;\n int y = j+idx[k].second;\n cout<<x<<\" \"<<y<<endl;\n if(x>=0 && y>=0 && x<grid.size() && y<grid[0].size()){\n if(grid[x][y]==1 || grid[x][y]==-1){\n ans--;\n if(grid[x][y]==1){\n dfs(grid, x, y);\n }\n }\n }\n }\n}\n int islandPerimeter(vector<vector<int>>& grid) {\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 }\n }\n }\n return ans;\n }\n};",
"memory": "28922"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\nprivate:\n int m;\n int n;\n vector< vector<int>> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};\npublic:\n int find_boundary(vector<vector<int>>& grid, int i, int j){\n int cells = 0;\n // if(i>0 && grid[i-1][j] == 1){\n // cells++;\n // }\n\n // if(j>0 && grid[i][j-1] == 1){\n // cells++;\n // }\n\n // if(i<m-1 && grid[i+1][j] == 1){\n // cells++;\n // }\n\n // if(j<n-1 && grid[i][j+1] == 1){\n // cells++;\n // }\n\n for(auto &dir : directions){\n int i_n = i+ dir[0];\n int j_n = j+ dir[1];\n if(i_n <0 || i_n>=m || j_n < 0|| j_n>=n){\n continue;\n }\n else if(grid[i_n][j_n]){\n cells++;\n }\n }\n\n return 4-cells;\n }\n\n int islandPerimeter(vector<vector<int>>& grid) {\n int perimeter = 0;\n m = grid.size();\n 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] == 1){\n perimeter += find_boundary(grid, i, j);\n }\n }\n }\n return perimeter;\n }\n};",
"memory": "30317"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int n, m;\n int vis[105][105];\n int cnt;\n vector<pair<int, int>> d = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};\n bool valid(int i, int j) { return (i >= 0 && i < n && j >= 0 && j < m); }\n void dfs(int si, int sj, vector<vector<int>>& grid){\n vis[si][sj] = true;\n for(int i=0;i<4;i++){\n int ci = si + d[i].first;\n int cj = sj + d[i].second;\n if(valid(ci,cj)){\n if(grid[ci][cj] == 0)\n cnt++;\n }else{\n cnt++;\n }\n if(valid(ci,cj) && !vis[ci][cj] && grid[ci][cj] == 1){\n dfs(ci,cj,grid);\n }\n }\n }\n int islandPerimeter(vector<vector<int>>& grid) {\n n = grid.size();\n m = grid[0].size();\n memset(vis, false, sizeof(vis));\n cnt = 0;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n if (!vis[i][j] && grid[i][j] == 1) {\n dfs(i, j, grid);\n }\n }\n }\n return cnt;\n }\n};",
"memory": "31712"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int vis[105][105];\n int ans;\n vector<pair<int, int>> d = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};\n int n, m;\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 void dfs(int si, int sj, vector<vector<int>>& grid) {\n vis[si][sj] = true;\n for (int i = 0; i < 4; i++) {\n int ci = si + d[i].first;\n int cj = sj + d[i].second;\n if (valid(ci, cj)) {\n if (grid[ci][cj] == 0)\n ans++;\n } else\n ans++;\n if (valid(ci, cj) && !vis[ci][cj] && grid[ci][cj] == 1) {\n dfs(ci, cj, grid);\n }\n }\n }\n int islandPerimeter(vector<vector<int>>& grid) {\n memset(vis, false, sizeof(vis));\n ans = 0;\n n = grid.size();\n m = grid[0].size();\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n if (valid(i, j) && !vis[i][j] && grid[i][j] == 1) {\n dfs(i, j, grid);\n }\n }\n }\n return ans;\n }\n};",
"memory": "33107"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int n,m;\n bool vis[105][105];\n int ans = 0;\n vector<pair<int,int>> d={{1,0},{-1,0},{0,1},{0,-1}};\n bool valid(int i, int j)\n {\n return i >= 0 && i < n && j >= 0 && j < m;\n }\n void dfs(int si, int sj, vector<vector<int>> &grid)\n {\n vis[si][sj] = true;\n for(int i = 0; i < 4; i++)\n {\n int ci = si + d[i].first;\n int cj = sj + d[i].second;\n if(!valid(ci,cj)) ans++;\n else\n if(grid[ci][cj] == 0) ans++;\n if(valid(ci,cj) && !vis[ci][cj] && grid[ci][cj] == 1)\n {\n dfs(ci,cj,grid);\n }\n }\n }\n int islandPerimeter(vector<vector<int>>& grid) {\n memset(vis, false, sizeof(vis));\n n = grid.size();\n m = grid[0].size();\n for(int i = 0; i < n; i++)\n {\n for(int j = 0; j < m; j++)\n {\n if(!vis[i][j] && grid[i][j] == 1)\n {\n dfs(i,j,grid);\n }\n }\n }\n return ans;\n }\n};",
"memory": "34502"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\nbool vis[105][105];\nint ans;\nvector<pair<int,int>>d= {{0,1},{0,-1},{1,0},{-1,0}};\nint n, m;\nbool valid(int ci,int cj)\n{\n if(ci>=0 && ci<n && cj>=0 && cj<m) return true;\n else return false;\n}\nvoid dfs(int si, int sj, vector<vector<int>>& grid)\n{\n vis[si][sj]=true;\n for(int i=0; i<4; i++)\n {\n int ci= si+d[i].first;\n int cj= sj+d[i].second;\n if(valid(ci,cj))\n {\n if(grid[ci][cj]==0) ans++;\n }\n else\n {\n ans++;\n }\n if(valid(ci,cj) && !vis[ci][cj] && grid[ci][cj]==1)\n {\n dfs(ci,cj,grid);\n }\n }\n}\n int islandPerimeter(vector<vector<int>>& grid) {\n memset(vis,false,sizeof(vis));\n ans=0;\n n=grid.size();\n m=grid[0].size();\n for(int i=0; i<n;i++)\n {\n for(int j=0; j<m;j++)\n {\n if(!vis[i][j] && grid[i][j]==1)\n {\n dfs(i,j,grid);\n }\n }\n } \n return ans;\n }\n};",
"memory": "34502"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int row,col,perimeter;\n bool vis[100][100];\n vector<pair<int,int>>d={{0,-1},{0,1},{-1,0},{1,0}};\n\n bool valid(int i,int j){\n return 0<=i&&i<row&&0<=j&&j<col;\n }\n\n void dfs(int i,int j,vector<vector<int>>&grid){\n vis[i][j]=true;\n for(int it=0;it<4;it++){\n int ii=i+d[it].first,jj=j+d[it].second;\n if(!valid(ii,jj)||(valid(ii,jj)&&!grid[ii][jj]))perimeter++;\n if(valid(ii,jj)&&!vis[ii][jj]&&grid[ii][jj])dfs(ii,jj,grid);\n }\n }\n\n int islandPerimeter(vector<vector<int>>& grid) {\n row=grid.size(),col=grid[0].size();\n memset(vis,false,sizeof(vis));\n perimeter=0;\n for(int i=0;i<row;i++){\n for(int j=0;j<col;j++){\n if(!vis[i][j]&&grid[i][j])dfs(i,j,grid);\n }\n }\n return perimeter;\n }\n};\n",
"memory": "35897"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 2 | {
"code": "class Solution\n{\n public:\n bool vis[105][105];\n int ans;\n vector<pair<int,int>> d={{0,1},{0,-1},{-1,0},{1,0}};\n int n,m;\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 void dfs(int si, int sj,vector<vector<int>>& grid)\n {\n vis[si][sj]=true;\n for(int i=0;i<4;i++)\n {\n int ci=si+d[i].first;\n int cj=sj+d[i].second;\n if(valid(ci,cj))\n {\n if(grid[ci][cj]==0) ans++;\n }\n else\n {\n ans++;\n }\n if(valid(ci,cj) && !vis[ci][cj] && grid[ci][cj]==1)\n {\n dfs(ci,cj,grid);\n }\n }\n }\n int islandPerimeter(vector<vector<int>> & grid)\n {\n memset(vis,false,sizeof(vis));\n ans=0;\n n=grid.size();\n m=grid[0].size();\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<m;j++)\n {\n if(!vis[i][j] && grid[i][j]==1)\n {\n dfs(i,j,grid);\n }\n }\n }\n return ans;\n }\n};",
"memory": "35897"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int islandPerimeter(vector<vector<int>>& grid) {\n if (grid.size() == 0)\n return 0;\n\n int rows = grid.size();\n int cols = grid[0].size();\n int perimeter = 0;\n vector<vector<bool>> visit(rows, vector<bool>(cols, false));\n\n for (int i = 0; i < rows; i++) {\n for (int j = 0; j < cols; j++) {\n // If current cell is land, hasn't visited\n if (grid[i][j] == 1 && visit[i][j] == false)\n perimeter += dfs(i, j, grid, visit);\n }\n }\n return perimeter;\n }\n\n int dfs(int row, int col, vector<vector<int>>& grid, vector<vector<bool>>& visit) {\n int rows = grid.size();\n int cols = grid[0].size();\n\n // Mark visited\n visit[row][col] = true;\n\n int perimeter = 0;\n int directions[4][2] = {\n {-1, 0}, // Left\n { 1, 0}, // Right\n { 0, -1}, // Up\n { 0, 1} // Down\n };\n\n for (auto dir : directions) {\n int newRow = row + dir[0];\n int newCol = col + dir[1];\n\n // If the neighbor is water or out of bounds, length of 1 is added\n if (newRow < 0 || newRow >= rows || newCol < 0 || newCol >= cols || grid[newRow][newCol] == 0)\n perimeter++;\n // If the neighbor is land, don't do anything\n }\n\n return perimeter;\n }\n};",
"memory": "41477"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>>value{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};\n int func(vector<vector<int>>& grid, int i, int j) {\n if (i < 0 || j < 0 || i == grid.size() || j == grid[0].size() || grid[i][j] == 0 || grid[i][j] == 2) return 0;\n int ans = 0;\n if (i-1 < 0 || (grid[i-1][j] == 0)) ++ans;\n if (j-1 < 0 || (grid[i][j-1] == 0)) ++ans;\n if (i+1 >= grid.size() || grid[i+1][j] == 0) ++ans;\n if (j+1 >= grid[0].size() || grid[i][j+1] == 0) ++ans;\n grid[i][j] = 2;\n for(int k = 0; k < value.size(); ++k) {\n ans += func(grid, i + value[k][0], j+value[k][1]);\n }\n \n return ans;\n }\n int islandPerimeter(vector<vector<int>>& grid) {\n for(int i = 0; i < grid.size(); ++i) {\n for(int j = 0; j < grid[0].size(); ++j) {\n if (grid[i][j]) return func(grid, i, j);\n }\n }\n return 0;\n }\n};",
"memory": "42872"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int islandPerimeter(vector<vector<int>>& grid) {\n int m = grid.size(), n = grid[0].size(), res = 0;\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n if (grid[i][j] == 1) {\n grid[i][j] = 2;\n return dfs(grid, i, j);\n }\n }\n }\n\n return 0;\n }\n\n vector<vector<int>> dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};\n\n int dfs(vector<vector<int>>& grid, int y, int x) {\n int m = grid.size(), n = grid[0].size(), res = 0;\n if (y == 0) {\n res++;\n }\n if (x == 0) {\n res++;\n }\n if (y == m-1) {\n res++;\n }\n if (x == n-1) {\n res++;\n }\n // grid[y][x] = 2;\n for (const auto& v : dir) {\n int a = y + v[0], b = x + v[1];\n if (a < 0 || a >= m || b < 0 || b >= n) continue;\n if (grid[a][b] == 0) {\n res++;\n } else if (grid[a][b] == 1) {\n grid[a][b] = 2;\n res += dfs(grid, a, b);\n }\n }\n // grid[y][x] = 1;\n return res;\n }\n};",
"memory": "44267"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int n,m;\n int dfs(int i,int j,vector<vector<int>> & grid,vector<vector<bool>> & visited){\n if (i < 0 || j < 0 || i >= n || j >= m || grid[i][j] == 0) {\n return 1;\n }\n if(visited[i][j]){\n return 0;\n }\n visited[i][j]=1;\n int ans1= dfs(i+1,j,grid,visited);\n int ans2=dfs(i-1,j,grid,visited);\n int ans3= dfs(i,j+1,grid,visited);\n int ans4=dfs(i,j-1,grid,visited);\n return ans1+ans2+ans3+ans4;\n }\n int islandPerimeter(vector<vector<int>>& grid) {\n n=grid.size();\n m=grid[0].size();\n int ans=0;\n vector<vector<bool>>visited(n,vector<bool>(m,0));\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]==0){\n ans+= dfs(i,j,grid,visited);\n }\n }\n }\n return ans;\n }\n};",
"memory": "45662"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\n\n int dfs(int i,int j,vector<vector<int>>& grid,vector<vector<bool>>& vis){\n // For boundry of grid or water neighbour we return 1\n if(i<0 or i>=grid.size() or j<0 or j>=grid[0].size() or grid[i][j] == 0) return 1 ;\n if(vis[i][j]) return 0;\n vis[i][j] = true;\n int perimeter = 0;\n\n perimeter += dfs(i+1,j,grid,vis);\n perimeter += dfs(i-1,j,grid,vis);\n perimeter += dfs(i,j+1,grid,vis);\n perimeter += dfs(i,j-1,grid,vis);\n \n\n return perimeter;\n }\npublic:\n int islandPerimeter(vector<vector<int>>& grid) {\n int n=grid.size(),c=grid[0].size();\n vector<vector<bool>>vis(n,vector<bool>(c,false));\n for(int i=0;i<n;i++)\n for(int j=0;j<c;j++){\n if(grid[i][j] == 1) return dfs(i,j,grid,vis);\n }\n return -1;\n }\n};",
"memory": "47057"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\n int rows;\n int cols;\n int perimeter = 0;\npublic:\n int islandPerimeter(vector<vector<int>>& grid) {\n rows = grid.size();\n cols = grid[0].size();\n\n // We only need to find the first land cell and explore the entire island\n for (int i = 0; i < rows; i++) {\n for (int j = 0; j < cols; j++) {\n if (grid[i][j] == 1) {\n // Use DFS to explore the island and calculate the perimeter\n vector<vector<bool>> visited(rows, vector<bool>(cols, false));\n exploreIsland(grid, visited, i, j);\n return perimeter; // Return perimeter after exploring the island\n }\n }\n }\n return perimeter;\n }\n\n void exploreIsland(vector<vector<int>>& grid, vector<vector<bool>>& visited, int row, int col) {\n // If out of bounds or water, contribute to perimeter\n if (row < 0 || row >= rows || col < 0 || col >= cols || grid[row][col] == 0) {\n perimeter++;\n return;\n }\n\n // If the cell is already visited, return\n if (visited[row][col]) return;\n \n // Mark the cell as visited\n visited[row][col] = true;\n\n // Explore in all 4 directions\n exploreIsland(grid, visited, row + 1, col); // Down\n exploreIsland(grid, visited, row - 1, col); // Up\n exploreIsland(grid, visited, row, col + 1); // Right\n exploreIsland(grid, visited, row, col - 1); // Left\n }\n};",
"memory": "48452"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int islandPerimeter(vector<vector<int>>& grid) {\n // dfs\n // hit water or a border, add 1\n int perim = 0;\n vector<vector<bool>> visited(grid.size(), vector<bool>(grid[0].size()));\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 dfs(grid, i, j, visited, perim);\n }\n }\n return perim;\n }\n\n void dfs(vector<vector<int>>& grid, int i, int j, vector<vector<bool>>& visited, int& perim)\n {\n if(i < 0 || j < 0 || i >= grid.size() || j >= grid[0].size() || grid[i][j] == 0)\n {\n perim++;\n return;\n }\n if(visited[i][j] == true)\n return;\n visited[i][j] = true;\n dfs(grid, i + 1, j, visited, perim);\n dfs(grid, i - 1, j, visited, perim);\n dfs(grid, i, j + 1, visited, perim);\n dfs(grid, i, j - 1, visited, perim);\n }\n};",
"memory": "48452"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n int dfs(vector<vector<int>>& grid,vector<vector<bool>> &visited,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 || grid[i][j]==0){\n return 1;\n }\n if(visited[i][j]){\n return 0;\n }\n visited[i][j]=true;\n int perimeter=0;\n perimeter+=dfs(grid,visited,i-1,j);\n perimeter+=dfs(grid,visited,i+1,j);\n perimeter+=dfs(grid,visited,i,j-1);\n perimeter+=dfs(grid,visited,i,j+1);\n\n return perimeter;\n \n }\n int islandPerimeter(vector<vector<int>>& grid) {\n int perimeter=0;\n vector<vector<bool>> visited(grid.size(),vector<bool>(grid[0].size(),false));\n for(int i=0;i<grid.size();i++){\n for(int j=0;j<grid[0].size();j++){\n if(!visited[i][j] && grid[i][j]==1){\n perimeter+=dfs(grid,visited,i,j);\n }\n }\n }\n return perimeter;\n }\n};",
"memory": "49847"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\n \n vector<vector<bool>> vis;\n public:\n int dfs(vector<vector<int>> &grid,int i, int j){\n if(i>=grid.size() || j>=grid[0].size()|| i<0|| j<0 ||grid[i][j] == 0)\n return 1;\n\n if(vis[i][j]){\n return 0;\n \n }\n vis[i][j]=true;\n int perimeter=0;\n perimeter+=dfs(grid,i,j+1);\n perimeter+=dfs(grid,i,j-1);\n perimeter+=dfs(grid,i-1,j);\n perimeter+=dfs(grid,i+1,j);\n return perimeter;\n }\npublic:\n int islandPerimeter(vector<vector<int>>& grid) {\n vis = vector<vector<bool>>(grid.size(), vector<bool>(grid[0].size(), false));\n int res=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 && !vis[i][j]) {\n \n res+= dfs(grid, i, j);\n \n }\n }\n \n \n }\n return res;\n}\n};",
"memory": "51242"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int row[4] = {-1,1,0,0};\n int col[4] = {0,0,1,-1};\n int ans = 0;\n int dfs(vector<vector<int>>&grid,vector<vector<bool>>&vis,int i,int j){\n if(i<0 || i>=grid.size() || j<0 || j>=grid[0].size() || grid[i][j] == 0)\n return 1;\n if(vis[i][j]) return 0;\n vis[i][j] = true;\n int cnt = 0;\n for(int k=0;k<4;k++){\n cnt += dfs(grid,vis,i+row[k],j+col[k]);\n }\n ans += cnt;\n return 0;\n }\n int islandPerimeter(vector<vector<int>>& grid) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n vector<vector<bool>>vis(grid.size(),vector<bool>(grid[0].size(),false));\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 && !vis[i][j]){\n dfs(grid,vis,i,j);\n }\n }\n }\n return ans;\n }\n};",
"memory": "51242"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void dfs(vector<vector<bool>>&visited,vector<vector<int>>& grid,int row,int col,int&count){\n\n visited[row][col] = true;\n\n int drow[] = {-1,0,1,0};\n int dcol[] = {0,1,0,-1};\n\n for(int i = 0;i<4;i++){\n int nrow = row + drow[i];\n int ncol = col + dcol[i];\n\n if(nrow>=0 && nrow<grid.size() && ncol>=0 && ncol<grid[0].size() && grid[nrow][ncol]==1){\n if(visited[nrow][ncol]==false)\n dfs(visited,grid,nrow,ncol,count);\n }\n else\n {\n count++;\n }\n }\n\n }\n\n int islandPerimeter(vector<vector<int>>& grid) {\n \n int n = grid.size();\n int m = grid[0].size();\n\n vector<vector<bool>> visited(n,vector<bool>(m,false));\n\n int row = -1;\n int col = -1;\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 row = i;\n col = j;\n break;\n }\n }\n }\n\n int count = 0;\n\n dfs(visited,grid,row,col,count);\n\n return count;\n }\n};",
"memory": "52637"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int c=0;\n int check(vector<vector<int>>& v,vector<vector<int>>& vis,int i,int j)\n {\n\n if(i<0 || i>=v.size() || j<0 || j>=v[0].size() || v[i][j]==0) return 1;\n if(vis[i][j]) return 0;\n vis[i][j]=1;\n int p=check(v,vis,i+1,j);\n int q=check(v,vis,i-1,j);\n int r=check(v,vis,i,j+1);\n int s=check(v,vis,i,j-1);\n return p+q+r+s;\n \n }\n int islandPerimeter(vector<vector<int>>& v) {\n int n=v.size(),m=v[0].size();\n vector<vector<int>> vis(n,vector<int>(m,0));\n int c=0;\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<m;j++)\n {\n if(v[i][j] && vis[i][j]==0)\n {\n c=c+check(v,vis,i,j);\n }\n }\n }\n return c;\n }\n};",
"memory": "54032"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> v;\n vector<pair<int, int>> chk = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\n bool vis[500][500];\n int per = 0;\n int row, col;\n\n int islandPerimeter(vector<vector<int>>& grid) {\n v = grid;\n memset(vis, false, sizeof(vis));\n row = v.size();\n col = v[0].size();\n for(int i=0; i<v.size(); i++) {\n for(int j=0; j<v[i].size(); j++) {\n if(grid[i][j] == 1) {\n valid_cnt(i, j);\n }\n }\n }\n\n return per;\n }\n\n void valid_cnt(int src_i, int src_j) {\n\n for(auto it : chk) {\n int c_i = src_i + it.first;\n int c_j = src_j + it.second;\n\n if(c_i < 0 || c_j < 0 || c_i >= row || c_j >= col) {\n per++;\n } else {\n if(v[c_i][c_j] != 1) {\n per++;\n } \n }\n }\n }\n};",
"memory": "55427"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nint cnt=0;\n void dfs( int i,int j, vector<vector<int>>& grid, vector<vector< int>>& vis){\n int n= grid.size();\n int m= grid[0].size();\n if(i<0 || i>=n || j<0 || j>=m || grid[i][j]==0){cnt++; return ;}\n\n if(vis[i][j]==1) return;\n\n vis[i][j]=1;\n dfs( i-1,j, grid, vis);\n dfs( i+1, j, grid, vis);\n dfs(i, j+1, grid, vis);\n dfs( i, j-1, grid, vis);\n\n\n\n\n\n\n\n\n }\n \n int islandPerimeter(vector<vector<int>>& grid) {\n int n= grid.size();\n int m= grid[0].size();\n vector<vector<int>>vis( n, vector<int>( m,0));\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n if(vis[i][j]==0 && grid[i][j]==1){\n dfs( i,j, grid, vis);\n\n }\n\n\n }\n }\nreturn cnt;\n \n }\n};",
"memory": "56822"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int dx[4] = { 1,-1,0,0 };\n int dy[4] = { 0,0,1,-1 };\npublic:\n //创建dfs函数\n void dfs(vector<vector<int>>& grid, vector<vector<int>>& used, int& result, int row, int col) {\n //将当前岛屿标记为使用\n used[row][col] = 1;\n //初始化当前岛屿的周长\n int temp = 4;\n\n for (int i = 0; i < 4; i++) {\n int x = dx[i] + row;\n int y = dy[i] + col; \n if (x >= 0 && y >= 0 && x < grid.size() && y < grid[0].size()) {\n //如果有相邻岛屿,则让当前岛屿周长减一\n if (grid[x][y] == 1) {\n temp--;\n }\n //再dfs相邻岛屿\n if (grid[x][y] == 1 && used[x][y] == 0) {\n dfs(grid, used, result, x, y);\n }\n }\n }\n //最后让结果加上当前岛屿的周长\n result += temp;\n }\n\n int islandPerimeter(vector<vector<int>>& grid) {\n //标记哪些岛屿被使用过\n vector<vector<int>> used(grid.size(), vector<int>(grid[0].size(), 0));\n //定义结果\n int result = 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 //只用进入一块岛屿即可,因为所有的岛屿是连在一起的\n dfs(grid, used, result, i, j);\n return result;\n }\n }\n }\n\n return result;\n }\n};",
"memory": "58217"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n void dfs(int row, int col, int &ans, vector<vector<int>>& grid, vector<vector<int>>& vis) {\n int n = grid.size();\n int m = grid[0].size();\n vis[row][col] = 1; // Mark as visited\n\n int delrow[] = {0, 1, 0, -1};\n int delcol[] = {-1, 0, 1, 0};\n\n // Explore neighbors in 4 directions\n for (int i = 0; i < 4; i++) {\n int nrow = row + delrow[i];\n int ncol = col + delcol[i];\n\n // Check if the neighbor is within bounds\n if (nrow >= 0 && ncol >= 0 && nrow < n && ncol < m && grid[nrow][ncol] == 1) { \n if (!vis[nrow][ncol]) {\n dfs(nrow, ncol, ans, grid, vis);\n }\n }\n else{\n ans++;\n }\n }\n }\n\npublic:\n int islandPerimeter(vector<vector<int>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n vector<vector<int>> vis(n, vector<int>(m, 0)); // Visited array\n int ans = 0;\n\n // Loop through the grid to find the first land cell\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n if (grid[i][j] == 1 && !vis[i][j]) {\n // Start DFS from the first unvisited land cell\n dfs(i, j, ans, grid, vis);\n }\n }\n }\n return ans;\n }\n};\n",
"memory": "59612"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n void dfs(int row, int col, int &ans, vector<vector<int>>& grid, vector<vector<int>>& vis) {\n int n = grid.size();\n int m = grid[0].size();\n vis[row][col] = 1; // Mark as visited\n\n int delrow[] = {0, 1, 0, -1};\n int delcol[] = {-1, 0, 1, 0};\n\n // Explore neighbors in 4 directions\n for (int i = 0; i < 4; i++) {\n int nrow = row + delrow[i];\n int ncol = col + delcol[i];\n\n // Check if the neighbor is within bounds\n if (nrow >= 0 && ncol >= 0 && nrow < n && ncol < m && grid[nrow][ncol] == 1) { \n if (!vis[nrow][ncol]) {\n dfs(nrow, ncol, ans, grid, vis);\n }\n }\n else{\n ans++;\n }\n }\n }\n\npublic:\n int islandPerimeter(vector<vector<int>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n vector<vector<int>> vis(n, vector<int>(m, 0)); // Visited array\n int ans = 0;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n if (grid[i][j] == 1 && !vis[i][j]) {\n dfs(i, j, ans, grid, vis);\n }\n }\n }\n return ans;\n }\n};\n",
"memory": "61007"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int perimeter = 0;\n // vector<vector<bool>> vis(100 , vector<bool>(100 , false));\n vector<int> delrow = {-1, 0 ,1 , 0};\n vector<int> delcol = {0 , 1 , 0 , -1};\n\n void dfs(int row , int col , vector<vector<int>>& grid , vector<vector<bool>>& vis){\n vis[row][col] = true;\n\n for(int i = 0 ; i < 4 ; i++){\n int nrow = row + delrow[i]; \n int ncol = col + delcol[i];\n\n if(nrow >= 0 && ncol >= 0 && nrow < grid.size() && ncol < grid[0].size() && grid[nrow][ncol] == 1 && !vis[nrow][ncol]){\n dfs(nrow , ncol , grid , vis);\n }else if(!(nrow >= 0 && ncol >= 0 && nrow < grid.size() && ncol < grid[0].size())) perimeter++;\n else if(grid[nrow][ncol] == 0) perimeter++;\n }\n }\n\n int islandPerimeter(vector<vector<int>>& grid) {\n vector<vector<bool>> vis(grid.size() , vector<bool>(grid[0].size() , false));\n\n for(int i = 0 ; i < grid.size() ; i++){\n for(int j = 0 ; j < grid[0].size() ; j++){\n if(!vis[i][j] && grid[i][j] == 1){\n dfs(i , j , grid , vis);\n }\n }\n }\n\n return perimeter;\n }\n};",
"memory": "62402"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int perimeter = 0;\n // vector<vector<bool>> vis(100 , vector<bool>(100 , false));\n vector<int> delrow = {-1, 0 ,1 , 0};\n vector<int> delcol = {0 , 1 , 0 , -1};\n\n void dfs(int row , int col , vector<vector<int>>& grid , vector<vector<bool>>& vis){\n vis[row][col] = true;\n\n for(int i = 0 ; i < 4 ; i++){\n int nrow = row + delrow[i]; \n int ncol = col + delcol[i];\n\n if(nrow >= 0 && ncol >= 0 && nrow < grid.size() && ncol < grid[0].size() && grid[nrow][ncol] == 1 && !vis[nrow][ncol]){\n dfs(nrow , ncol , grid , vis);\n }else if(!(nrow >= 0 && ncol >= 0 && nrow < grid.size() && ncol < grid[0].size())) perimeter++;\n else if(grid[nrow][ncol] == 0) perimeter++;\n }\n }\n\n int islandPerimeter(vector<vector<int>>& grid) {\n vector<vector<bool>> vis(grid.size() , vector<bool>(grid[0].size() , false));\n\n for(int i = 0 ; i < grid.size() ; i++){\n for(int j = 0 ; j < grid[0].size() ; j++){\n if(!vis[i][j] && grid[i][j] == 1){\n dfs(i , j , grid , vis);\n }\n }\n }\n\n return perimeter;\n }\n};",
"memory": "62402"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\n vector<int>dx={0,1,0,-1,0};\npublic:\n bool isValid(int i,int j,int n,int m){\n return i>=0 && j>=0 && i<n && j<m;\n }\n int dfs(int i,int j,vector<vector<int>>& grid,vector<vector<int>>& vis,int n,int m){\n if(!isValid(i,j,n,m))return 0;\n if(vis[i][j] || grid[i][j]==0)return 0;\n vis[i][j]=1;\n int ans = 4;\n for(int k=0;k<4;k++){\n int x = i+dx[k];\n int y = j+dx[k+1];\n if(isValid(x,y,n,m)){\n ans-=(grid[x][y]==1);\n ans+=dfs(x,y,grid,vis,n,m);\n }\n }\n return ans;\n }\n int islandPerimeter(vector<vector<int>>& grid) {\n int ans = 0;\n int n = grid.size();\n int m = grid[0].size();\n vector<vector<int>>vis(n,vector<int>(m,0));\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n ans += dfs(i,j,grid,vis,n,m);\n }\n }\n return ans;\n }\n};",
"memory": "63797"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n void dfs(vector<vector<int>>& grid, vector<vector<int>>& visited, int row, int col,\n int delRow[], int delCol[], int& count) {\n visited[row][col] = 1;\n int n = grid.size();\n int m = grid[0].size();\n\n for (int i = 0; i < 4; i++) {\n int nrow = row + delRow[i];\n int ncol = col + delCol[i];\n\n if (nrow < 0 || nrow >= n || ncol < 0 || ncol >= m || grid[nrow][ncol] == 0) {\n count++;\n } \n else if (!visited[nrow][ncol] && grid[nrow][ncol] == 1) {\n dfs(grid, visited, nrow, ncol, delRow, delCol, count);\n }\n }\n }\n\npublic:\n int islandPerimeter(vector<vector<int>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n vector<vector<int>> visited(n, vector<int>(m, 0));\n\n int delRow[] = {-1, 0, 1, 0};\n int delCol[] = {0, 1, 0, -1};\n int count = 0;\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 dfs(grid, visited, i, j, delRow, delCol, count);\n return count;\n }\n }\n }\n return 0;\n }\n};",
"memory": "65192"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int n, m;\n bool valid(int ci, int cj, int n, int m) {\n if (ci < 0 || ci >= n || cj < 0 || cj >= m)\n return false;\n return true;\n }\n int islandPerimeter(vector<vector<int>>& grid) {\n int cnt = 0;\n int n = grid.size();\n int m = grid[0].size();\n bool vis[n][m];\n memset(vis, false, sizeof(vis));\n vector<pair<int, int>> d = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};\n queue<pair<int, int>> q;\n int si = -1, sj = -1;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n if (!vis[i][j] && grid[i][j] == 1) {\n si = i;\n sj = j;\n break;\n }\n }\n }\n q.push({si, sj});\n\n while (!q.empty()) {\n pair<int, int> p = q.front();\n int a = p.first;\n int b = p.second;\n q.pop();\n vis[a][b] = true;\n for (auto e : d) {\n int ci = a + e.first;\n int cj = b + e.second;\n if (valid(ci, cj,n,m)) {\n if (grid[ci][cj] == 0)\n cnt++;\n if (!vis[ci][cj] && grid[ci][cj] == 1) {\n q.push({ci, cj});\n vis[ci][cj] = true;\n }\n } else {\n cnt++;\n }\n }\n }\n return cnt;\n }\n};",
"memory": "66587"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int n, m;\n bool valid(int ci, int cj) {\n if (ci < 0 || ci >= n || cj < 0 || cj >= m)\n return false;\n return true;\n }\n int islandPerimeter(vector<vector<int>>& grid) {\n int cnt = 0;\n n = grid.size();\n m = grid[0].size();\n bool vis[n][m];\n memset(vis, false, sizeof(vis));\n vector<pair<int, int>> d = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};\n queue<pair<int, int>> q;\n int si = -1, sj = -1;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n if (!vis[i][j] && grid[i][j] == 1) {\n si = i;\n sj = j;\n break;\n }\n }\n }\n q.push({si, sj});\n\n while (!q.empty()) {\n pair<int, int> p = q.front();\n int a = p.first;\n int b = p.second;\n q.pop();\n vis[a][b] = true;\n for (auto e : d) {\n int ci = a + e.first;\n int cj = b + e.second;\n if (valid(ci, cj)) {\n if (grid[ci][cj] == 0)\n cnt++;\n if (!vis[ci][cj] && grid[ci][cj] == 1) {\n q.push({ci, cj});\n vis[ci][cj] = true;\n }\n } else {\n cnt++;\n }\n }\n }\n return cnt;\n }\n};",
"memory": "66587"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int p =0;\n void fun(vector<vector<int>>& grid,int i,int j,vector<vector<int>>& visited)\n {\n if(i<0 || j<0 || i>grid.size()-1 || j>grid[0].size()-1|| grid[i][j]==0)\n {\n p++;\n return;\n } \n if(visited[i][j]==1)\n return;\n \n visited[i][j]=1;\n fun(grid,i-1,j,visited);\n fun(grid,i,j-1,visited);\n fun(grid,i+1,j,visited);\n fun(grid,i,j+1,visited);\n }\n int islandPerimeter(vector<vector<int>>& grid) {\n int n=grid.size();\n int m=grid[0].size();\n vector<vector<int>> visited(n+1,vector<int>(m+1,-1));\n int perimeter=0;\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<m;j++)\n {\n if(grid[i][j]==1)\n fun(grid,i,j,visited);\n }\n }\n\n return p;\n }\n};",
"memory": "67982"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\n int visited[101][101] = {0};\n vector<vector<int>> g;\npublic:\n int per = 0;\n int X = 0, Y = 0;\n\n int getGrid(int x, int y)\n {\n if (x < 0 || y < 0 || x >= X || y>=Y )\n return 0;\n else\n return g[y][ x];\n }\n void search(int x, int y)\n {\n if ( x < 0 || y < 0 || y >= Y || x >= X)\n {\n //cout << \"\\t\" << x << \", \" << y << \"OOB\\n\";\n return;\n }\n\n \n if (getGrid(x, y) == 0)\n {\n //cout << \"\\t\" << x << \", \" << y << \" \" << g[y][x] << getGrid(x, y)<<\"Water\\n <<\";\n return;\n }\n \n else //we're in land\n {\n if (visited[y][x] == 1)\n return;\n ////cout << x << \" \" << y << \" \" << getGrid(y, x) << endl;\n visited[y][x] = 1;\n if (getGrid(x+1, y) == 0)\n {\n //cout << x <<\", \" << y << \"> \" << \"W\" << endl; \n per++;\n }\n else\n {\n //cout << x <<\", \" << y << \"> \" << \"L\" << endl;\n search(x+1, y);\n }\n\n if (getGrid(x-1, y) == 0)\n {\n //cout << x <<\", \" << y << \"< \" << \"W\" << endl;\n per++;\n }\n else\n {\n //cout << x <<\", \" << y << \"< \" << \"L\" << endl;\n search(x-1, y);\n }\n if (getGrid(x, y+1) == 0)\n {\n //cout << x <<\", \" << y << \"v \" << \"W\" << endl;\n per++;\n }\n else\n {\n //cout << x <<\", \" << y << \"v \" << \"L\" << endl;\n search(x, y+1);\n }\n if (getGrid(x, y-1) == 0)\n {\n //cout << x <<\", \" << y << \"^ \" << \"W\" << endl;\n per++;\n }\n else\n {\n //cout << x <<\", \" << y << \"^ \" << \"L\" << endl;\n search(x, y-1);\n }\n \n }\n }\n\n int islandPerimeter(vector<vector<int>>& grid) {\n Y = grid.size();\n X = grid[0].size();\n g = grid;\n //cout << \"X: \" << X << \"Y: \" << Y << endl;\n\n for (size_t i = 0; i < Y; i++)\n for (size_t j = 0; j < X; j++)\n if (grid[i][j] == 1)\n {\n //cout << \"Start: \" << j << \" \" << i << endl;\n search(j, i);\n break;\n }\n return per;\n }\n};\n",
"memory": "69377"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int islandPerimeter(vector<vector<int>>& grid) {\n // find the land by iterating across the grid until finding a 1\n // Add the first piece of land on to a queue\n // Track the visited nodes by turning visited nodes to 2\n\n\n // Find the land\n pair<int, int> initial_land;\n bool found_land = false;\n for (int i = 0; i < grid.size() && !found_land; i++) {\n for (int j = 0; j < grid[i].size(); j++) {\n if (grid[i][j] == 1) {\n initial_land.first = i;\n initial_land.second = j;\n found_land = true;\n break;\n }\n }\n }\n\n queue<pair<int, int>> discovered;\n discovered.push(initial_land);\n int perimeter = 0;\n\n while (!discovered.empty()) {\n pair<int, int> land = discovered.front();\n discovered.pop();\n if (grid[land.first][land.second] == 2){\n continue;\n }\n\n // Check land's border for neighoring land cells and water that can be added to the perimeter\n\n // row - 1\n if (land.first - 1 < 0 || grid[land.first - 1][land.second] == 0) {\n // if edge or water\n perimeter++;\n } else if (grid[land.first - 1][land.second] == 1) {\n // if land\n discovered.push(pair<int, int>(land.first - 1, land.second));\n }\n // row + 1\n if (land.first + 1 >= grid.size() || grid[land.first + 1][land.second] == 0) {\n // if edge or water\n perimeter++;\n } else if (grid[land.first + 1][land.second] == 1) {\n // if land\n discovered.push(pair<int, int>(land.first + 1, land.second));\n }\n // col - 1\n if (land.second - 1 < 0 || grid[land.first][land.second - 1] == 0) {\n // if edge or water\n perimeter++;\n } else if (grid[land.first][land.second - 1] == 1) {\n // if land\n discovered.push(pair<int, int>(land.first, land.second - 1));\n }\n // col + 1\n if (land.second + 1 >= grid[land.first].size() || grid[land.first][land.second + 1] == 0) {\n // if edge or water\n perimeter++;\n } else if (grid[land.first][land.second + 1] == 1) {\n // if land\n discovered.push(pair<int, int>(land.first, land.second + 1));\n }\n\n\n // Mark as visited\n grid[land.first][land.second] = 2;\n }\n\n return perimeter;\n }\n};",
"memory": "70772"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> gGrid;\n bool visited[101][101];\n int dr[4] = {1, 0, -1, 0};\n int dc[4] = {0, 1, 0, -1};\n bool isValid(int r, int c) {\n if (r < 0 || r >= gGrid.size() || c < 0 || c >= gGrid[r].size()) return false;\n return true;\n }\n\n int solve(int r, int c) {\n int ret = 0;\n\n visited[r][c] = true;\n for (int i = 0; i < 4; i++) {\n int nr = r + dr[i];\n int nc = c + dc[i];\n if (isValid(nr, nc) && gGrid[nr][nc] == 1) {\n if (!visited[nr][nc]) {\n ret += solve(nr, nc);\n }\n } else {\n ret++;\n }\n }\n\n return ret;\n }\n int islandPerimeter(vector<vector<int>>& grid) {\n gGrid = grid;\n int ret = 0;\n bool f = false;\n for (int i = 0; !f && i < gGrid.size(); i++) {\n for (int j = 0; j < gGrid[i].size(); j++) {\n if (gGrid[i][j] == 1) {\n f = true;\n ret = solve(i, j);\n break;\n }\n }\n }\n return ret;\n }\n};",
"memory": "72167"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "int dir[][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};\n\nclass Solution {\npublic:\n int expand(queue<pair<int, int>>& bfs, vector<vector<int>>& grid) {\n int perimeter = 0;\n while (!bfs.empty()) {\n auto cur = bfs.front();\n bfs.pop();\n\n if (grid[cur.first][cur.second] == 2) continue;\n grid[cur.first][cur.second] = 2;\n\n for (int i = 0; i < 4; i++) {\n int r = cur.first + dir[i][0];\n int c = cur.second + dir[i][1];\n if (r < 0 || r >= grid.size() || c < 0 || c >= grid[0].size() || grid[r][c] == 0) {\n perimeter++;\n continue;\n }\n if (grid[r][c] == 1) {\n bfs.push({r, c});\n }\n }\n }\n return perimeter;\n }\n\n int islandPerimeter(vector<vector<int>>& grid) {\n for (int r = 0; r < grid.size(); r++) {\n for (int c = 0; c < grid[0].size(); c++) {\n if (grid[r][c] == 0) continue;\n queue<pair<int, int>> bfs;\n bfs.push({r, c});\n return expand(bfs, grid);\n }\n }\n return 0;\n }\n};",
"memory": "73562"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int islandPerimeter(vector<vector<int>>& grid) {\n int rows = grid.size();\n int cols = grid[0].size();\n int ans = 0;\n queue<pair<int, int>> q;\n\n for (int i = 0; i < rows; i++) {\n for (int j = 0; j < cols; j++) {\n if (grid[i][j] == 1) {\n q.push({i, j});\n break;\n }\n }\n if (q.size() > 0) {\n break;\n }\n }\n vector<pair<int, int>> directions = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};\n while (q.size() > 0) {\n auto [row, col] = q.front();\n q.pop();\n if (grid[row][col] == 1) {\n grid[row][col] = -1;\n for (auto [dr, dc] : directions) {\n int newRow = row + dr;\n int newCol = col + dc;\n if (newRow == -1 || newCol == -1 || newRow >= rows ||\n newCol >= cols || grid[newRow][newCol] == 0) {\n ans++;\n } else if (grid[newRow][newCol] == 1) {\n q.push({newRow, newCol});\n }\n }\n }\n }\n\n return ans;\n }\n};",
"memory": "74957"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nvector<vector<int>> df{{1,0},{-1,0},{0,1},{0,-1}};\n int dfs(vector<vector<int>>& grid, vector<vector<bool>>& vis, int x, int y){\n if(x<0 || x>=grid.size() || y<0 || y>=grid[0].size()){\n return 1;\n }\n if(grid[x][y] == 0){\n return 1;\n }\n if(vis[x][y]){\n return 0;\n }\n vis[x][y]=true;\n int sum =0;\n for(int i=0;i<4;i++){\n int nx=x+df[i][0];\n int ny=y+df[i][1];\n sum += dfs(grid,vis,nx,ny);\n }\n return sum;\n }\n int islandPerimeter(vector<vector<int>>& grid) {\n vector<vector<bool>> vis(grid.size(),vector<bool>(grid[0].size(),false));\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 {\n return dfs(grid, vis, i, j);\n }\n }\n }\n return 0;\n }\n};",
"memory": "80537"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "\n\nclass Solution {\npublic:\n int vis[1005][1005];\n int ans = 0;\n vector<pair<int, int>> d = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};\n\n int n, m;\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 void dfs(int si, int sj, vector<vector<int>>& grid) {\n vis[si][sj] = true;\n\n for (int i = 0; i < 4; i++) {\n int ci = si + d[i].first;\n int cj = sj + d[i].second;\n if (valid(ci, cj)) {\n if (grid[ci][cj] == 0)\n ans++;\n } else\n ans++;\n\n if (valid(ci, cj) && !vis[ci][cj] && grid[ci][cj] == 1) {\n dfs(ci, cj, grid);\n }\n }\n };\n int islandPerimeter(vector<vector<int>>& grid) {\n memset(vis, false, sizeof(vis));\n n = grid.size();\n m = grid[0].size();\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 dfs(i, j, grid);\n }\n }\n }\n\n return ans;\n }\n};",
"memory": "80537"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool vis[101][101];\n int n, m;\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 }\n int ans = 0;\n void bfs(int si, int sj, vector<vector<int>> &grid) {\n vis[si][sj] = true;\n queue<pair<int, int>> q;\n q.push({si, sj});\n while(!q.empty()) {\n pair<int, int> p = q.front();\n int a = p.first;\n int b = p.second;\n q.pop();\n for(int i = 0; i < 4; i++) {\n int ci = a + d[i].first;\n int cj = b + d[i].second;\n if(valid(ci, cj) == true) {\n if(grid[ci][cj] == 0) {\n ans++;\n }\n } else {\n ans++;\n }\n if(valid(ci, cj) == true && vis[ci][cj] == false && grid[ci][cj] == 1) {\n vis[ci][cj] = true;\n q.push({ci, cj});\n }\n }\n }\n }\n // void dfs(int si, int sj, vector<vector<int>> &grid) {\n // vis[si][sj] = true;\n // for(int i = 0; i < 4; i++) {\n // int ci = si+d[i].first;\n // int cj = sj+d[i].second;\n // if(valid(ci, cj) == true) {\n // if(grid[ci][cj] == 0) {\n // ans++;\n // }\n // } else {\n // ans++;\n // }\n // if(valid(ci, cj) == true && vis[ci][cj] == false && grid[ci][cj] == 1) {\n // dfs(ci, cj, grid);\n // }\n // }\n // }\n int islandPerimeter(vector<vector<int>>& grid) {\n memset(vis, false, sizeof(vis));\n n = grid.size();\n m = grid[0].size();\n for(int i = 0; i < n; i++) {\n for(int j = 0; j < m; j++) {\n if(vis[i][j] == false && grid[i][j] == 1) {\n bfs(i, j, grid);\n }\n }\n }\n return ans;\n }\n};",
"memory": "81932"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int ans = 0;\n bool vstd[105][105];\n\n vector<pair<int,int>> pth = {{1,0},{-1,0},{0,1},{0,-1}};\n \n bool isValid(int l , int k , int n , int m){\n if(l>=0 && l<n && k>=0 && k<m && vstd[l][k] == false) {\n return true;\n }\n return false;\n }\n\n void DFS(vector<vector<int>>& grid , int si , int sj , int n , int m){\n queue<pair<int,int>> qu;\n qu.push({si,sj});\n vstd[si][sj] = true;\n \n while(!qu.empty()){\n pair<int,int> a = qu.front();\n int ai = a.first;\n int aj = a.second;\n qu.pop();\n\n\n for(auto crnt: pth){\n int ni = ai + crnt.first;\n int nj = aj + crnt.second;\n\n if(ni < 0 || ni >= n || nj < 0 || nj >= m || grid[ni][nj] == 0){\n ans++;\n }\n \n if(isValid(ni,nj,n,m) && grid[ni][nj] == 1){\n vstd[ni][nj] = true;\n qu.push({ni,nj});\n }\n }\n\n }\n }\n\n int islandPerimeter(vector<vector<int>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n\n int i , j;\n\n for(i=0 ; i<n ; i++){\n for(j=0 ; j<m ; j++){\n if(grid[i][j] == 1 && vstd[i][j] == false){\n DFS(grid,i,j,n,m);\n break;\n }\n }\n }\n\n return ans;\n }\n};",
"memory": "81932"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool vis[105][105];\n int ans;\n int r, c;\n vector<pair<int, int>> v = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};\n bool valid(int ci, int cj){\n if(ci >= 0 && ci < r && cj >= 0 && cj < c){\n return true;\n }\n return false;\n }\n void bfs(int si, int sj, vector<vector<int>>& grid){\n queue<pair<int, int>> q;\n q.push({si, sj});\n vis[si][sj] = true;\n while(!q.empty()){\n pair<int, int> par = q.front();\n q.pop();\n for(int i = 0; i < 4; i++){\n int ci = par.first + v[i].first;\n int cj = par.second + v[i].second;\n if(valid(ci, cj)){\n if(grid[ci][cj] == 0) ans++;\n } else {\n ans++;\n }\n if(valid(ci, cj) && !vis[ci][cj] && grid[ci][cj] == 1){\n vis[ci][cj] = true;\n q.push({ci, cj});\n }\n }\n }\n }\n int islandPerimeter(vector<vector<int>>& grid) {\n ans = 0;\n r = grid.size();\n c = grid[0].size();\n for(int i = 0; i < grid.size(); i++){\n for(int j = 0; j < grid[0].size(); j++){\n if(!vis[i][j] && grid[i][j] == 1){\n bfs(i, j, grid);\n }\n }\n }\n return ans;\n }\n};",
"memory": "83327"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool visited[105][105];\n vector<int> d[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};\n bool isValid(int i, int j, int row, int col) {\n if (i < 0 || i >= row || j < 0 || j >= col) {\n return false;\n } else {\n return true;\n }\n }\n int islandPerimeter(vector<vector<int>>& grid) {\n int row = grid.size();\n int col = grid[0].size();\n int si=-1;\n int sj=-1;\n for (int i = 0; i < row; i++) {\n for (int j = 0; j < col; j++) {\n \n if (grid[i][j] == 1) {\n si = i;\n sj = j;\n break;\n }\n if(si !=-1) \n {\n break;\n }\n }\n }\n memset(visited, false, sizeof(visited));\n int count = 0;\n queue<pair<int, int>> q;\n q.push({si, sj});\n visited[si][sj] = true;\n while (!q.empty()) {\n pair<int, int> par = q.front();\n q.pop();\n int si = par.first;\n int sj = par.second;\n for (int i = 0; i < 4; i++) {\n int ci = si + d[i][0];\n int cj = sj + d[i][1];\n if (isValid(ci, cj, row, col) && !visited[ci][cj] && grid[ci][cj]) {\n q.push({ci, cj});\n visited[ci][cj] = true;\n }\n if (!isValid(ci, cj, row, col)) {\n count++;\n }\n else if (grid[ci][cj] == 0) {\n count++;\n }\n }\n }\n return count;\n }\n};",
"memory": "84722"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int parameter;\n vector<pair<int,int>> d = {{1,0},{-1,0},{0,1},{0,-1}};\n\n bool valid(int i, int j, int n, int m){\n if(i<0 || i>=n || j<0 || j>=m) return false;\n return true;\n }\n\n void dfs(vector<vector<int>>& grid, vector<vector<bool>> &visited,int n, int m, int i, int j){\n visited[i][j] = true;\n if(grid[i][j] == 1){\n parameter += 4;\n for(pair<int,int> c:d){\n int ci = c.first+i;\n int cj = c.second+j;\n if(valid(ci,cj,n,m) && grid[ci][cj] == 1 && visited[ci][cj]){\n parameter-=2;\n };\n }\n }\n for(pair<int,int> c:d){\n int ci = c.first+i;\n int cj = c.second+j;\n if(valid(ci,cj,n,m) && !visited[ci][cj]){\n dfs(grid,visited,n,m,ci,cj);\n }\n }\n }\n\n int islandPerimeter(vector<vector<int>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n vector<vector<bool>> visited(n, vector<bool> (m,false));\n parameter = 0;\n dfs(grid,visited,n,m,0,0);\n return parameter;\n }\n};",
"memory": "86117"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "#include <vector>\n#include <queue>\n#include <utility>\n\nusing namespace std;\n\nclass Solution {\npublic:\n // Function to check if the cell is valid (within grid bounds)\n bool isvalid(int new_i, int new_j, int m ,int n){\n return new_i >= 0 && new_i < m && new_j >= 0 && new_j < n;\n }\n \n int islandPerimeter(vector<vector<int>>& grid) {\n int m = grid.size(); // Number of rows\n int n = grid[0].size(); // Number of columns\n int perimeter = 0; // Initialize perimeter\n \n // Direction vectors for up, right, down, left\n int r[] = {-1, 0, 1, 0}; // Row changes\n int c[] = {0, 1, 0, -1}; // Column changes\n \n // BFS initialization\n queue<pair<int, int>> q;\n vector<vector<bool>> visited(m, vector<bool>(n, false));\n \n // Find the first land cell (1) to start BFS\n for (int i = 0; i < m; i++) {\n bool found = false;\n for (int j = 0; j < n; j++) {\n if (grid[i][j] == 1) {\n q.push({i, j});\n visited[i][j] = true;\n found = true;\n break;\n }\n }\n if (found) break;\n }\n \n // Perform BFS\n while (!q.empty()) {\n int x = q.front().first;\n int y = q.front().second;\n q.pop();\n \n // Check the four possible directions (up, right, down, left)\n for (int k = 0; k < 4; k++) {\n int new_x = x + r[k];\n int new_y = y + c[k];\n \n // If the neighboring cell is out of bounds or water, it contributes to the perimeter\n if (!isvalid(new_x, new_y, m, n) || grid[new_x][new_y] == 0) {\n perimeter++;\n } \n // If the neighboring cell is land and not visited, add it to the BFS queue\n else if (grid[new_x][new_y] == 1 && !visited[new_x][new_y]) {\n visited[new_x][new_y] = true;\n q.push({new_x, new_y});\n }\n }\n }\n \n return perimeter;\n }\n};\n",
"memory": "87512"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\n vector<vector<int>> grid;\n int n,m;\n int dfs(int i, int j, vector<vector<bool>> &vis) {\n if(i<0 || j<0 || i>=n || j>=m || grid[i][j] == 0)\n return 1;\n\n if(vis[i][j]) return 0;\n\n vis[i][j] = 1;\n\n int perimeter = 0;\n\n perimeter += dfs(i+1,j,vis);\n perimeter += dfs(i,j+1,vis);\n perimeter += dfs(i-1,j,vis);\n perimeter += dfs(i,j-1,vis);\n\n return perimeter;\n }\npublic:\n int islandPerimeter(vector<vector<int>>& g) {\n this->grid = g;\n this->n = size(grid);\n this->m = size(grid[0]);\n\n cout << n << \" \" << m;\n\n vector<vector<bool>> vis(n, vector<bool>(m,0));\n\n for(int i=0 ; i<n ; i++) {\n for(int j=0 ; j<m ; j++) {\n if(grid[i][j])\n return dfs(i,j,vis);\n }\n }\n return -1;\n }\n\n};",
"memory": "88907"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int solve(vector<vector<int>>&grid,vector<vector<bool>>&vis,int sr,int sc)\n {\n int n = grid.size();\n int m = grid[0].size();\n\n queue<pair<int,int>>q;\n q.push({sr,sc});\n vis[sr][sc] = true;\n int peri = 0;\n\n int delRow[] = {-1,1,0,0};\n int delCol[] = {0,0,-1,1};\n\n while( !q.empty()){\n auto it = q.front();\n q.pop();\n \n int r = it.first;\n int c = it.second;\n\n for(int i=0;i<4;i++){\n int nr = r + delRow[i];\n int nc = c + delCol[i];\n\n if(nr>=0 && nr<n && nc>=0 && nc<m && grid[nr][nc] == 0)peri++;\n else if((nr < 0 || nr >= n || nc < 0 || nc >= m)) peri++;\n\n if(nr>=0 && nr<n && nc>=0 && nc<m && grid[nr][nc] == 1 && !vis[nr][nc]){\n vis[nr][nc] = true;\n q.push({nr,nc});\n }\n }\n }\n return peri;\n }\n int islandPerimeter(vector<vector<int>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n\n vector<vector<bool>>vis(n,vector<bool>(m));\n int res = 0;\n for(int i=0;i<n;i++)\n for(int j=0;j<m;j++)\n if(vis[i][j] == false && grid[i][j] == 1){\n vis[i][j] = true;\n res += solve(grid,vis,i,j);\n }\n return res;\n \n }\n};",
"memory": "90302"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n vector<int> stepCol = {1, 0, -1, 0};\n vector<int> stepRow = {0, 1, 0, -1};\n bool valid(int row, int col, auto& grid) {\n return (row >=0 && row < grid.size() && col >=0 && col < grid[0].size());\n }\npublic:\n int islandPerimeter(vector<vector<int>>& grid) {\n queue<pair<int, int>> q;\n int result = 0;\n int nRow = grid.size();\n int nCol = grid[0].size();\n vector<vector<bool>> visited(nRow, vector<bool>(nCol, false));\n // find first island loc.\n for (int i = 0; i < nRow; i++) {\n for (int j = 0; j < nCol; j++) {\n if (grid[i][j] == 1) {\n q.push({i, j});\n visited[i][j] = true;\n break;\n } \n }\n if (!q.empty()) break;\n }\n while(!q.empty()) {\n pair<int, int> apair = q.front(); q.pop();\n int curRow = apair.first;\n int curCol = apair.second;\n for (int i = 0; i < 4; i++) {\n int nextRow = curRow + stepRow[i];\n int nextCol = curCol + stepCol[i];\n if (!valid(nextRow, nextCol, grid) || grid[nextRow][nextCol] == 0) {\n result++;\n }\n if (valid(nextRow, nextCol, grid)\n && visited[nextRow][nextCol] == false\n && grid[nextRow][nextCol] == 1) {\n q.push({nextRow, nextCol});\n visited[nextRow][nextCol] = true;\n }\n }\n }\n return result;\n }\n};",
"memory": "91697"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\n vector<vector<int>> grid;\n int n,m;\n int dfs(int i, int j, vector<vector<bool>> &vis) {\n if(i<0 || j<0 || i>=n || j>=m || grid[i][j] == 0)\n return 1;\n\n if(vis[i][j]) return 0;\n\n vis[i][j] = 1;\n\n int perimeter = 0;\n\n perimeter += dfs(i+1,j,vis);\n perimeter += dfs(i,j+1,vis);\n perimeter += dfs(i-1,j,vis);\n perimeter += dfs(i,j-1,vis);\n\n return perimeter;\n }\npublic:\n int islandPerimeter(vector<vector<int>>& g) {\n this->grid = g;\n this->n = size(grid);\n this->m = size(grid[0]);\n\n cout << n << \" \" << m;\n\n vector<vector<bool>> vis(n, vector<bool>(m,0));\n\n for(int i=0 ; i<n ; i++) {\n for(int j=0 ; j<m ; j++) {\n if(grid[i][j])\n return dfs(i,j,vis);\n }\n }\n return -1;\n }\n};",
"memory": "93092"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int m;\n int n;\n vector<vector<int>> direction{{1,0},{-1,0}, {0,1},{0,-1}};\n int bfs(vector<vector<int>>& grid, int i, int j) {\n int perim =0;\n queue<pair<int,int>>q;\n q.push({i,j});\n grid[i][j] =-1;\n while(!q.empty()){\n auto it = q.front();\n q.pop();\n for(auto &dir:direction){\n int i_ = it.first + dir[0]; \n int j_ = it.second + dir[1];\n\n if (i_ < 0 || i_ >= m || j_ < 0 || j_ >= n || grid[i_][j_] == 0) {\n perim++;\n }\n else if(grid[i_][j_]==-1){\n continue;\n }else{\n q.push({i_,j_});\n grid[i_][j_]=-1;\n }\n }\n }\n return perim;\n }\n\n int islandPerimeter(vector<vector<int>>& grid) {\n m = grid.size(); \n 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] == 1) {\n return bfs(grid, i, j); \n }\n }\n }\n return -1; \n }\n};\n",
"memory": "94487"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> directions{{1,0}, {-1,0}, {0,1}, {0,-1}};\n \n int BFS(vector<vector<int>>& grid, int i, int j, int m, int n, int& peri){\n \n queue<pair<int,int>> que;\n que.push({i,j});\n grid[i][j] = -1;\n \n while(!que.empty()){\n \n auto it = que.front();\n que.pop();\n \n for(auto& dir : directions){\n \n int i_ = it.first + dir[0];\n int j_ = it.second + dir[1];\n \n if(i_ < 0 || i_ >= m || j_ < 0 || j_ >= n || grid[i_][j_] == 0){\n \n peri++;\n }\n else if(grid[i_][j_] == -1){\n continue;\n }\n else{\n que.push({i_, j_});\n grid[i_][j_] = -1;\n }\n }\n }\n return peri;\n }\n \n int islandPerimeter(vector<vector<int>>& grid) {\n \n int m = grid.size();\n int n = grid[0].size();\n int peri = 0;\n \n for(int i = 0 ; i < m ; i++){\n for(int j = 0 ; j < n ; j++){\n \n if(grid[i][j] == 1){\n return BFS(grid, i, j, m, n, peri);\n }\n }\n }\n return -1;\n }\n};",
"memory": "95882"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int m;\n int n;\n int perimeter;\n //bfs\n vector<vector<int>> directions {{1,0},{-1,0},{0,1},{0,-1}};\n\n int bfs(vector<vector<int>>& grid, int i, int j){\n //use a queue in bfs to store and process the encountered edges\n queue<pair<int,int>> que;\n que.push({i,j});\n grid[i][j]=-1;//visited\n\n while(!que.empty()){\n auto it=que.front();\n que.pop();\n //so it.first=i and it.second=j\n\n for(auto& dir: directions){\n int i_=it.first+dir[0];\n int j_=it.second+dir[1];\n\n if(i_<0 || i_>=m || j_<0 || j_>=n || grid[i_][j_]==0){\n perimeter++;\n }\n else if(grid[i_][j_]==-1){\n\n }\n else{//grid[i_][j_]==1\n que.push({i_,j_});\n grid[i_][j_]=-1;//imp !! \n }\n }\n\n \n }\n return perimeter;\n }\n int islandPerimeter(vector<vector<int>>& grid) {\n //best problem to get started and practice regularly for dfs and bfs !\n m=grid.size();\n n=grid[0].size();\n perimeter=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 return bfs(grid,i,j);\n }\n \n }\n }\n return -1;\n }\n};",
"memory": "97277"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool vis[105][105];\n vector<pair<int,int> >d={{0,1},{0,-1},{-1,0},{1,0}};\n int n,m;\n int prt=0;\n bool valid(int i,int j)\n {\n if(i<0 || i>=n || j<0 || j>=m)\n {\n return false;\n }\n return true;\n }\n void bfs(int si,int sj,vector<vector<int>>& grid)\n {\n queue<pair<int,int>>q;\n q.push({si,sj});\n vis[si][sj]=true;\n while(!q.empty())\n {\n pair<int,int>par = q.front();\n int a=par.first,b=par.second;\n q.pop();\n for(int i=0;i<4;++i)\n {\n int ci = a+d[i].first;\n int cj = b+d[i].second;\n if(valid(ci,cj)==true && vis[ci][cj]==false)\n {\n q.push({ci,cj});\n vis[ci][cj]=true;\n }\n if(valid(ci,cj)==true && grid[a][b]==1 && grid[ci][cj]==0)\n {\n ++prt;\n }\n if(grid[a][b]==1 && valid(ci,cj)==false)\n {\n ++prt;\n }\n }\n }\n }\n int islandPerimeter(vector<vector<int>>& grid) {\n memset(vis,false,sizeof(vis));\n n=grid.size();\n m=grid[0].size();\n bfs(0,0,grid);\n return prt;\n }\n};",
"memory": "97277"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\n int bfs(int i, int j, vector<vector<int>>& grid, vector<vector<int>>& vis){\n queue<pair<int, int>> q;\n q.push({i, j});\n vis[i][j] = 1;\n\n int n = grid.size(), m = grid[0].size(), ans = 0;\n\n int rc[] = {-1, 0, 1, 0};\n int cc[] = {0, 1, 0, -1};\n\n while(!q.empty()){\n int row = q.front().first;\n int col = q.front().second;\n q.pop();\n\n for(int k = 0; k < 4; k++){\n int newRow = row + rc[k];\n int newCol = col + cc[k];\n\n if(min(newRow, newCol) >= 0 && newRow < n && newCol < m && grid[newRow][newCol] == 1){\n if(!vis[newRow][newCol]){\n q.push({newRow, newCol});\n vis[newRow][newCol] = 1;\n }\n }\n else ans++;\n }\n }\n\n return ans;\n }\npublic:\n int islandPerimeter(vector<vector<int>>& grid) {\n int n = grid.size(), m = grid[0].size(), ans = 0;\n vector<vector<int>> vis(n, vector<int>(m, 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 return bfs(i, j, grid, vis);\n }\n }\n }\n\n return 0;\n }\n};",
"memory": "98672"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int n, m;\n\n int row[4] = {0, 0, -1, 1};\n int col[4] = {-1, 1, 0, 0};\n\n bool valid(int i, int j) {\n return i >= 0 && j >= 0 && i < n && j < m;\n }\n\n int islandPerimeter(vector<vector<int>>& grid) {\n n = grid.size();\n m = grid[0].size();\n\n queue<pair<int, int>> q;\n int ans = 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 q.push({i, j});\n break;\n }\n }\n if (!q.empty()) break; \n }\n\n vector<vector<bool>> visited(n, vector<bool>(m, false));\n\n while (!q.empty()) {\n int r = q.front().first;\n int c = q.front().second;\n q.pop();\n\n if (visited[r][c]) continue;\n visited[r][c] = true;\n\n for (int k = 0; k < 4; k++) {\n int new_r = r + row[k];\n int new_c = c + col[k];\n\n if (!valid(new_r, new_c) || grid[new_r][new_c] == 0) {\n ans++; \n } else if (!visited[new_r][new_c]) {\n q.push({new_r, new_c});\n }\n }\n }\n\n return ans;\n }\n};\n",
"memory": "100067"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int islandPerimeter(vector<vector<int>>& grid) {\n int cnt=0;\n int x[]={-1,0,1,0};\n int y[]={0,1,0,-1};\n queue<pair<int,int>>q;\n int n=grid.size();\n int m=grid[0].size();\n vector<vector<int>>vis(n,vector<int>(m,0));\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n if(grid[i][j]==1 && !vis[i][j]){\n vis[i][j]=1;\n q.push({i,j});\n\n \n \n \n\n \n\n while(!q.empty()){\n int r =q.front().first;\n int c=q.front().second;\n q.pop();\n \n for(int i=0;i<4;i++){\n int nrow=r+x[i];\n int ncol=c+y[i];\n\n if (nrow < 0 || nrow >= n || ncol < 0 || ncol >= m || grid[nrow][ncol] ==0) { \n cnt++; \n } \n else if( vis[nrow][ncol]==0 && grid[nrow][ncol]==1 ){\n \n vis[nrow][ncol]=1;\n q.push({nrow,ncol});\n }\n\n }\n }\n } \n } \n }\n return cnt;\n }\n\n};",
"memory": "101462"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calc(int row,int col,vector<vector<int>>& grid){\n int total=4;\n \n int n = grid.size();\n int m = grid[0].size();\n if(row>=0 && row<n && (col-1) >= 0 && col < m && grid[row][col-1]==1)\n total--;\n if((row-1)>=0 && row<n && col >= 0 && col < m && grid[row-1][col]==1)\n total--;\n if(row>=0 && row<n && col >= 0 && (col+1) < m && grid[row][col+1]==1)\n total--;\n if(row>=0 && (row+1)<n && col >= 0 && col < m && grid[row+1][col]==1)\n total--;\n // cout<<row<<\" \"<<col<<\" total=\"<<total<<endl;\n return total;\n }\n void bfs(int row, int col, vector<vector<int>>& vis,\n vector<vector<int>>& grid, int& ans) {\n vis[row][col] = 1;\n queue<pair<int, int>> q;\n int n = grid.size();\n int m = grid[0].size();\n ans+=calc(row,col,grid);\n q.push({row, col});\n int delRow[] = {-1, 0, +1, 0};\n int delCol[] = {0, +1, 0, -1};\n while (!q.empty()) {\n int nrow = q.front().first;\n int ncol = q.front().second;\n q.pop();\n for (int i = 0; i < 4; i++) {\n int grow = nrow + delRow[i];\n int gcol = ncol + delCol[i];\n if (grow >= 0 && grow < n && gcol >= 0 && gcol < m && grid[grow][gcol]==1 &&\n !vis[grow][gcol]) {\n ans+=calc(grow,gcol,grid);\n vis[grow][gcol] = 1;\n q.push({grow, gcol});\n }\n }\n }\n }\n int islandPerimeter(vector<vector<int>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n int ans = 0;\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 (!vis[row][col] && grid[row][col] == 1) {\n bfs(row, col, vis, grid, ans);\n }\n }\n }\n return ans;\n }\n};",
"memory": "102857"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int dx[4] = {-1, 0, 1, 0};\n int dy[4] = {0, -1, 0, 1};\n int islandPerimeter(vector<vector<int>> & grid) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n int n = grid.size(), m = grid[0].size();\n vector<vector<int>> vis(n, vector<int>(m));\n int per = 0;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n if (!vis[i][j] and grid[i][j]) {\n queue<pair<int, int>> q;\n q.push({i, j});\n while (!q.empty()) {\n auto [x, y] = q.front();\n vis[x][y]=1;\n q.pop();\n int ans = 0;\n for (int k = 0; k < 4; k++) {\n int nx = x + dx[k];\n int ny = y + dy[k];\n if (nx >= 0 and nx < n and ny >= 0 and ny < m and\n !vis[nx][ny] and grid[nx][ny]) {\n vis[nx][ny] = 1;\n q.push({nx, ny});\n } \n else if((nx < 0 or nx >= n or ny < 0 or ny >= m) or !vis[nx][ny])ans++;\n }\n per += ans;\n }\n }\n }\n }\n return per;\n }\n};",
"memory": "104252"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int islandPerimeter(vector<vector<int>>& grid) {\n int m = grid.size(), n = grid[0].size();\n\n // Find first island tile\n int i = 0, j = 0;\n while(i < m) {\n if(grid[i][j])\n break;\n \n j++;\n if(j == n) {\n j = 0;\n i++;\n }\n }\n\n // No island found\n if(i == m)\n return 0;\n \n queue<pair<int, int>> toVisit;\n vector<vector<bool>> visited(m, vector<bool>(n, false));\n visited[i][j] = true;\n toVisit.push({i, j});\n int perimeter = 0;\n while(!toVisit.empty()) {\n auto [i, j] = toVisit.front();\n toVisit.pop();\n// cout << \"On [\" << i << \", \" << j << \"]... \";\n\n // Perimeter is the number of sides that are water\n if(i == 0 || !grid[i-1][j]) { // up\n perimeter++;\n// cout << \"up \";\n }\n if(j == 0 || !grid[i][j-1]) { // left\n perimeter++;\n// cout << \"left \";\n }\n if(i == m-1 || !grid[i+1][j]) { // down\n perimeter++;\n// cout << \"down \";\n }\n if(j == n-1 || !grid[i][j+1]) { // right\n perimeter++;\n// cout << \"right \";\n }\n// cout << endl;\n \n // Add surrounding unvisted nodes\n if(i > 0 && grid[i-1][j] && !visited[i-1][j]) { // up\n toVisit.push({i-1, j});\n visited[i-1][j] = true;\n }\n if(j > 0 && grid[i][j-1] && !visited[i][j-1]) { // left\n toVisit.push({i, j-1});\n visited[i][j-1] = true;\n }\n if(i < m-1 && grid[i+1][j] && !visited[i+1][j]) { // down\n toVisit.push({i+1, j});\n visited[i+1][j] = true;\n }\n if(j < n-1 && grid[i][j+1] && !visited[i][j+1]) { // right\n toVisit.push({i, j+1});\n visited[i][j+1] = true;\n }\n }\n\n return perimeter;\n }\n};",
"memory": "105647"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int islandPerimeter(vector<vector<int>>& grid) {\n\n std::queue <pair<int,int>> bfs_queue;\n vector<vector<bool>> visited(grid.size(),vector<bool>(grid[0].size(), 0));\n \n //Push the first occuring land into the bfs \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 bfs_queue.push({i,j});\n visited[i][j] = true;\n break;\n }\n }\n }\n\n vector<pair<int,int>> direction{{0,1}, {0,-1},{1,0},{-1,0}};\n int perimeter = 0;\n //find the sides of square adjacent to water or its out of bound.\n while (!bfs_queue.empty())\n {\n int qsize = bfs_queue.size();\n for (int i = 0; i< qsize; i++)\n {\n auto [cur_row, cur_col] = bfs_queue.front();\n bfs_queue.pop();\n for (auto delta: direction)\n {\n int next_row = cur_row + delta.first;\n int next_col = cur_col + delta.second;\n\n if (next_row< 0 || next_row >= grid.size() || next_col < 0 || next_col >= grid[0].size() || \n grid[next_row][next_col] == 0)\n {\n perimeter++;\n }\n else if (next_row >=0 && next_row < grid.size() &&\n next_col >= 0 && next_col < grid[0].size() && !visited[next_row][next_col])\n {\n bfs_queue.push({next_row,next_col});\n visited[next_row][next_col] = true;\n }\n } \n }\n }\n return perimeter;\n }\n};",
"memory": "107042"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int islandPerimeter(vector<vector<int>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n int peri = 0;\n queue<pair<int, int>> q;\n vector<int> x = {0, 1, 0, -1};\n vector<int> y = {1, 0, -1, 0};\n vector<vector<int>> vis(n, vector<int> (m, 0));\n int ar = 0;\n\n for(int i =0; i<n; i++){\n for(int j=0; j<m; j++){\n if(grid[i][j] && !vis[i][j]){\n q.push({i, j});\n vis[i][j]=1;\n while(!q.empty()){\n int xx = q.front().first;\n int yy = q.front().second;\n q.pop();\n ar++;\n for(int k =0; k<4; k++){\n int newx = xx+x[k];\n int newy = yy+y[k];\n if(newx>=0 && newx <n && newy>=0 && newy<m){\n if(grid[newx][newy]==1 && !vis[newx][newy]){\n q.push({newx, newy});\n vis[newx][newy] = 1;\n }\n if(!grid[newx][newy]){\n peri++;\n cout << xx<<yy<<endl;\n }\n }\n else peri++;\n }\n }\n }\n }\n }\n return peri;\n }\n};",
"memory": "108437"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int islandPerimeter(vector<vector<int>>& grid) {\n queue<pair<int, int>> q;\n int rows = grid.size();\n int cols = grid[0].size();\n \n // Find the first land cell and push it to the queue\n bool found = false;\n for(int i = 0; i < rows; i++) {\n for(int j = 0; j < cols; j++) {\n if(grid[i][j] == 1) {\n q.push({i, j});\n found = true;\n break;\n }\n }\n if (found) break;\n } \n \n int perimeter = 0;\n vector<vector<int>> visited(rows, vector<int>(cols, 0));\n\n while(!q.empty()) {\n pair<int, int> front = q.front();\n int r = front.first;\n int c = front.second;\n q.pop();\n \n if (visited[r][c]) continue;\n visited[r][c] = 1;\n\n int adj = 0;\n \n // Check adjacent cells and calculate perimeter\n if(r + 1 < rows && grid[r + 1][c] == 1) {\n adj++;\n if(!visited[r + 1][c]) {\n q.push({r + 1, c});\n }\n }\n if(r - 1 >= 0 && grid[r - 1][c] == 1) {\n adj++;\n if(!visited[r - 1][c]) {\n q.push({r - 1, c});\n }\n }\n if(c + 1 < cols && grid[r][c + 1] == 1) {\n adj++;\n if(!visited[r][c + 1]) {\n q.push({r, c + 1});\n }\n }\n if(c - 1 >= 0 && grid[r][c - 1] == 1) {\n adj++;\n if(!visited[r][c - 1]) {\n q.push({r, c - 1});\n }\n }\n perimeter += (4 - adj);\n }\n\n return perimeter;\n }\n};\n",
"memory": "109832"
} |
463 | <p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1]]
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,0]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>row == grid.length</code></li>
<li><code>col == grid[i].length</code></li>
<li><code>1 <= row, col <= 100</code></li>
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
<li>There is exactly one island in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool is_valid(std::vector<std::vector<int>>& grid, int r, int c) {\n if (r >= 0 && r < grid.size() && c >= 0 && c < grid[0].size()) {\n if (grid[r][c] == 1 || grid[r][c]==-1)\n return true;\n }\n return false;\n }\n int bfs(std::vector<std::vector<int>>& grid, int r, int c){\n int perimeter = 0;\n std::queue<std::pair<int,int>> que;\n std::vector<std::vector<int>> directions { {1,0},{0,1},{-1,0},{0,-1}};\n que.push({r,c});\n grid[r][c]=-1;\n while(!que.empty()){\n int count=0;\n std::pair<int,int> current = que.front();\n int row = current.first;\n int col = current.second;\n que.pop();\n for(const auto& dir: directions){\n int n_r = row+dir[0];\n int n_c = col+dir[1];\n if(is_valid(grid,n_r,n_c)){\n count++;\n if(grid[n_r][n_c]==1){\n que.push({n_r,n_c});\n grid[n_r][n_c]=-1;\n }\n }\n }\n perimeter += 4 - count;\n }\n return perimeter;\n\n }\n int islandPerimeter(vector<vector<int>>& grid) {\n int n_rows = grid.size();\n int n_cols = grid[0].size();\n for(int row=0; row<n_rows; ++row){\n for(int col=0; col<n_cols; ++col){\n if (grid[row][col]==1){\n return bfs(grid,row,col);\n }\n }\n }\n return 0;\n\n }\n};",
"memory": "109832"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.