id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
482 | <p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p>
<p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p>
<p>Return <em>the reformatted license key</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "5F3Z-2e-9-w", k = 4
<strong>Output:</strong> "5F3Z-2E9W"
<strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters.
Note that the two extra dashes are not needed and can be removed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "2-5g-3-J", k = 2
<strong>Output:</strong> "2-5G-3J"
<strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of English letters, digits, and dashes <code>'-'</code>.</li>
<li><code>1 <= k <= 10<sup>4</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n\n string l;\n for(int i=0;i<s.length();i++)\n {\n if(s[i]!='-')\n {\n l.push_back(toupper(s[i]));\n }\n }\n reverse(l.begin(),l.end());\n int count=0;\n string ans;\n\n for(int i=0;i<l.length();i++)\n {\n count++;\n ans.push_back(l[i]);\n\n if(count==k&&i!=l.length()-1)\n {\n ans.push_back('-');\n count=0;\n }\n \n }\n reverse(ans.begin(),ans.end());\n return ans;\n }\n};",
"memory": "10200"
} |
482 | <p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p>
<p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p>
<p>Return <em>the reformatted license key</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "5F3Z-2e-9-w", k = 4
<strong>Output:</strong> "5F3Z-2E9W"
<strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters.
Note that the two extra dashes are not needed and can be removed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "2-5g-3-J", k = 2
<strong>Output:</strong> "2-5G-3J"
<strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of English letters, digits, and dashes <code>'-'</code>.</li>
<li><code>1 <= k <= 10<sup>4</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n\n string l;\n for(int i=0;i<s.length();i++)\n {\n if(s[i]!='-')\n {\n l.push_back(toupper(s[i]));\n }\n }\n reverse(l.begin(),l.end());\n int count=0;\n string ans;\n\n for(int i=0;i<l.length();i++)\n {\n count++;\n ans.push_back(l[i]);\n\n if(count==k&&i!=l.length()-1)\n {\n ans.push_back('-');\n count=0;\n }\n \n }\n reverse(ans.begin(),ans.end());\n return ans;\n }\n};",
"memory": "10300"
} |
482 | <p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p>
<p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p>
<p>Return <em>the reformatted license key</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "5F3Z-2e-9-w", k = 4
<strong>Output:</strong> "5F3Z-2E9W"
<strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters.
Note that the two extra dashes are not needed and can be removed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "2-5g-3-J", k = 2
<strong>Output:</strong> "2-5G-3J"
<strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of English letters, digits, and dashes <code>'-'</code>.</li>
<li><code>1 <= k <= 10<sup>4</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\n public:\n string licenseKeyFormatting(string s, int k) {\n\n string l;\n for(int i=0;i<s.length();i++)\n {\n if(s[i]!='-')\n {\n l.push_back(toupper(s[i]));\n }\n }\n reverse(l.begin(),l.end());\n int count=0;\n string ans;\n\n for(int i=0;i<l.length();i++)\n {\n count++;\n ans.push_back(l[i]);\n\n if(count==k&&i!=l.length()-1)\n {\n ans.push_back('-');\n count=0;\n }\n \n }\n reverse(ans.begin(),ans.end());\n return ans;\n }\n };\n",
"memory": "10400"
} |
482 | <p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p>
<p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p>
<p>Return <em>the reformatted license key</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "5F3Z-2e-9-w", k = 4
<strong>Output:</strong> "5F3Z-2E9W"
<strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters.
Note that the two extra dashes are not needed and can be removed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "2-5g-3-J", k = 2
<strong>Output:</strong> "2-5G-3J"
<strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of English letters, digits, and dashes <code>'-'</code>.</li>
<li><code>1 <= k <= 10<sup>4</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n string temp =\"\";\n string proto = \"\";\n string ans =\"\";\n for(auto it: s){\n if(it!='-'){\n temp+= toupper(it);\n }\n else {\n proto +=temp;\n temp=\"\";\n }\n }\n proto+= temp;\n int j=0;\n for(int i =proto.size()-1;i>=0;i--){\n if(j==k){\n ans+='-';\n j=0;\n }\n ans+=proto[i];\n j++;\n }\n reverse(ans.begin(),ans.end());\n return ans;\n }\n};",
"memory": "10500"
} |
482 | <p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p>
<p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p>
<p>Return <em>the reformatted license key</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "5F3Z-2e-9-w", k = 4
<strong>Output:</strong> "5F3Z-2E9W"
<strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters.
Note that the two extra dashes are not needed and can be removed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "2-5g-3-J", k = 2
<strong>Output:</strong> "2-5G-3J"
<strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of English letters, digits, and dashes <code>'-'</code>.</li>
<li><code>1 <= k <= 10<sup>4</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string upper(string s)\n {\n \n for(int i=0;i<s.size();i++)\n {\n if(s[i]>='a' and s[i]<='z')\n {\n s[i] = s[i] - 'a'+ 'A'; \n }\n }\n return s;\n }\n string remove(string s)\n {\n string temp;\n for(int i=0;i<s.size();i++)\n {\n if(s[i]!='-')\n {\n temp+=s[i];\n }\n else\n {\n continue;\n }\n }\n return temp;\n }\n string licenseKeyFormatting(string s, int k) {\n //\n int n = s.size();\n \n // this should be first part length\n // if remainder is zero then each part is of equal length\n string ans;\n // we also need to transform string to uppercase\n string newstring = upper(s);\n newstring = remove(newstring);\n \n int charandnumcnt = newstring.size();\n if(charandnumcnt==0)\n {\n return ans;\n }\n int remainder = charandnumcnt%k;\n // we will remove dashes\n \n int i=0;\n \n while(remainder!=0)\n {\n ans+=newstring[i];\n i++;\n remainder--;\n if(remainder==0)\n {\n ans+='-';\n }\n \n }\n \n // for first part we are done\n int cnt=0;\n \n for(i;i<charandnumcnt;i++)\n {\n while(cnt<k)\n {\n ans+=newstring[i];\n cnt++;\n i++;\n }\n if(cnt==k)\n {\n ans+='-'; // adding the dash after each part\n cnt=0; // updating the cnt\n i--; // at the end i was getting incremented two times so for that\n }\n \n }\n ans.pop_back();\n return ans;\n \n }\n};",
"memory": "10600"
} |
482 | <p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p>
<p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p>
<p>Return <em>the reformatted license key</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "5F3Z-2e-9-w", k = 4
<strong>Output:</strong> "5F3Z-2E9W"
<strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters.
Note that the two extra dashes are not needed and can be removed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "2-5g-3-J", k = 2
<strong>Output:</strong> "2-5G-3J"
<strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of English letters, digits, and dashes <code>'-'</code>.</li>
<li><code>1 <= k <= 10<sup>4</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n string justalphanum;\n string res;\n for(int i=0;i<s.size();i++){\n if(!isalnum(s[i])){\n continue;\n }\n justalphanum.push_back(s[i]);\n\n }\n\n string upperal;\n for(int i=0;i<justalphanum.size();i++){\n upperal.push_back(toupper(justalphanum[i]));\n }\n reverse(upperal.begin(),upperal.end());\n\n\n int count=0;\n for(int i=0;i<upperal.size();i++){\n count++;\n res.push_back(upperal[i]);\n if(count==k && i<upperal.size()-1){\n res.push_back('-');\n count=0;\n }\n\n }\n reverse(res.begin(),res.end());\n return res;\n \n }\n};",
"memory": "10700"
} |
482 | <p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p>
<p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p>
<p>Return <em>the reformatted license key</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "5F3Z-2e-9-w", k = 4
<strong>Output:</strong> "5F3Z-2E9W"
<strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters.
Note that the two extra dashes are not needed and can be removed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "2-5g-3-J", k = 2
<strong>Output:</strong> "2-5G-3J"
<strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of English letters, digits, and dashes <code>'-'</code>.</li>
<li><code>1 <= k <= 10<sup>4</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n string licenceWithoutDash = \"\";\n for(auto c : s) {\n if(c != '-')\n licenceWithoutDash.push_back(toupper(c));\n }\n string currWord = \"\";\n string res = \"\";\n for(int i = licenceWithoutDash.size() - 1; i >= 0; --i) {\n currWord.push_back(licenceWithoutDash[i]);\n if(currWord.size() == k) {\n if(res.size() != 0) res.push_back('-');\n res += currWord;\n currWord = \"\";\n }\n }\n if(currWord.size() > 0) {\n if(res.size() != 0) res.push_back('-');\n res += currWord;\n }\n reverse(res.begin(), res.end());\n return res;\n }\n};",
"memory": "10800"
} |
482 | <p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p>
<p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p>
<p>Return <em>the reformatted license key</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "5F3Z-2e-9-w", k = 4
<strong>Output:</strong> "5F3Z-2E9W"
<strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters.
Note that the two extra dashes are not needed and can be removed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "2-5g-3-J", k = 2
<strong>Output:</strong> "2-5G-3J"
<strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of English letters, digits, and dashes <code>'-'</code>.</li>
<li><code>1 <= k <= 10<sup>4</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n stack<char>st;\n string result=\"\";\n\n int count=0;\n\n\n for(auto ch : s){\n\n if(ch != '-'){\n st.push(toupper(ch));\n }\n \n\n }\n\n while(! st.empty()){\n \n result += st.top();\n st.pop();\n count++;\n\n if(count == k && !st.empty()){\n \n result += '-';\n count=0;\n\n }\n\n }\n\n reverse(result.begin(),result.end());\n\n return result;\n \n }\n};",
"memory": "10900"
} |
482 | <p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p>
<p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p>
<p>Return <em>the reformatted license key</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "5F3Z-2e-9-w", k = 4
<strong>Output:</strong> "5F3Z-2E9W"
<strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters.
Note that the two extra dashes are not needed and can be removed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "2-5g-3-J", k = 2
<strong>Output:</strong> "2-5G-3J"
<strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of English letters, digits, and dashes <code>'-'</code>.</li>
<li><code>1 <= k <= 10<sup>4</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n int r = 0;\n string res = \"\";\n string q = \"\";\n for (auto i : s){\n if (i != '-'){\n r++;\n q.push_back(toupper(i));\n }\n }\n int m = q.size()/k;\n int rem = q.size() % k;\n int temp = 0;\n res += q.substr(0, rem);\n cout << res;\n if (rem != 0) res += \"-\";\n for (auto c : q.substr(rem, q.size())){\n\n res.push_back(c);\n temp+=1;\n if (temp % k == 0){\n temp = 0;\n res.push_back('-');\n }\n }\n\n \n return res.substr(0, res.size()-1);\n }\n};",
"memory": "11000"
} |
482 | <p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p>
<p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p>
<p>Return <em>the reformatted license key</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "5F3Z-2e-9-w", k = 4
<strong>Output:</strong> "5F3Z-2E9W"
<strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters.
Note that the two extra dashes are not needed and can be removed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "2-5g-3-J", k = 2
<strong>Output:</strong> "2-5G-3J"
<strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of English letters, digits, and dashes <code>'-'</code>.</li>
<li><code>1 <= k <= 10<sup>4</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n stack<char> letters;\n \n int n = s.length();\n int letterCount = 0;\n for(int i = n - 1; i > -1; --i) {\n if(s[i] != '-') {\n if(letterCount == k) {\n letters.push('-');\n letterCount = 0;\n }\n\n if(s[i] >= 97 && s[i] <= 122) {\n letters.push(s[i] - 32);\n } else {\n letters.push(s[i]);\n }\n \n letterCount++;\n }\n }\n \n string formatted = \"\";\n while(!letters.empty()) {\n formatted += letters.top();\n letters.pop();\n }\n \n return formatted;\n}\n\n};",
"memory": "11100"
} |
482 | <p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p>
<p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p>
<p>Return <em>the reformatted license key</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "5F3Z-2e-9-w", k = 4
<strong>Output:</strong> "5F3Z-2E9W"
<strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters.
Note that the two extra dashes are not needed and can be removed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "2-5g-3-J", k = 2
<strong>Output:</strong> "2-5G-3J"
<strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of English letters, digits, and dashes <code>'-'</code>.</li>
<li><code>1 <= k <= 10<sup>4</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n stack<char> letters;\n \n int n = s.length();\n int letterCount = 0;\n for(int i = n - 1; i > -1; --i) {\n if(s[i] != '-') {\n if(letterCount == k) {\n letters.push('-');\n letterCount = 0;\n }\n\n if(s[i] >= 97 && s[i] <= 122) {\n letters.push(s[i] - 32);\n } else {\n letters.push(s[i]);\n }\n \n letterCount++;\n }\n }\n \n string formatted = \"\";\n while(!letters.empty()) {\n formatted += letters.top();\n letters.pop();\n }\n \n return formatted;\n}\n\n};",
"memory": "11100"
} |
482 | <p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p>
<p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p>
<p>Return <em>the reformatted license key</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "5F3Z-2e-9-w", k = 4
<strong>Output:</strong> "5F3Z-2E9W"
<strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters.
Note that the two extra dashes are not needed and can be removed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "2-5g-3-J", k = 2
<strong>Output:</strong> "2-5G-3J"
<strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of English letters, digits, and dashes <code>'-'</code>.</li>
<li><code>1 <= k <= 10<sup>4</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n // Remove all dashes from the original string\n s.erase(remove(s.begin(), s.end(), '-'), s.end());\n \n // Convert all characters to uppercase\n for (auto& ch : s) {\n ch = toupper(ch);\n }\n \n // Initialize the formatted key\n string key = \"\";\n \n // Calculate the first group's size\n int firstGroupSize = s.size() % k;\n int i = 0;\n \n // If the first group has characters, add them first\n if (firstGroupSize > 0) {\n key = s.substr(0, firstGroupSize);\n i = firstGroupSize;\n }\n \n // Add the remaining groups\n while (i < s.size()) {\n if (!key.empty()) {\n key += \"-\";\n }\n key += s.substr(i, k);\n i += k;\n }\n \n return key;\n }\n};\n",
"memory": "11200"
} |
482 | <p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p>
<p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p>
<p>Return <em>the reformatted license key</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "5F3Z-2e-9-w", k = 4
<strong>Output:</strong> "5F3Z-2E9W"
<strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters.
Note that the two extra dashes are not needed and can be removed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "2-5g-3-J", k = 2
<strong>Output:</strong> "2-5G-3J"
<strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of English letters, digits, and dashes <code>'-'</code>.</li>
<li><code>1 <= k <= 10<sup>4</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n\n int cnt = 0;\n\n queue<char> q;\n for (char ch : s) {\n if (ch != '-') {\n q.push(toupper(ch)); // Convert to uppercase and push to queue\n cnt++;\n }\n }\n\n string ans = \"\";\n int firstGroupSize = cnt % k;\n\n if (firstGroupSize > 0) {\n for (int i = 0; i < firstGroupSize; ++i) {\n ans += q.front();\n q.pop();\n }\n }\n // Handle the remaining groups\n while (!q.empty()) {\n if (!ans.empty()) {\n ans += '-'; // Add a dash before each new group\n }\n for (int i = 0; i < k; ++i) {\n if (!q.empty()) {\n ans += q.front();\n q.pop();\n }\n }\n }\n\n return ans;\n\n \n }\n};",
"memory": "11300"
} |
482 | <p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p>
<p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p>
<p>Return <em>the reformatted license key</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "5F3Z-2e-9-w", k = 4
<strong>Output:</strong> "5F3Z-2E9W"
<strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters.
Note that the two extra dashes are not needed and can be removed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "2-5g-3-J", k = 2
<strong>Output:</strong> "2-5G-3J"
<strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of English letters, digits, and dashes <code>'-'</code>.</li>
<li><code>1 <= k <= 10<sup>4</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n \n std::string cleaned;\n \n // Step 1: Remove all dashes and convert to uppercase\n for (char c : s) {\n if (c != '-') {\n cleaned += std::toupper(c);\n }\n }\n \n // Step 2: Calculate the size of the first group\n int firstGroupSize = cleaned.size() % k;\n std::string result;\n \n // Step 3: Add the first group if it exists\n if (firstGroupSize > 0) {\n result = cleaned.substr(0, firstGroupSize);\n }\n \n // Step 4: Add the remaining groups of size k\n for (int i = firstGroupSize; i < cleaned.size(); i += k) {\n if (!result.empty()) {\n result += \"-\"; // Add dash between groups\n }\n result += cleaned.substr(i, k);\n }\n \n return result;\n }\n};",
"memory": "11600"
} |
482 | <p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p>
<p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p>
<p>Return <em>the reformatted license key</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "5F3Z-2e-9-w", k = 4
<strong>Output:</strong> "5F3Z-2E9W"
<strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters.
Note that the two extra dashes are not needed and can be removed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "2-5g-3-J", k = 2
<strong>Output:</strong> "2-5G-3J"
<strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of English letters, digits, and dashes <code>'-'</code>.</li>
<li><code>1 <= k <= 10<sup>4</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n if(k == 0 || s.size() == 0) return \"\";\n int c = 0, dashes = 0;\n string chars = \"\";\n for(auto x : s) {\n if(x != '-') {\n c++;\n if(!isdigit(x)) {\n x = toupper(x);\n }\n chars += x;\n } else dashes++;\n }\n\n string ans = \"\";\n bool one = false;\n if(c%k != 0) one = true;\n int ind = 0;\n if(one) {\n int rem = c%k;\n ans += chars.substr(0, rem);\n ind = rem;\n \n } \n if(ind < chars.size() && one) {\n ans += '-';\n }\n while(ind < chars.size()) {\n \n ans += (chars.substr(ind, k));\n ind += k;\n if(ind != chars.size()) ans += '-';\n }\n\n return ans;\n\n }\n};",
"memory": "11600"
} |
482 | <p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p>
<p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p>
<p>Return <em>the reformatted license key</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "5F3Z-2e-9-w", k = 4
<strong>Output:</strong> "5F3Z-2E9W"
<strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters.
Note that the two extra dashes are not needed and can be removed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "2-5g-3-J", k = 2
<strong>Output:</strong> "2-5G-3J"
<strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of English letters, digits, and dashes <code>'-'</code>.</li>
<li><code>1 <= k <= 10<sup>4</sup></code></li>
</ul>
| 3 | {
"code": "#include <string>\nusing namespace std;\n\nclass Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n string cleanStr;\n \n // Step 1: Remove all dashes and convert to uppercase\n for (char c : s) {\n if (c != '-') {\n cleanStr += toupper(c); // Convert to uppercase and append\n }\n }\n \n // Step 2: Prepare result with dashes after every k characters from the end\n string result;\n int len = cleanStr.length();\n int firstGroupLen = len % k; // Length of the first group\n \n int i = 0;\n \n // Add the first group if it has a non-zero length\n if (firstGroupLen != 0) {\n result += cleanStr.substr(i, firstGroupLen);\n i += firstGroupLen;\n }\n \n // Add dashes and the next groups of k characters\n while (i < len) {\n if (!result.empty()) {\n result += '-'; // Add a dash between groups\n }\n result += cleanStr.substr(i, k); // Add a group of k characters\n i += k;\n }\n \n return result;\n }\n};\n",
"memory": "11700"
} |
482 | <p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p>
<p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p>
<p>Return <em>the reformatted license key</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "5F3Z-2e-9-w", k = 4
<strong>Output:</strong> "5F3Z-2E9W"
<strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters.
Note that the two extra dashes are not needed and can be removed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "2-5g-3-J", k = 2
<strong>Output:</strong> "2-5G-3J"
<strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of English letters, digits, and dashes <code>'-'</code>.</li>
<li><code>1 <= k <= 10<sup>4</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n string str = \"\";\n for (auto c : s) {\n if (c == '-') continue;\n if (isdigit(c)) str += c;\n else str += toupper(c);\n }\n\n string res = \"\";\n reverse(str.begin(), str.end());\n for (int i = 0; i < str.size()/k; ++i) {\n res += str.substr(i*k, k) + '-';\n }\n if (str.size() % k != 0)\n res += str.substr(str.size()/k *k);\n else {\n if (!res.empty()) res.pop_back();\n }\n reverse(res.begin(), res.end());\n\n return res;\n }\n};",
"memory": "11800"
} |
482 | <p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p>
<p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p>
<p>Return <em>the reformatted license key</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "5F3Z-2e-9-w", k = 4
<strong>Output:</strong> "5F3Z-2E9W"
<strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters.
Note that the two extra dashes are not needed and can be removed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "2-5g-3-J", k = 2
<strong>Output:</strong> "2-5G-3J"
<strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of English letters, digits, and dashes <code>'-'</code>.</li>
<li><code>1 <= k <= 10<sup>4</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n\n string normalString = \"\";\n for (auto x : s) {\n if(x!='-'){\n normalString += toupper(x);\n }\n }\n\n string ans = \"\";\n\n int length = normalString.length();\n int start = 0;\n reverse(normalString.begin(),normalString.end());\n while (start < length) {\n string sub = normalString.substr(start, k);\n //cout<<normalString.substr(start, k)<<endl;\n //cout<<normalString.substr(1, 4)<<endl;\n if (start == 0) {\n ans = sub;\n\n } else {\n ans += \"-\" + sub;\n }\n start += k;\n }\n reverse(ans.begin(),ans.end());\n return ans;\n }\n};",
"memory": "11900"
} |
482 | <p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p>
<p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p>
<p>Return <em>the reformatted license key</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "5F3Z-2e-9-w", k = 4
<strong>Output:</strong> "5F3Z-2E9W"
<strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters.
Note that the two extra dashes are not needed and can be removed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "2-5g-3-J", k = 2
<strong>Output:</strong> "2-5G-3J"
<strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of English letters, digits, and dashes <code>'-'</code>.</li>
<li><code>1 <= k <= 10<sup>4</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n transform(s.begin(), s.end(), s.begin(), ::toupper);\n string full=\"\";\n int count=0;\n for(int i=0;i<s.length();i++){\n if(s[i]!='-'){\n full+=s[i];\n count++;\n }\n }\n double x=ceil((float)count/k);\n string ans=\"\";\n int remaining_len=full.length()%k;\n ans+=full.substr(0,remaining_len);\n if(ans.length()>0){\n ans=ans+\"-\";\n }\n for(int i=remaining_len;i<full.length();i+=k){\n ans+=full.substr(i,k)+\"-\";\n }\n return ans.substr(0,ans.length()-1);\n \n }\n};",
"memory": "12000"
} |
482 | <p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p>
<p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p>
<p>Return <em>the reformatted license key</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "5F3Z-2e-9-w", k = 4
<strong>Output:</strong> "5F3Z-2E9W"
<strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters.
Note that the two extra dashes are not needed and can be removed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "2-5g-3-J", k = 2
<strong>Output:</strong> "2-5G-3J"
<strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of English letters, digits, and dashes <code>'-'</code>.</li>
<li><code>1 <= k <= 10<sup>4</sup></code></li>
</ul>
| 3 | {
"code": "/*\n20240820 18:01\n*/\nclass Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n string str = \"\";\n for (auto c : s) {\n if (isdigit(c) || isalpha(c)) str += toupper(c);\n }\n int n = str.size() / k;\n reverse(str.begin(), str.end());\n string res = \"\";\n string tmp = \"\";\n for (int i = 0; i < n; ++i) {\n tmp = str.substr(i*k, k);\n res += tmp + \"-\";\n }\n if (str.size() % k != 0) {\n tmp = str.substr(n*k);\n res += tmp;\n } else {\n res = res.substr(0, res.size()-1);\n }\n reverse(res.begin(), res.end());\n return res;\n }\n};",
"memory": "12100"
} |
482 | <p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p>
<p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p>
<p>Return <em>the reformatted license key</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "5F3Z-2e-9-w", k = 4
<strong>Output:</strong> "5F3Z-2E9W"
<strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters.
Note that the two extra dashes are not needed and can be removed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "2-5g-3-J", k = 2
<strong>Output:</strong> "2-5G-3J"
<strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of English letters, digits, and dashes <code>'-'</code>.</li>
<li><code>1 <= k <= 10<sup>4</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n transform(s.begin(), s.end(), s.begin(), ::toupper);\n string full=\"\";\n int count=0;\n for(int i=0;i<s.length();i++){\n if(s[i]!='-'){\n full+=s[i];\n count++;\n }\n }\n double x=ceil((float)count/k);\n string ans=\"\";\n int remaining_len=full.length()%k;\n ans+=full.substr(0,remaining_len);\n if(ans.length()>0){\n ans=ans+\"-\";\n }\n for(int i=remaining_len;i<full.length();i+=k){\n ans+=full.substr(i,k)+\"-\";\n }\n return ans.substr(0,ans.length()-1);\n \n }\n};",
"memory": "12300"
} |
482 | <p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p>
<p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p>
<p>Return <em>the reformatted license key</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "5F3Z-2e-9-w", k = 4
<strong>Output:</strong> "5F3Z-2E9W"
<strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters.
Note that the two extra dashes are not needed and can be removed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "2-5g-3-J", k = 2
<strong>Output:</strong> "2-5G-3J"
<strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of English letters, digits, and dashes <code>'-'</code>.</li>
<li><code>1 <= k <= 10<sup>4</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n transform(s.begin(), s.end(), s.begin(), ::toupper);\n string full=\"\";\n int count=0;\n for(int i=0;i<s.length();i++){\n if(s[i]!='-'){\n full+=s[i];\n count++;\n }\n }\n double x=ceil((float)count/k);\n string ans=\"\";\n int remaining_len=full.length()%k;\n ans+=full.substr(0,remaining_len);\n if(ans.length()>0){\n ans=ans+\"-\";\n }\n for(int i=remaining_len;i<full.length();i+=k){\n ans+=full.substr(i,k)+\"-\";\n }\n return ans.substr(0,ans.length()-1);\n \n }\n};",
"memory": "12300"
} |
482 | <p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p>
<p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p>
<p>Return <em>the reformatted license key</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "5F3Z-2e-9-w", k = 4
<strong>Output:</strong> "5F3Z-2E9W"
<strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters.
Note that the two extra dashes are not needed and can be removed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "2-5g-3-J", k = 2
<strong>Output:</strong> "2-5G-3J"
<strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of English letters, digits, and dashes <code>'-'</code>.</li>
<li><code>1 <= k <= 10<sup>4</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n char to_capital(char a)\n {\n if(a >= 'a' && a <= 'z')\n return a - 'a' + 'A';\n return a; \n }\n string licenseKeyFormatting(string s, int k) {\n string temp = \"\";\n int n = s.length();\n stack<string> st;\n for(int i=n-1;i>=0;i--)\n {\n if(temp.size() == k && s[i] != '-')\n {\n reverse(temp.begin(),temp.end());\n st.push(temp);\n temp = \"\";\n }\n if(s[i] != '-')\n {\n temp += to_capital(s[i]);\n }\n }\n reverse(temp.begin(),temp.end());\n st.push(temp);\n string ans = \"\";\n while(!st.empty())\n {\n ans += st.top();\n st.pop();\n if(!st.empty())\n ans += '-';\n }\n return ans;\n }\n};",
"memory": "13500"
} |
482 | <p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p>
<p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p>
<p>Return <em>the reformatted license key</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "5F3Z-2e-9-w", k = 4
<strong>Output:</strong> "5F3Z-2E9W"
<strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters.
Note that the two extra dashes are not needed and can be removed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "2-5g-3-J", k = 2
<strong>Output:</strong> "2-5G-3J"
<strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of English letters, digits, and dashes <code>'-'</code>.</li>
<li><code>1 <= k <= 10<sup>4</sup></code></li>
</ul>
| 3 | {
"code": "#include <iostream>\n#include <string>\n#include <vector>\nusing namespace std;\n\nclass Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n string ans = \"\";\n\n for (auto &x : s) {\n if (x != '-') {\n if (x >= 'a' && x <= 'z')\n ans += x - 'a' + 'A'; \n else\n ans += x; \n }\n }\n\n if (ans.empty()) return \"\";\n\n vector<string> v;\n int rem = ans.length() % k;\n string temp = \"\";\n\n if (rem) {\n v.push_back(ans.substr(0, rem));\n }\n\n for (int i = rem; i < ans.length(); i++) {\n temp += ans[i];\n if (temp.size() == k) {\n v.push_back(temp);\n temp = \"\";\n }\n }\n if (!temp.empty()) {\n v.push_back(temp);\n }\n ans = \"\";\n for (int i = 0; i < v.size(); i++) {\n ans += v[i];\n if (i < v.size() - 1) {\n ans += \"-\";\n }\n }\n return ans;\n }\n};\n\n\n",
"memory": "15200"
} |
482 | <p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p>
<p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p>
<p>Return <em>the reformatted license key</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "5F3Z-2e-9-w", k = 4
<strong>Output:</strong> "5F3Z-2E9W"
<strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters.
Note that the two extra dashes are not needed and can be removed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "2-5g-3-J", k = 2
<strong>Output:</strong> "2-5G-3J"
<strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of English letters, digits, and dashes <code>'-'</code>.</li>
<li><code>1 <= k <= 10<sup>4</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n string x=\"\";\n vector<string> v;\n for(auto c:s){\n if(c!='-'&& !isdigit(c)){\n x+=toupper(c); \n }else if(c!='-'&& isdigit(c)){\n x+=c;\n }\n }\n for(int i=x.size()-1;i>=0;){\n int a=k;\n s=\"\";\n for(int j=0;j<k;j++){\n if(i>=0){\n s+=x[i];\n i--;\n }\n }\n reverse(s.begin(),s.end());\n v.push_back(s);\n }\n s=\"\";\n for(int i=v.size()-1;i>=0;i--){\n s+=v[i];\n if(i!=0)\n s+='-';\n }\n return s;\n }\n};",
"memory": "15300"
} |
482 | <p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p>
<p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p>
<p>Return <em>the reformatted license key</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "5F3Z-2e-9-w", k = 4
<strong>Output:</strong> "5F3Z-2E9W"
<strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters.
Note that the two extra dashes are not needed and can be removed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "2-5g-3-J", k = 2
<strong>Output:</strong> "2-5G-3J"
<strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of English letters, digits, and dashes <code>'-'</code>.</li>
<li><code>1 <= k <= 10<sup>4</sup></code></li>
</ul>
| 3 | {
"code": "#include <iostream>\n#include <string>\n#include <vector>\nusing namespace std;\n\nclass Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n string ans = \"\";\n\n for (auto &x : s) {\n if (x != '-') {\n if (x >= 'a' && x <= 'z')\n ans += x - 'a' + 'A'; \n else\n ans += x; \n }\n }\n\n if (ans.empty()) return \"\";\n\n vector<string> v;\n int rem = ans.length() % k;\n string temp = \"\";\n\n if (rem) {\n v.push_back(ans.substr(0, rem));\n }\n\n for (int i = rem; i < ans.length(); i++) {\n temp += ans[i];\n if (temp.size() == k) {\n v.push_back(temp);\n temp = \"\";\n }\n }\n // if (!temp.empty()) {\n // v.push_back(temp);\n // }\n ans = \"\";\n for (int i = 0; i < v.size(); i++) {\n ans += v[i];\n if (i < v.size() - 1) {\n ans += \"-\";\n }\n }\n return ans;\n }\n};\n\n\n",
"memory": "15400"
} |
482 | <p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p>
<p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p>
<p>Return <em>the reformatted license key</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "5F3Z-2e-9-w", k = 4
<strong>Output:</strong> "5F3Z-2E9W"
<strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters.
Note that the two extra dashes are not needed and can be removed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "2-5g-3-J", k = 2
<strong>Output:</strong> "2-5G-3J"
<strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of English letters, digits, and dashes <code>'-'</code>.</li>
<li><code>1 <= k <= 10<sup>4</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n vector<string> ans;\n string temp = \"\";\n \n // Remove dashes and convert to uppercase\n for (char c : s) {\n if (c != '-') {\n temp.push_back(toupper(c));\n }\n }\n \n // Calculate the size of the first group\n int firstGroupSize = temp.size() % k;\n if (firstGroupSize > 0) {\n ans.push_back(temp.substr(0, firstGroupSize));\n }\n \n // Process the rest of the characters in groups of size k\n for (int i = firstGroupSize; i < temp.size(); i += k) {\n ans.push_back(temp.substr(i, k));\n }\n \n // Build the final formatted string\n string uttar = \"\";\n for (int i = 0; i < ans.size(); i++) {\n if (i > 0) {\n uttar += \"-\";\n }\n uttar += ans[i];\n }\n \n return uttar;\n }\n};\n",
"memory": "15600"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n \n int iMax = 0;\n int iCurr = 0;\n for (int i = 0; i < nums.size(); ++i)\n {\n if (nums[i] == 1)\n {\n iCurr++;\n }\n else\n {\n if (iMax < iCurr)\n iMax = iCurr;\n \n iCurr = 0;\n }\n }\n \n if (iMax < iCurr)\n iMax = iCurr;\n \n return iMax;\n }\n};",
"memory": "38600"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n int maxi = 0, count = 0;\n\n for(int i = 0; i<nums.size(); i++){\n if(nums[i] == 1){\n count++;\n maxi = max(maxi, count);\n }\n\n else{\n count = 0;\n }\n }\n\n return max(maxi, count);\n }\n};",
"memory": "38600"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n \n int count = 0;\n int ans = 0;\n \n for(int i = 0; i < nums.size(); ++i) {\n \n if(nums[i] == 1) {\n ++count; \n } else {\n if(ans < count) ans = count; \n count = 0;\n }\n }\n \n // If the loop ends with a sequence of 1s, we need to update ans\n if(ans < count) ans = count;\n \n return ans;\n }\n};\n",
"memory": "38700"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n\n int cnt = 0;\n int maxi = 0;\n for (int i = 0; i < nums.size(); i++) {\n if (nums[i] == 1) {\n cnt++;\n maxi = max(maxi, cnt);\n } else {\n cnt = 0;\n }\n }\n return maxi;\n }\n};",
"memory": "38700"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n int max_count = 0;\n int current_count = 0;\n\n for (int num : nums) {\n if (num == 1) {\n current_count += 1;\n max_count = max(max_count, current_count);\n } else {\n current_count = 0;\n }\n }\n\n return max_count;\n }\n};",
"memory": "38800"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n int n=nums.size();\n int best=0,count=0;\n for(int i =0;i<n;i++){\n if(nums[i]==1) count++;\n else{\n \n count=0;\n }\n best=max(best,count);\n }\n return best;\n }\n};",
"memory": "38800"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n int max=0,count=0;\n int x=nums.size();\n for(int i=0;i<x;i++){\n if(nums[i]==1){\n count++;\n }else{\n if(count>max){\n max=count;\n }\n count=0;\n }\n \n }\n if(count>max){\n max=count;\n }\n \n return max;\n\n\n }\n};",
"memory": "38900"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n int cnt2 = 0;\n int cnt = 0;\n for (int i = 0; i < nums.size(); i++) {\n if (cnt==0) {\n if (nums[i] == 1)\n cnt++;\n }\n else if (nums[i] == 1 && nums[i - 1] == 1)\n cnt++;\n else {\n cnt2 = max(cnt, cnt2);\n cnt = 0;\n }\n }\n cnt2 = max(cnt,cnt2);\n return cnt2;\n }\n};",
"memory": "38900"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n int l = 0;\n int r = 0;\n int ans = 0;\n int ones = 0;\n int zeros = 0;\n while(r < nums.size())\n {\n if(nums[r] == 1) ones++;\n else zeros++;\n while(zeros > 0)\n {\n if(nums[l] == 1) ones--;\n else zeros--;\n l++;\n }\n ans = max(ans, ones);\n r++;\n }\n return ans;\n }\n};\nauto init = []\n{\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n ofstream out(\"user.out\");\n cout.rdbuf(out.rdbuf());\n return 'c';\n}();",
"memory": "39000"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& arr) {\n vector<int> counter;\n \n int c = 1; \n if(arr.size() <= 1 &&arr[0]==1) return 0+1;\n if(arr.size()<=1 &&arr[0]==0) return 0;\n if(arr[0] == 0 && arr[1]==1 && arr.size() == 2)\n return 1;\n int found = 0;\n for(int i= 0;i<=arr.size()-2;i++)\n {\n if(arr[i]==1)\n {\n found = 1;\n if(arr[i]==arr[i+1])\n {\n c+=1;\n }\n else\n {\n counter.push_back(c);\n c= 1;\n \n }\n }\n }\n if(found)\n counter.push_back(c);\n int maxi =0;\n for(auto it :counter)\n {\n cout << it<<endl;\n maxi = max(maxi,it);\n } \n return maxi;\n }\n};",
"memory": "39100"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& arr) {\n vector<int> counter;\n \n int c = 1; \n if(arr.size() <= 1 &&arr[0]==1) return 0+1;\n if(arr.size()<=1 &&arr[0]==0) return 0;\n if(arr[0] == 0 && arr[1]==1 && arr.size() == 2)\n return 1;\n int found = 0;\n for(int i= 0;i<=arr.size()-2;i++)\n {\n if(arr[i]==1)\n {\n found = 1;\n if(arr[i]==arr[i+1])\n {\n c+=1;\n }\n else\n {\n counter.push_back(c);\n c= 1;\n \n }\n }\n }\n if(found)\n counter.push_back(c);\n int maxi =0;\n for(auto it :counter)\n {\n cout << it<<endl;\n maxi = max(maxi,it);\n } \n return maxi;\n }\n};",
"memory": "39100"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\nint maxValue(int a, int b) {\n return (a > b) ? a : b;\n}\n int findMaxConsecutiveOnes(vector<int>& nums) {\n \n string s = \"\";\n int size1 = 0;\n for(int i = 0 ; i<nums.size(); i++){\n if(nums[i] == 1){\n s+=nums[i];\n }else{\n size1 = maxValue(s.size(), size1);\n s = \"\";\n }\n }\n size1 = maxValue(s.size(), size1);\n\n return size1;\n }\n};",
"memory": "39200"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n vector<int> myNums;\n bool ones = false;\n for (int i = 0; i < nums.size(); i++) {\n if (nums[i] == 1) {\n if (ones) {\n myNums[myNums.size() - 1]++;\n }\n else {\n myNums.push_back(1);\n ones = true;\n }\n } else {\n ones = false;\n }\n }\n int max = 0;\n for (int i = 0; i < myNums.size(); i++) {\n if (myNums[i] > max) {max = myNums[i];}\n }\n return max;\n }\n};",
"memory": "39200"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& arr) {\n vector<int>v;\n int count=0;\n for(int i=0;i<arr.size();i++){\n if(arr[i]==1){\n count++;\n }\n if(arr[i]==0 && count !=0 ){\n v.push_back(count);\n count=0;\n }\n }\n //laste one\n if (count != 0) {\n v.push_back(count);\n }\n\n // If no 1s were found, return 0\n if (v.empty()) return 0;\n\n return *std::max_element(v.begin(), v.end());\n }\n \n};",
"memory": "39300"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n stack<int> st;\n int maxCount = 0;\n if(nums.size() <= 0) return maxCount; \n maxCount = nums[0] == 1 ? 1 : 0;\n for(int i = 1; i < nums.size(); i++){\n if(nums[i] == 0) {\n st.push(maxCount);\n maxCount = 0;\n continue;\n }\n maxCount++;\n }\n while(!st.empty()){\n auto max = st.top();\n st.pop();\n if(max > maxCount){\n maxCount = max;\n }\n }\n return maxCount;\n }\n};",
"memory": "39400"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n vector <int> v;\n int count=0;\n for(int i=0; i<nums.size(); i++){\n if((i+1)==nums.size() && nums[i]==1 ){\n count++;\n v.push_back(count);\n }\n else if(nums[i]==1){\n count++;\n }\n else{\n v.push_back(count);\n count=0;\n }\n }\n //finding maximum;\n int max = v[0];\n for(int i=1;i<v.size(); i++){\n if(v[i]>max){\n max=v[i];\n }\n }\n return max;\n }\n};",
"memory": "39500"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n vector<int> v;\n int maxCount = 0;\n for(int i = 0; i < nums.size(); i++){\n if(nums[i] == 0) {\n v.push_back(maxCount);\n maxCount = 0;\n continue;\n }\n maxCount++;\n }\n for(int i = 0; i < v.size(); i++){\n int max = v[i];\n if(max > maxCount){\n maxCount = max;\n }\n \n \n }\n return maxCount;\n }\n};",
"memory": "39600"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n vector<int> v;\n int maxCount = 0;\n for(int i = 0; i < nums.size(); i++){\n if(!nums[i]) {\n v.push_back(maxCount);\n maxCount = 0;\n continue;\n }\n maxCount++;\n }\n for(int i = 0; i < v.size(); i++){\n int max = v[i];\n if(max > maxCount){\n maxCount = max;\n } \n }\n return maxCount;\n }\n};",
"memory": "39600"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n vector<int>a;\n int count=0;\n nums.push_back(0);\n for(int i=0;i<nums.size();i++){\n if(nums[i]==1){\n count++;\n }\n else{\n a.push_back(count);\n count=0;\n }\n }\n sort(a.begin(),a.end());\n return a[a.size()-1];\n }\n};",
"memory": "40000"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n vector<int>v; int ct=0;\n for(int i=0;i<nums.size();i++){\n if(nums[i]==1){\n ct++;\n }\n if(nums[i]!=1||i==nums.size()-1){\n v.push_back(ct);\n ct=0;\n }\n }\n sort(v.begin(),v.end());\n return v[v.size()-1];\n }\n};",
"memory": "40100"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) \n {\n int n = nums.size();\n vector<int> pre(n+1,0);\n pre[1] = nums[0];\n for(int i=2;i<=n;i++)\n {\n if(nums[i-1]==1)\n pre[i] = pre[i-1]+nums[i-1];\n else\n pre[i] = 0;\n }\n int mx = 0;\n for(int i=0;i<=n;i++)\n mx = max(pre[i],mx);\n return mx; \n }\n};",
"memory": "40200"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n int n=nums.size();\n \n vector<int> b(n);\n int i,j;\n int count=0,max1=0;\n for(i=0;i<n;i++){\n if(nums[i]==1){\n count++; \n max1=max(max1,count);\n }\n else\n count=0;\n \n }\n return max1;\n }\n};",
"memory": "40300"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n int n = nums.size();\n vector<int> temp(n,0);\n\n int count;\n for(int i=0;i<n;i++){\n if(nums[i]!=0){\n count++;\n }else{\n temp[i] = count;\n count = 0;\n }\n }\n if(nums[n-1]==1){\n temp[n-1]=count;\n }\n\n int g=0;\n for(int i=0; i<n;i++){\n if(temp[i]>=g){\n g = temp[i];\n }\n }\n\n return g;\n }\n};",
"memory": "40300"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n int n=nums.size();\n \n vector<int> b(n);\n int i,j;\n int count=0,max1=0;\n for(i=0;i<n;i++){\n if(nums[i]==1)\n count++; \n else\n count=0;\n max1=max(max1,count);\n }\n return max1;\n }\n};",
"memory": "40400"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n int n=nums.size();\n \n vector<int> b(n);\n int i,j;\n int count=0,max1=0;\n for(i=0;i<n;i++){\n if(nums[i]==1){\n count++; \n max1=max(max1,count);\n }\n else{\n count=0;\n \n }\n }\n return max1;\n }\n};",
"memory": "40400"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n vector<int> places; // To store positions of zeros\n vector<int> ones; // To store counts of consecutive ones\n\n // Step 1: Find the positions of all zeros\n for (int i = 0; i < nums.size(); i++) {\n if (nums[i] == 0) {\n places.push_back(i);\n }\n }\n\n // Handle edge cases\n if (places.empty()) { // No zeros in the array\n return nums.size(); // The entire array is made up of ones\n }\n\n // Step 2: Calculate the lengths of consecutive ones between zeros\n for (int i = 0; i < places.size() - 1; i++) {\n ones.push_back(places[i + 1] - places[i] - 1);\n }\n\n // Add the number of ones before the first zero and after the last zero\n ones.push_back(places[0]); // Before the first zero\n ones.push_back(nums.size() - places.back() - 1); // After the last zero\n\n // Step 3: Find and return the maximum count of consecutive ones\n return *max_element(ones.begin(), ones.end());\n }\n};\n",
"memory": "40500"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n stack<int>st;\n int count =0;\n int n = nums.size();\n int max_count =0;\n if(nums[0]== 1)\n {\n count =1;\n }\n if(nums[0]== 1 && n==1)\n {\n return 1;\n }\n st.push(nums[0]);\n for(int i=1; i<n; i++)\n {\n if(nums[i]==1 && st.top()==1 || nums[i]== 1 && st.top()==0)\n {\n count++;\n max_count = max(max_count, count);\n }\n else if(nums[i] == 0)\n {\n max_count = max(max_count, count);\n count =0;\n }\n st.push(nums[i]);\n }\n\n return max_count;\n }\n};",
"memory": "40600"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n int n=nums.size();\n priority_queue<int> occ;\n\n int count=0;\n for(int i=0; i<n; i++){\n if(nums[i]==1){\n count++;\n }\n else if(nums[i]==0){\n occ.push(count);\n count=0;\n }\n }\n\n occ.push(count);\n\n return occ.top();\n }\n};",
"memory": "40700"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n vector<int> v1;\n vector<int> v2;\n nums.push_back(0);\n for(int i=0;i<nums.size();i++){\n if(nums[i]==1){\n v1.push_back(1);\n }\n else{\n v2.push_back(v1.size());\n v1.clear();\n }\n \n }\n return *max_element(v2.begin(),v2.end());\n\n \n }\n};",
"memory": "40800"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n vector<int> v1;\n vector<int> v2;\n nums.push_back(0);\n for(int i=0;i<nums.size();i++){\n if(nums[i]==1){\n v1.push_back(1);\n }\n else{\n v2.push_back(v1.size());\n v1.clear();\n }\n \n }\n return *max_element(v2.begin(),v2.end());\n\n \n }\n};",
"memory": "40800"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n int j=0;\n vector<int>v(nums.size(),0);\n for(int i=0;i<nums.size();i++){\n if(nums[i]==1){\n v[j]++;\n \n }else if(nums[i]!=1){\n j++;\n \n }\n\n }\n \n sort(v.begin(),v.end());\n return v[v.size()-1];\n\n }\n};",
"memory": "41400"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n vector<int>prefix(nums.size());\n prefix[0]=nums[0];\n for(int i=1;i<nums.size();i++){\n int prev=prefix[i-1];\n if(nums[i]==0)prefix[i]=0;\n else prefix[i]=nums[i]+prev;\n }\n sort(prefix.begin(),prefix.end());\n return prefix[prefix.size()-1];\n }\n};",
"memory": "41500"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "#include <bits/stdc++.h>\n#include<vector>\nclass Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {int n=nums.size();\n vector<int> vect(10);int count=0;\n for(int i=0;i<nums.size();i++){\n if(nums[i]==1){\n count++;vect.push_back(count);\n }\n else{\n count=0;\n }\n }\n return *max_element(vect.begin(),vect.end());\n }\n};",
"memory": "41600"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n int co=1;\n \n vector<int>v;\n\n for(int i=0;i<nums.size();i++){\n int count=0;\n for(int j=i;j<nums.size();j++){\n if(nums[j]==co){\n count++;\n }\n else{\n // v.push_back(count);\n i=j;\n break;\n }\n }\n v.push_back(count);\n }\n sort(v.begin(),v.end());\n return v[v.size()-1];\n }\n};",
"memory": "41700"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n int co=1;\n \n vector<int>v;\n\n for(int i=0;i<nums.size();i++){\n int count=0;\n for(int j=i;j<nums.size();j++){\n if(nums[j]==co){\n count++;\n }\n else{\n // v.push_back(count);\n i=j;\n break;\n }\n }\n v.push_back(count);\n }\n sort(v.begin(),v.end());\n return v[v.size()-1];\n }\n};",
"memory": "41800"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n int count=1;\n vector<int> count_arr;\n int max_count=0;\n for(int i=0;i<nums.size()-1;i++)\n {\n if(nums[i]==1&&nums[i+1]==1)\n {\n count++;\n count_arr.push_back(count);\n }\n else\n {\n count=1;\n }\n }\n\n if(nums.size()==1)\n {\n for(int i=0;i<nums.size();i++)\n {\n if(nums[i]==1)\n {\n return 1;\n }\n else\n {\n return 0;\n }\n }\n }\n\n if(nums.size()==2)\n {\n for(int i=0;i<nums.size()-1;i++)\n {\n if(nums[i]==0&&nums[i+1]==0)\n {\n return 0;\n }\n }\n }\n\n\n for(int i=0;i<count_arr.size();i++)\n {\n max_count=std::max(max_count,count_arr[i]);\n count=max_count;\n }\n\n return count;\n }\n};",
"memory": "42000"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int> nums) {\n int count = 0;\n int res = 0;\n nums.push_back(0);\n for (int i = 0; i < nums.size(); i++){\n if (nums[i] == 1) count ++;\n else{\n if (count > res) res = count;\n count = 0;\n }\n }\n return res;\n }\n};",
"memory": "42100"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n int count=0;\n vector<int> consec(nums.size());\n for(int i=0;i<nums.size();i++){\n if(nums[i]==1){\n count++;\n }\n else{\n consec.push_back(count);\n count=0;\n }\n }\n for(int i=0;i<consec.size();i++){\n if(consec[i]>count){\n count=consec[i];\n }\n }\n return count;\n }\n};",
"memory": "42200"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "#include <bits/stdc++.h>\n#include<vector>\nclass Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {int n=nums.size();\n vector<int> vect(2);int count=0;\n for(int i=0;i<nums.size();i++){\n if(nums[i]==1){\n count++;vect.push_back(count);\n }\n else{\n count=0;\n }\n }\n return *max_element(vect.begin(),vect.end());\n }\n};",
"memory": "42300"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n vector<int> a;\n int count=0;\n for(int i=0; i<nums.size(); i++){\n if(nums[i]==1){\n count++;\n if(nums[nums.size()-1]==1){\n a.push_back(count);\n }\n }\n if(nums[i]!=1){\n a.push_back(count);\n count=0;\n }\n \n \n }\n auto val= max_element(a.begin(), a.end());\n return *(val);\n }\n};",
"memory": "42400"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n\n\n int count = 0;\n vector<int>temp;\n\n for(int i =0 ; i<nums.size() ; i++){\n \n if(nums[i] == 1){\n count ++;\n temp.push_back(count);\n }\n else{\n count = 0;\n }\n }\n int ans = 0;\n for(int i =0 ; i<temp.size() ; i++){\n if(ans<temp[i]){\n ans = temp[i];\n }\n }\n return ans;\n }\n};",
"memory": "42500"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n vector<int> a;\n int count=0;\n for(int i=0; i<nums.size(); i++){\n if(nums[i]==1){\n count++;\n if(nums[nums.size()-1]==1){\n a.push_back(count);\n }\n }\n if(nums[i]!=1){\n a.push_back(count);\n count=0;\n }\n \n \n }\n auto val= max_element(a.begin(), a.end());\n return *(val);\n }\n};",
"memory": "42600"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n vector<int> v;\n int cnt = 0; int ans = 0;\n for(int i =0;i<nums.size();++i){\n if(nums[i] == 1){\n cnt++;\n ans = max(ans,cnt);\n v.push_back(ans);\n }\n if(nums[i] == 0){\n cnt = 0; ans = 0;\n v.push_back(0);\n }\n }\n auto maxi = max_element(v.begin(),v.end());\n return *maxi;\n }\n};",
"memory": "42800"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n // int max=INT_MIN;\n int count =0;\n int sum=0;\n vector<int> ans;\n int n= nums.size();\n for(int i=0;i<n;i++){\n if(nums[i]==1) count++;\n ans.push_back(count);\n if(nums[i]!=1) count=0;\n }\n int m=ans.size();\n int max=0;\n for(int i=0;i<m;i++){\n if(ans[i]>max) max=ans[i];\n }\n return max;\n }\n};",
"memory": "42900"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "#include <bits/stdc++.h>\n#include<vector>\nclass Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {int n=nums.size();\n vector<int> vect(n);int count=0;\n for(int i=0;i<nums.size();i++){\n if(nums[i]==1){\n count++;vect.push_back(count);\n }\n else{\n count=0;\n }\n }\n return *max_element(vect.begin(),vect.end());\n }\n};",
"memory": "42900"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "#include <bits/stdc++.h>\n#include<vector>\nclass Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {int n=nums.size();\n vector<int> vect(n);int count=0;\n for(int i=0;i<nums.size();i++){\n if(nums[i]==1){\n count++;vect.push_back(count);\n }\n else{\n count=0;\n }\n }\n return *max_element(vect.begin(),vect.end());\n }\n};",
"memory": "43000"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n int n=nums.size();\n vector <int> v;\n for(int i=0;i<n;i++){\n if(i==0){\n v.push_back(nums[0]);\n continue;\n }\n if(nums[i]==0){\n v.push_back(0);\n }\n if(nums[i]==1){\n v.push_back(v[i-1]+1);\n }\n }\n int max=v[0];\n for(int i=0;i<n;i++){\n if(v[i]>max){\n max=v[i];\n }\n\n }\n return max;\n\n }\n};",
"memory": "43100"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n // int max=INT_MIN;\n int count =0;\n int sum=0;\n vector<int> ans;\n int n= nums.size();\n for(int i=0;i<n;i++){\n if(nums[i]==1) count++;\n ans.push_back(count);\n if(nums[i]!=1) count=0;\n }\n int m=ans.size();\n int max=0;\n for(int i=0;i<m;i++){\n if(ans[i]>max) max=ans[i];\n }\n return max;\n }\n};",
"memory": "43200"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n vector <int> s;\n int count =0;\n int m=0;\n for(int x=0;x<nums.size();x++){\n \n if(nums[x]==1){\n count+=1;\n }\n else {\n s.push_back(count);\n count =0;\n }\n s.push_back(count);\n } \n m = *max_element(s.begin(),s.end());\n return m; \n }\n};",
"memory": "43300"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n int count = 0;\n int i = 0;\n vector<int> ans;\n while (i < nums.size()) {\n if(nums[i]==1)\n {\n count++;\n ans.push_back(count);\n }\n else{\n ans.push_back(count);\n count=0;\n }\n i++;\n }\n sort(ans.begin(),ans.end());\n return ans[ans.size()-1];\n }\n};",
"memory": "44200"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n int count=0;\n vector<int>ans;\n for(int i=0;i<nums.size();i++){\n if(nums[i]==1){\n count++;\n }\n else{\n count=0;\n }\n ans.push_back(count);\n }\n sort(ans.begin(),ans.end());\n\n return ans[ans.size()-1];\n }\n};",
"memory": "44300"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n int l=nums.size();int c=0; vector<int>a; \n for(int i=0;i<l;i++){\n if (nums[i]==0){\n \n c=0;\n }\n else{\n c++; \n }\n a.push_back(c);\n }\n int x=a.size();\n sort(a.begin(),a.end());\n return a[x-1]; \n }\n};",
"memory": "44400"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n vector<int>ans;\n int count=0;\n for(int i=0;i<nums.size();i++){\n if(nums[i]==1){\n count++;\n }\n else if(nums[i]==0){\n ans.push_back(count);\n count=0; \n }\n ans.push_back(count);\n }\n sort(ans.begin(),ans.end());\n return ans[ans.size()-1];\n }\n};",
"memory": "44500"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\nint maxNo(vector<int> freq)\n{\n int maxno=INT_MIN;\n for(int i=0;i<freq.size();i++)\n maxno=max(maxno,freq[i]);\n return maxno;\n}\n int findMaxConsecutiveOnes(vector<int>& nums) {\n vector<int> freq;\n int c=0;\n for(int i=0;i<nums.size();i++)\n {\n if(nums[i]==1)\n {\n c++;\n }\n else\n c=0;\n\n freq.push_back(c);\n }\n\n int maxno=maxNo(freq);\n return maxno;\n }\n};",
"memory": "44600"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n int count =0;\n set<int, greater<int>> maxr;\n for(int i : nums){\n if(i==1){\n count+=1;\n }\n else{\n maxr.insert(count);\n count=0;\n }\n maxr.insert(count);\n }\n return *maxr.begin();\n }\n};",
"memory": "45400"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n int n = nums.size(),cnt = 0;\n set<int>temp;\n for(int i = 0; i<n; i++){\n if(nums[i] == 1){\n cnt++;\n temp.insert(cnt);\n }else if(nums[i]==0){\n temp.insert(cnt);\n cnt = 0;\n }\n }\n int max = INT_MIN;\n for(auto it : temp){\n if(it>max){\n max = it;\n }\n }\n return max;\n }\n};",
"memory": "45600"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n int n = nums.size() , b=0;\n vector<int>freq(n,0);\n for(int i=0; i<n; i++){\n if(nums[i]!=0){\n b = b+1;\n }\n if(nums[i]==0){\n freq.push_back(b);\n b=0;\n }\n freq.push_back(b);\n }\n return *max_element(freq.begin(), freq.end());\n }\n};",
"memory": "46500"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n int max=0;\n int currmax=0;\n for(int i=0;i<nums.size();i++)\n {\n if(nums[i]==1)\n {\n currmax++;\n }\n if(nums[i]==0)\n {\n if(currmax>max)\n {\n max=currmax;\n }\n\n currmax=0;\n }\n\n \n }\n if(currmax>max)\n {\n max=currmax;\n }\n\n return max;\n }\n};",
"memory": "48700"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n int maxi = 0;//contains the value of consecutive no's of 1\n int count=0;//count the no.'s of 1 consecutively and reset to 0 when 0 come\n for(int i=0;i<nums.size();i++){\n //\n if(nums[i]==1){\n count++;//lineraly increase the value when 1 comes\n }else{\n maxi = max(maxi,count);//store the value of max no of 1 in consecutively order\n count = 0;//suddenly count = 0 , \n }\n maxi = max(maxi,count);\n }\n\n\n \n return maxi;//himanshukansal101\n }\n};",
"memory": "48800"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n int maxi = 0;\n int count = 0;\n for(int i=0;i<nums.size();i++)\n {\n if(nums[i]==1)\n {\n count++;\n maxi = max(maxi,count);\n }\n else\n {\n count = 0;\n }\n }\n return maxi;\n }\n};",
"memory": "48800"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n \n int n=nums.size();\n int count=0;\n int max_count=0;\n\n for(int i=0;i<n;i++)\n {\n if(nums[i] == 1)\n count++;\n else\n count =0;\n\n if(count >max_count)\n max_count=count; \n }\n\n return max_count;\n }\n\n \n};",
"memory": "48900"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n \n int maxi = 0 ;\n int cons = 0 ;\n\n for(int i = 0 ; i<nums.size() ; i++){\n\n if(nums[i] == 0){\n maxi = max(maxi,cons) ;\n cons = 0 ;\n }\n\n else{\n cons++ ;\n maxi = max(maxi,cons) ;\n }\n\n }\n\n \n\n return maxi ;\n\n }\n};",
"memory": "48900"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n int maxCount = 0, currentCount = 0;\n \n for (int num : nums) {\n if (num == 1) {\n currentCount++;\n maxCount = max(maxCount, currentCount);\n } else {\n currentCount = 0;\n }\n }\n \n return maxCount;\n\n\n \n }\n};",
"memory": "49000"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n int n=nums.size();\n int count=0;\n int maxcount=0;\n for(int i =0 ; i<n;i++){\n if(nums[i] ==1){\n count++ ;\n maxcount = max(maxcount,count);\n }else{\n count=0;\n }\n }\n return maxcount;\n \n }\n};",
"memory": "49000"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n int mx=0;\n int cnt=0;\n for(int i=0;i<nums.size();i++)\n {\n if(i==0&&nums[i]==1)\n cnt++;\n if(i!=0&&nums[i]==1&&nums[i-1]==1)\n cnt++;\n else if(i!=0&&nums[i]==1&&nums[i-1]!=1)\n cnt=1;\n else if(i!=0&&nums[i]!=1)\n cnt=0;\n mx=max(mx,cnt);\n }\n return mx;\n }\n};",
"memory": "49100"
} |
485 | <p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxConsecutiveOnes(vector<int>& nums) {\n int maxCount = 0; \n int currentCount = 0; \n \n for (int num : nums) {\n if (num == 1) {\n currentCount++; \n } else {\n maxCount = max(maxCount, currentCount);\n currentCount = 0; \n }\n }\n \n \n return max(maxCount, currentCount);\n }\n};\n",
"memory": "49100"
} |
486 | <p>You are given an integer array <code>nums</code>. Two players are playing a game with this array: player 1 and player 2.</p>
<p>Player 1 and player 2 take turns, with player 1 starting first. Both players start the game with a score of <code>0</code>. At each turn, the player takes one of the numbers from either end of the array (i.e., <code>nums[0]</code> or <code>nums[nums.length - 1]</code>) which reduces the size of the array by <code>1</code>. The player adds the chosen number to their score. The game ends when there are no more elements in the array.</p>
<p>Return <code>true</code> if Player 1 can win the game. If the scores of both players are equal, then player 1 is still the winner, and you should also return <code>true</code>. You may assume that both players are playing optimally.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> Initially, player 1 can choose between 1 and 2.
If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2).
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
Hence, player 1 will never be the winner and you need to return false.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,233,7]
<strong>Output:</strong> true
<strong>Explanation:</strong> Player 1 first chooses 1. Then player 2 has to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 20</code></li>
<li><code>0 <= nums[i] <= 10<sup>7</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool predictTheWinner(vector<int>& nums) {\n\n int total = 0;\n for(auto it:nums) total += it;\n\n int s1 = firstScore(0, nums.size()-1, 0, nums);\n return s1 >= total - s1;\n }\n\n int firstScore(int i, int j, int t, vector<int>& nums) {\n if(i>j) return 0;\n int s1, s2, val;\n if (t == 0) {\n s1 = nums[i] + firstScore(i+1, j, 1, nums);\n s2 = nums[j] + firstScore(i, j-1, 1, nums);\n val = max(s1, s2);\n } else {\n s1 = firstScore(i+1, j, 0, nums);\n s2 = firstScore(i, j-1, 0, nums);\n val = min(s1, s2);\n }\n return val;\n }\n\n};",
"memory": "8700"
} |
486 | <p>You are given an integer array <code>nums</code>. Two players are playing a game with this array: player 1 and player 2.</p>
<p>Player 1 and player 2 take turns, with player 1 starting first. Both players start the game with a score of <code>0</code>. At each turn, the player takes one of the numbers from either end of the array (i.e., <code>nums[0]</code> or <code>nums[nums.length - 1]</code>) which reduces the size of the array by <code>1</code>. The player adds the chosen number to their score. The game ends when there are no more elements in the array.</p>
<p>Return <code>true</code> if Player 1 can win the game. If the scores of both players are equal, then player 1 is still the winner, and you should also return <code>true</code>. You may assume that both players are playing optimally.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> Initially, player 1 can choose between 1 and 2.
If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2).
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
Hence, player 1 will never be the winner and you need to return false.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,233,7]
<strong>Output:</strong> true
<strong>Explanation:</strong> Player 1 first chooses 1. Then player 2 has to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 20</code></li>
<li><code>0 <= nums[i] <= 10<sup>7</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\nint solve(int i,int j,vector<int> & nums){\n if(i==j) return nums[i];\n if(i>j) return 0;\n\n int take_i=nums[i]+min(solve(i+2,j,nums),solve(i+1,j-1,nums));\n int take_j=nums[j]+min(solve(i,j-2,nums),solve(i+1,j-1,nums));\n return max(take_i,take_j);\n}\n bool predictTheWinner(vector<int>& nums) {\n int sum=accumulate(begin(nums),end(nums),0);\n int player1=solve(0,nums.size()-1,nums);\n int player2=sum-player1;\n\n if(player1>=player2) return true;\n return false;\n }\n};",
"memory": "8800"
} |
486 | <p>You are given an integer array <code>nums</code>. Two players are playing a game with this array: player 1 and player 2.</p>
<p>Player 1 and player 2 take turns, with player 1 starting first. Both players start the game with a score of <code>0</code>. At each turn, the player takes one of the numbers from either end of the array (i.e., <code>nums[0]</code> or <code>nums[nums.length - 1]</code>) which reduces the size of the array by <code>1</code>. The player adds the chosen number to their score. The game ends when there are no more elements in the array.</p>
<p>Return <code>true</code> if Player 1 can win the game. If the scores of both players are equal, then player 1 is still the winner, and you should also return <code>true</code>. You may assume that both players are playing optimally.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> Initially, player 1 can choose between 1 and 2.
If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2).
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
Hence, player 1 will never be the winner and you need to return false.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,233,7]
<strong>Output:</strong> true
<strong>Explanation:</strong> Player 1 first chooses 1. Then player 2 has to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 20</code></li>
<li><code>0 <= nums[i] <= 10<sup>7</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int first(int left, int right, vector<int>& nums) {\n if (left == right)\n return nums[left];\n return max(nums[left] + slow(left + 1, right, nums),\n nums[right] + slow(left, right - 1, nums));\n }\n\n int slow(int left, int right, vector<int>& nums) {\n if (left == right)\n return 0;\n return min(first(left + 1, right, nums), first(left, right - 1, nums));\n } \n bool predictTheWinner(vector<int>& nums) {\n int sum = accumulate(nums.begin(), nums.end(), 0);\n if (first(0, nums.size() - 1, nums) >= sum - first(0, nums.size() - 1, nums))\n return true;\n else\n return false;\n }\n};",
"memory": "8900"
} |
486 | <p>You are given an integer array <code>nums</code>. Two players are playing a game with this array: player 1 and player 2.</p>
<p>Player 1 and player 2 take turns, with player 1 starting first. Both players start the game with a score of <code>0</code>. At each turn, the player takes one of the numbers from either end of the array (i.e., <code>nums[0]</code> or <code>nums[nums.length - 1]</code>) which reduces the size of the array by <code>1</code>. The player adds the chosen number to their score. The game ends when there are no more elements in the array.</p>
<p>Return <code>true</code> if Player 1 can win the game. If the scores of both players are equal, then player 1 is still the winner, and you should also return <code>true</code>. You may assume that both players are playing optimally.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> Initially, player 1 can choose between 1 and 2.
If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2).
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
Hence, player 1 will never be the winner and you need to return false.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,233,7]
<strong>Output:</strong> true
<strong>Explanation:</strong> Player 1 first chooses 1. Then player 2 has to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 20</code></li>
<li><code>0 <= nums[i] <= 10<sup>7</sup></code></li>
</ul>
| 0 | {
"code": "class Solution{\npublic:\n // Create a Recursive 'Solve()' function which will return true if the score of 'player1' is greater than or equal to the\n // score of 'player2' \n bool Solve(vector<int> &nums, int i, int j, int score1, int score2, bool flag){\n // BASE CASE: If we finish selecting all the elements of the I/p Array nums, then return true of 'player1' has\n // more or equal score than 'player2' \n if(i > j) return score1 >= score2;\n\n // If 'player1' has the current turn, then he has 2 choices: \n // Choice 1: Add the 'i'th element to his score and ask Recursion to do the rest of the job by passing 'flag = false'\n // because in next turn, 'player2' will have the turn\n // Choice 2: Add 'j'th element to his score and ask recursion to do remaining task \n if(flag){\n bool choice1 = Solve(nums, i + 1, j, score1 + nums[i], score2, false); // since we selected 'i'th, go for 'i+1'th\n bool choice2 = Solve(nums, i, j - 1, score1 + nums[j], score2, false); // since we selected 'j'th, next is 'j-1'th\n \n // If 'player1' can win in any 1 of the above case, return true.\n return (choice1 || choice2); \n }\n else{\n // Similarly, 'player2' will also have 2 choices as 'player1\n bool choice1 = Solve(nums, i + 1, j, score1, score2 + nums[i], true); // Add 'i'th element \n bool choice2 = Solve(nums, i, j - 1, score1, score2 + nums[j], true); // Add 'j'th element \n \n // If 'player2' wins in both the 2 cases, he will be the final winner\n return (choice1 && choice2);\n }\n return 0;\n }\n\n bool predictTheWinner(vector<int>& nums){\n // Approach: RECURSION \n // Self Solved \n // TC : O(2 ^ n) \n // SC : O(2 ^ n)\n // C++ CODE: \n int score1 = 0; // to stroe the maximum score 'player1' can get \n int score2 = 0; // to store the maximum score 'player2' can get \n int i = 0; // start of the I/p Array \n int j = nums.size() - 1; // end of the I/p Array \n bool flag = true; // Initially, 'player1' has the turn to select either the 'i'th or 'j'th element \n\n bool canPlayer1Win = Solve(nums, i, j, score1, score2, flag); // Ask Recursion if 'player1' can win or not\n\n return (canPlayer1Win ? true : false);\n }\n};",
"memory": "8900"
} |
486 | <p>You are given an integer array <code>nums</code>. Two players are playing a game with this array: player 1 and player 2.</p>
<p>Player 1 and player 2 take turns, with player 1 starting first. Both players start the game with a score of <code>0</code>. At each turn, the player takes one of the numbers from either end of the array (i.e., <code>nums[0]</code> or <code>nums[nums.length - 1]</code>) which reduces the size of the array by <code>1</code>. The player adds the chosen number to their score. The game ends when there are no more elements in the array.</p>
<p>Return <code>true</code> if Player 1 can win the game. If the scores of both players are equal, then player 1 is still the winner, and you should also return <code>true</code>. You may assume that both players are playing optimally.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> Initially, player 1 can choose between 1 and 2.
If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2).
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
Hence, player 1 will never be the winner and you need to return false.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,233,7]
<strong>Output:</strong> true
<strong>Explanation:</strong> Player 1 first chooses 1. Then player 2 has to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 20</code></li>
<li><code>0 <= nums[i] <= 10<sup>7</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int first(int left, int right, vector<int>& nums) {\n if (left == right)\n return nums[left];\n return max(nums[left] + slow(left + 1, right, nums),\n nums[right] + slow(left, right - 1, nums));\n }\n\n int slow(int left, int right, vector<int>& nums) {\n if (left == right)\n return 0;\n return min(first(left + 1, right, nums), first(left, right - 1, nums));\n } \n bool predictTheWinner(vector<int>& nums) {\n int sum = accumulate(nums.begin(), nums.end(), 0);\n if (first(0, nums.size() - 1, nums) >= sum - first(0, nums.size() - 1, nums))\n return true;\n else\n return false;\n }\n};",
"memory": "9000"
} |
486 | <p>You are given an integer array <code>nums</code>. Two players are playing a game with this array: player 1 and player 2.</p>
<p>Player 1 and player 2 take turns, with player 1 starting first. Both players start the game with a score of <code>0</code>. At each turn, the player takes one of the numbers from either end of the array (i.e., <code>nums[0]</code> or <code>nums[nums.length - 1]</code>) which reduces the size of the array by <code>1</code>. The player adds the chosen number to their score. The game ends when there are no more elements in the array.</p>
<p>Return <code>true</code> if Player 1 can win the game. If the scores of both players are equal, then player 1 is still the winner, and you should also return <code>true</code>. You may assume that both players are playing optimally.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> Initially, player 1 can choose between 1 and 2.
If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2).
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
Hence, player 1 will never be the winner and you need to return false.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,233,7]
<strong>Output:</strong> true
<strong>Explanation:</strong> Player 1 first chooses 1. Then player 2 has to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 20</code></li>
<li><code>0 <= nums[i] <= 10<sup>7</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\nprivate:\n int helper(vector<int>& nums, int l, int r) {\n if (l == r) {\n return nums[l];\n }\n\n int pickLeft = nums[l] - helper(nums, l + 1, r);\n int pickRight = nums[r] - helper(nums, l, r - 1);\n\n return max(pickLeft, pickRight);\n }\n\npublic:\n bool predictTheWinner(vector<int>& nums) {\n int n = nums.size();\n // If the result is non-negative, Player 1 can either win or tie\n return helper(nums, 0, n - 1) >= 0;\n }\n};\n",
"memory": "9000"
} |
486 | <p>You are given an integer array <code>nums</code>. Two players are playing a game with this array: player 1 and player 2.</p>
<p>Player 1 and player 2 take turns, with player 1 starting first. Both players start the game with a score of <code>0</code>. At each turn, the player takes one of the numbers from either end of the array (i.e., <code>nums[0]</code> or <code>nums[nums.length - 1]</code>) which reduces the size of the array by <code>1</code>. The player adds the chosen number to their score. The game ends when there are no more elements in the array.</p>
<p>Return <code>true</code> if Player 1 can win the game. If the scores of both players are equal, then player 1 is still the winner, and you should also return <code>true</code>. You may assume that both players are playing optimally.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> Initially, player 1 can choose between 1 and 2.
If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2).
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
Hence, player 1 will never be the winner and you need to return false.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,233,7]
<strong>Output:</strong> true
<strong>Explanation:</strong> Player 1 first chooses 1. Then player 2 has to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 20</code></li>
<li><code>0 <= nums[i] <= 10<sup>7</sup></code></li>
</ul>
| 1 | {
"code": "#include <vector>\nusing namespace std;\n\nclass Solution {\npublic:\n bool predictTheWinner(vector<int>& nums) {\n int n = nums.size();\n vector<vector<int>> dp(n, vector<int>(n, 0));\n\n for (int i = 0; i < n; ++i) {\n dp[i][i] = nums[i];\n }\n\n for (int len = 1; len < n; ++len) {\n for (int i = 0; i < n - len; ++i) {\n int j = i + len;\n dp[i][j] = max(nums[i] - dp[i + 1][j], nums[j] - dp[i][j - 1]);\n }\n }\n\n return dp[0][n - 1] >= 0;\n }\n};",
"memory": "9100"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.