id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
0
{ "code": "class Solution {\npublic:\n string getBinary(int num){\n string ans;\n while(num){\n ans+= num%2 ? '1' : '0';\n num/=2;\n }\n string a;\n for(int i=ans.size()-1;i>=0;i--){\n a+=ans[i];\n }\n return a;\n }\n string convertDateToBinary(string date) {\n \n int num = 0;\n string ans;\n for(int i=0;i<date.size();i++){\n if(date[i]=='-'){\n ans+=getBinary(num);\n // cout<<ans<<endl;\n num = 0;\n ans+='-';\n }\n else{\n int digit = (int)date[i] - '0';\n // cout<<digit<<endl;\n num = num*10 + digit;\n }\n }\n \n ans+=getBinary(num);\n return ans;\n }\n};", "memory": "8000" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
0
{ "code": "class Solution {\npublic:\n string toBinary(int n)\n {\n string str = \"\";\n while (n > 0){\n str += (n&1)+'0';\n n >>= 1;\n }\n reverse(str.begin(),str.end());\n return str;\n }\n string convertDateToBinary(string date)\n {\n int year = (date[0]-'0')*1000 + (date[1]-'0')*100 + (date[2]-'0')*10 + (date[3]-'0');\n int month = (date[5]-'0')*10 + (date[6]-'0');\n int day = (date[8]-'0')*10 + (date[9]-'0');\n return toBinary(year) + \"-\" + toBinary(month) + \"-\" + toBinary(day);\n }\n};", "memory": "8000" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
0
{ "code": "class Solution {\npublic:\n string convertDateToBinary(string d) {\n string tmp=\"\";\n for(int i=0;i<4;i++) tmp.push_back(d[i]);\n string ans=\"\";\n\n int a=stoi(tmp);\n int ps=0;\n\n for(int i=15;i>=0;i--){\n if((a >> i) & 1){\n ps=1; ans.push_back('1');\n }\n else if(ps) ans.push_back('0');\n }\n ans.push_back('-');\n tmp=\"\";\n for(int i=5;i<7;i++) tmp.push_back(d[i]);\n\n a=stoi(tmp);\n ps=0;\n\n for(int i=15;i>=0;i--){\n if((a >> i) & 1){\n ps=1; ans.push_back('1');\n }\n else if(ps) ans.push_back('0');\n }\n ans.push_back('-');\n\n tmp=\"\";\n for(int i=8;i<10;i++) tmp.push_back(d[i]);\n\n a=stoi(tmp);\n ps=0;\n\n for(int i=15;i>=0;i--){\n if((a >> i) & 1){\n ps=1; ans.push_back('1');\n }\n else if(ps) ans.push_back('0');\n }\n\n return ans;\n }\n};", "memory": "8100" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
0
{ "code": "class Solution {\npublic:\n string intToBin(int num){\n string s=\"\";\n while(num){\n s+=(num%2+'0');\n num=num/2;\n }\n reverse(s.begin(), s.end());\n return s;\n }\n string convertDateToBinary(string date) {\n int year=0;\n for(int i=0; i<4; i++){\n year=year*10+(date[i]-'0');\n }\n int month=0;\n for(int i=5; i<7; i++){\n month=month*10+(date[i]-'0');\n }\n int day=0;\n for(int i=8; i<10; i++){\n day=day*10+(date[i]-'0');\n }\n string ans=intToBin(year)+'-'+intToBin(month)+'-'+intToBin(day);\n return ans;\n \n }\n};", "memory": "8100" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
0
{ "code": "class Solution {\npublic:\n string convertDateToBinary(string date) {\n int year = (date[0]-48)*1000 + (date[1]-48)*100 + (date[2]-48)*10 + (date[3]-48);\n int month = (date[5]-48)*10 + (date[6]-48);\n int day = (date[8]-48)*10 + (date[9]-48);\n return convert(year)+\"-\"+convert(month)+\"-\"+convert(day);\n }\nstring convert(int num){\n string ans=\"\";\n while(num){\n if(num&1){\n ans+=\"1\";\n } else {\n ans+=\"0\";\n }\n num=num>>1;\n }\n reverse(ans.begin(),ans.end());\n return ans;\n }\n};", "memory": "8200" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
0
{ "code": "class Solution {\npublic:\n string convertDateToBinary(string date) {\n int y=0, m=0, d=0;\n sscanf(date.c_str(), \"%d-%d-%d\", &y, &m, &d);\n const auto itob = [](int x) -> std::string {\n std::string res = \"\";\n int dig = 0;\n while ((x >> dig) > 1) dig++;\n while (dig >= 0) res += '0' + ((x >> dig--) & 1);\n return res;\n };\n return itob(y) + \"-\" + itob(m) + \"-\" + itob(d);\n }\n};", "memory": "8200" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
0
{ "code": "class Solution {\npublic:\n static string to_binary(int x){\n int bit=31-__builtin_clz(x);\n string ans=\"1\";\n for(int i=bit-1; i>=0; i--){\n ans+=(x & (1<<i))?'1':'0';\n }\n return ans;\n }\n static string convertDateToBinary(string& date) {\n stringstream ss(date);\n int yy, mm, dd;\n char c;\n ss>>yy>>c>>mm>>c>>dd;\n return to_binary(yy)+\"-\"+to_binary(mm)+\"-\"+to_binary(dd);\n }\n};", "memory": "8300" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
0
{ "code": "class Solution {\npublic:\n string convertBinary(string data) {\n int converted = 0;\n for (auto it : data) {\n converted = converted * 10 + (it - '0');\n }\n string answer = \"\";\n while (converted) {\n if (converted % 2)\n answer += '1';\n else\n answer += '0';\n converted /= 2;\n }\n \n reverse(answer.begin(), answer.end());\n while (answer[0] == '0' && answer.size() > 1)\n answer.erase(0, answer.length() - 1);\n return answer;\n }\n string convertDateToBinary(string date) {\n date += '-';\n string answer = \"\";\n string current = \"\";\n for (auto it : date) {\n if (it == '-') {\n answer += convertBinary(current);\n current = \"\";\n answer += '-';\n continue;\n }\n current += it;\n }\n\n return answer.substr(0, answer.length() - 1);\n }\n};", "memory": "8300" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
0
{ "code": "class Solution {\npublic:\n static string to_binary(int x) {\n int bit = 31 - __builtin_clz(x);\n string ans = \"1\";\n for (int i = bit - 1; i >= 0; i--) {\n ans += (x & (1 << i)) ? '1' : '0';\n }\n return ans;\n }\n static string convertDateToBinary(string& date) {\n stringstream ss(date);\n int yy, mm, dd;\n char c;\n ss >> yy >> c >> mm >> c >> dd;\n return to_binary(yy) + \"-\" + to_binary(mm) + \"-\" + to_binary(dd);\n }\n};", "memory": "8400" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
0
{ "code": "class Solution {\npublic:\n static string to_binary(int x) {\n int bit = 31 - __builtin_clz(x);\n string ans = \"1\";\n for (int i = bit - 1; i >= 0; i--) {\n ans += (x & (1 << i)) ? '1' : '0';\n }\n return ans;\n }\n static string convertDateToBinary(string& date) {\n stringstream ss(date);\n int yy, mm, dd;\n char c;\n ss >> yy >> c >> mm >> c >> dd;\n return to_binary(yy) + \"-\" + to_binary(mm) + \"-\" + to_binary(dd);\n }\n};", "memory": "8400" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
0
{ "code": "class Solution {\npublic:\n string convertDateToBinary(string date) {\n int year = stoi(date.substr(0, 4));\n int month = stoi(date.substr(5, 2));\n int day = stoi(date.substr(8, 2));\n string y_str = \"\";\n string m_str = \"\";\n string d_str = \"\";\n while (year > 0) {\n y_str.push_back(year % 2 + '0');\n year >>= 1;\n }\n while (month > 0) {\n m_str.push_back(month % 2 + '0');\n month >>= 1;\n }\n while (day > 0) {\n d_str.push_back(day % 2 + '0');\n day >>= 1;\n }\n string result = \"\";\n reverse(y_str.begin(), y_str.end());\n reverse(m_str.begin(), m_str.end());\n reverse(d_str.begin(), d_str.end());\n result += y_str;\n result.push_back('-');\n result += m_str;\n result.push_back('-');\n result += d_str;\n return result;\n }\n};", "memory": "8500" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
0
{ "code": "class Solution {\npublic:\n string convertDateToBinary(string date) {\n string str,str1,str2;\n for(int i=0;i<date.size();i++){\n if(date[i]!='-'){\n str+=date[i];\n }\n else{\n int val=stoi(str);\n bitset<16>decimal(val);\n str1=decimal.to_string();\n str1 = str1.substr(str1.find('1'));\n str2+=str1+'-';\n cout<<str2<<endl;\n str.erase(0);\n //str1.erase(0);\n }\n }\n int val=stoi(str);\n bitset<5>decimal(val);\n str1=decimal.to_string();\n str1 = str1.substr(str1.find('1'));\n str2+=str1;\n return str2;\n }\n};", "memory": "8500" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
0
{ "code": "class Solution {\npublic:\nstring f (int a){\n string x = \"\";\n int size = 0 ;\n while(a>0){\n if(a%2==0){\n x += '0';\n }else{\n x += '1';\n }\n a = a/2 ;\n size ++ ;\n }\n int l = 0 , r = size - 1 ;\n while(l<=r){\n char c = x[l];\n x[l] = x[r] ;\n x[r] = c ;\n l++ ;\n r-- ;\n }\n return x ;\n}\n string convertDateToBinary(string date) {\n string a = \"\";\n for(int i=0 ; i<4;i++){\n a+=date[i];\n }\n string b = \"\";\n for(int i=5 ;i<7 ;i++){\n b += date[i];\n }\n string c = \"\";\n for(int i=8 ; i<10 ; i++){\n c += date[i];\n }\n int a1 = stoi(a);\n int b1 = stoi(b);\n int c1 = stoi(c);\n string ans = \"\";\n ans += f(a1);\n ans += '-' ;\n ans += f(b1);\n ans += '-' ;\n ans += f(c1);\n return ans ;\n }\n};", "memory": "8600" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
0
{ "code": "class Solution {\nprivate:\n static std::string ToBinary(int num) {\n std::string bin;\n bool bNonZero = false;\n for (int i = 8*sizeof(num)-1; i >= 0; --i) {\n if (!(num & (1 << i))) {\n if (bNonZero) {\n bin.push_back('0');\n }\n } else {\n bin.push_back('1');\n bNonZero = true;\n }\n }\n return bin;\n }\n\npublic:\n std::string convertDateToBinary(const std::string& date) {\n int yyyy = stoi(date.substr(0, 4));\n int mm = stoi(date.substr(5, 2));\n int dd = stoi(date.substr(8, 2));\n return ToBinary(yyyy) + \"-\" + ToBinary(mm) + \"-\" + ToBinary(dd);\n }\n};", "memory": "8600" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
0
{ "code": "class Solution {\npublic:\n string DecimalToBinary(int num)\n {\n string str;\n while(num){\n if(num & 1)\n str+='1';\n else\n str+='0';\n num>>=1; \n } \n reverse(str.begin(), str.end());\n return str;\n }\n string convertDateToBinary(string date) {\n int year = stoi(date.substr(0, 4));\n int month = stoi(date.substr(5, 2));\n int day = stoi(date.substr(8, 2));\n return DecimalToBinary(year) + \"-\" + DecimalToBinary(month) + '-' + DecimalToBinary(day);\n }\n};", "memory": "8700" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
0
{ "code": "class Solution {\npublic:\n string convertDateToBinary(string date) {\n string str,str1,str2;\n for(int i=0;i<date.size();i++){\n if(date[i]!='-'){\n str+=date[i];\n }\n else{\n int val=stoi(str);\n bitset<16>decimal(val);\n str1=decimal.to_string();\n str1 = str1.substr(str1.find('1'));\n str2+=str1+'-';\n cout<<str2<<endl;\n str.erase(0);\n //str1.erase(0);\n }\n }\n int val=stoi(str);\n bitset<5>decimal(val);\n str1=decimal.to_string();\n str1 = str1.substr(str1.find('1'));\n str2+=str1;\n return str2;\n }\n};", "memory": "8700" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
1
{ "code": "class Solution {\npublic:\n string stringtobin(int &s){\n string binary = bitset<32>(s).to_string();\n int i=binary.find('1');\n return binary.substr(i);\n }\n string convertDateToBinary(string &date) {\n int year=stoi(date.substr(0,4));\n int month=stoi(date.substr(5,2));\n int dates=stoi(date.substr(8,2));\n string ans=\"\";\n ans+=stringtobin(year);\n ans+='-';\n ans+=stringtobin(month);\n ans+='-';\n ans+=stringtobin(dates);\n \n return ans;\n }\n};", "memory": "8800" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
1
{ "code": "class Solution {\npublic:\n string solve(int a) {\n string ans = \"\";\n while (a) {\n if (a % 2 == 1) {\n ans += '1';\n a /= 2;\n } else {\n ans += '0';\n a /= 2;\n }\n }\n reverse(ans.begin(),ans.end());\n return ans;\n }\n string convertDateToBinary(string date) {\n string y = date.substr(0, 4);\n string m = date.substr(5, 2);\n string d = date.substr(8, 2);\n cout << y << \" \" << m << \" \" << d << endl;\n int yy = stoi(y);\n int mm = stoi(m);\n int dd = stoi(d);\n\n return solve(yy) + '-' + solve(mm) + '-' +solve(dd);\n }\n};", "memory": "8800" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<int> convert(string s){\n vector<int> ans;\n string temp;\n\n for(char c:s){\n if(c=='-'){\n int val=stoi(temp);\n ans.push_back(val);\n temp=\"\";\n }\n else{\n temp.push_back(c);\n }\n }\n if(!temp.empty()){\n ans.push_back(stoi(temp));\n }\n return ans;\n }\n\n string to_binary(int n){\n string ans;\n\n while(n>0){\n int last=n%2;\n ans.push_back(last+'0');\n n/=2;\n }\n reverse(ans.begin(),ans.end());\n return ans;\n }\n\n\n\n string convertDateToBinary(string date) {\n vector<int> check=convert(date);\n vector<string> c2;\n for(int i:check){\n c2.push_back(to_binary(i));\n }\n\n string ans;\n\n for(int i=0;i<c2.size();i++){\n if(i==0){\n ans+=c2[i];\n }\n else{\n ans.push_back('-');\n ans+=c2[i];\n }\n }\n\n return ans;\n }\n};", "memory": "8900" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
1
{ "code": "class Solution {\npublic:\n string to_binary(string dt){\n string temp=\"\";\n int d=stoi(dt);\n while(d){\n if(d%2==1) temp+='1';\n else temp+='0';\n d=d/2;\n }\n reverse(temp.begin(),temp.end());\n return temp;\n }\n string convertDateToBinary(string date) {\n string year=date.substr(0,4);\n string month=date.substr(5,2);\n string day=date.substr(8,2);\n\n string yearbinary=to_binary(year);\n string monthbinary=to_binary(month);\n string daybinary=to_binary(day);\n\n return yearbinary+\"-\"+monthbinary+\"-\"+daybinary;\n }\n};", "memory": "8900" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
1
{ "code": "class Solution {\npublic:\n string convertDateToBinary(string date) {\n int y=stoi(date.substr(0,4));\n string year = bitset<32>(y).to_string();\n int i=0;\n while(year[i]=='0')\n {\n i++;\n }\n year=year.substr(i);\n int m=stoi(date.substr(5,2));\n string month = bitset<32>(m).to_string();\n i=0;\n while(month[i]=='0')\n {\n i++;\n }\n month=month.substr(i);\n int d=stoi(date.substr(8));\n string date1 = bitset<32>(d).to_string();\n i=0;\n while(date1[i]=='0')\n {\n i++;\n }\n date1=date1.substr(i);\n return year+\"-\"+month+\"-\"+date1;\n }\n};", "memory": "9000" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
1
{ "code": "class Solution {\npublic:\n string helper(string temp) {\n int n;\n stringstream ss;\n ss << temp;\n ss >> n;\n string s;\n while(n > 0) {\n int rem = n%2;\n n = n/2;\n s += rem+'0';\n }\n reverse(s.begin(),s.end());\n return s;\n }\n string convertDateToBinary(string date) {\n string ans;\n string temp;\n for(int i =0;i < date.size();i++) {\n if(date[i] != '-') \n temp += date[i];\n else {\n temp = helper(temp);\n ans+=temp;\n ans+='-';\n temp = \"\";\n }\n }\n ans+=helper(temp);\n return ans;\n }\n};", "memory": "9100" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
1
{ "code": "class Solution {\n string convert(int a){\n string s;\n while(a){\n s=((a%2==1)?'1':'0')+s;\n a/=2;\n }\n return s;\n }\npublic:\n string convertDateToBinary(string date) {\n \n string s;\n string output;\n for(char c:date){\n if(c=='-'){\n output+=convert(stoi(s))+'-';\n s=\"\";\n }\n else s+=c;\n }\n output+=convert(stoi(s));\n s=\"\";\n return output;\n }\n};", "memory": "9200" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
1
{ "code": "class Solution {\npublic:\n\n string getBinaryString(bitset<32>& bits) {\n string s = bits.to_string();\n int idx = s.find('1');\n return s.substr(idx);\n }\n \n string convertDateToBinary(string date) { \n bitset<32> year(stoi(date.substr(0, 4))); \n bitset<32> month(stoi(date.substr(5, 2))); \n bitset<32> day(stoi(date.substr(8, 2))); \n \n string ans = getBinaryString(year);\n ans += \"-\" + getBinaryString(month);\n ans += \"-\" + getBinaryString(day);\n \n \n return ans;\n }\n};", "memory": "9200" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
2
{ "code": "class Solution {\npublic:\n string toBinary(int x) {\n string r;\n while (x != 0) {\n r = (x % 2 ? \"1\":\"0\") + r;\n x /= 2;\n }\n return r;\n }\n string convertDateToBinary(string date) {\n string ans;\n\n ans += toBinary(stoi(date.substr(0, 4)));\n ans += \"-\";\n ans += toBinary(stoi(date.substr(5, 7)));\n ans += \"-\";\n ans += toBinary(stoi(date.substr(8, 10)));\n\n return ans;\n }\n};", "memory": "9300" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
2
{ "code": "class Solution {\npublic:\n string dtob(int n)\n {\n string s = bitset<64>(n).to_string();\n const auto l = s.find('1');\n if (l != string::npos)\n {\n return s.substr(l);\n }\n return \"0\";\n }\n string convertDateToBinary(string date) {\n std::ios_base::sync_with_stdio(false);\n\t std::cin.tie(NULL);\n\t std::cout.tie(NULL);\n int y = stoi(date.substr(0, 4));\n int m = stoi(date.substr(5, 2));\n int d = stoi(date.substr(8, 2));\n string ans = \"\";\n ans += dtob(y);\n ans += '-';\n ans += dtob(m);\n ans += '-';\n ans += dtob(d);\n return ans;\n }\n};", "memory": "9300" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
2
{ "code": "class Solution {\npublic:\n string convertDateToBinary(string date) {\n int n=date.size();\n string ans1=date.substr(0,4);\n string ans2=date.substr(5,2);\n string ans3=date.substr(8,2);\n int a2=stoi(ans2);\n int a3=stoi(ans3);\n int a1=stoi(ans1);\n ans1=\"\";\n ans2=\"\";\n ans3=\"\";\n while(a1>0)\n {\n if(a1%2==0)\n ans1=ans1+'0';\n else\n ans1=ans1+'1';\n a1=a1/2;\n }\n while(a2>0)\n {\n if(a2%2==0)\n ans2=ans2+'0';\n else\n ans2=ans2+'1';\n a2=a2/2;\n }\n while(a3>0)\n {\n if(a3%2==0)\n ans3=ans3+'0';\n else\n ans3=ans3+'1';\n a3=a3/2;\n }\n reverse(ans1.begin(),ans1.end());\n reverse(ans2.begin(),ans2.end());\n reverse(ans3.begin(),ans3.end());\n string ans=ans1+'-'+ ans2+'-'+ ans3;\n return ans;\n }\n};", "memory": "9400" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
2
{ "code": "class Solution {\npublic:\n string convertDateToBinary(string date) {\n int num[3], idx = 0;\n string s = \"\";\n for (char ch : date) {\n if (ch == '-') {num[idx] = stoi(s); s = \"\"; idx++;}\n else s += ch;\n }\n num[2] = stoi(s);\n string ans = \"\";\n for (int i = 0; i < 3; i++){\n long long b = 0;\n while (num[i] > 0){\n int lg = log2(num[i]);\n b += pow(10, lg);\n num[i] -= pow(2, lg);\n }\n ans += to_string(b) + (i != 2 ? \"-\" : \"\");\n }\n return ans;\n }\n};", "memory": "9400" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
2
{ "code": "class Solution {\npublic:\nint convert(string num)\n{\n int ind =num.size()-1;\n int mul =1;\n int ans =0;\n\n while(ind>=0)\n {\n int currVal =num[ind]-'0';\n ans +=(mul*currVal);\n mul*=10;\n\n ind--;\n }\n\n return ans;\n}\n\nstring toBinary(string num,vector<int>&store)\n{\n\n int val =convert(num);\n string ans;\n\n for(int i =store.size()-1; i>=0; i--)\n {\n if((val-store[i])>=0)\n {\n val-=store[i];\n ans +='1';\n }\n else \n {\n ans +='0';\n }\n\n \n }\n\n return ans;\n\n}\n\n string convertDateToBinary(string date) {\n string y;\n string m;\n string d;\n\n int ind =0;\n while(date[ind]!='-')\n {\n y +=date[ind];\n ind++;\n } \n ind++;\n while(date[ind]!='-')\n {\n m +=date[ind];\n ind++;\n }\n ind++;\n while(ind<date.size())\n {\n d+=date[ind];\n ind++;\n }\n\n vector<int>store;\n store.push_back(1);\n int temp =2;\n\n for(int i =1; i<12; i++)\n {\n store.push_back(temp);\n temp =temp*2;\n }\n\n string newY =toBinary(y,store);\n string newM =toBinary(m,store);\n string newD =toBinary(d,store);\n\n string fY,fM,fD;\n\n int i =0;\n while(newY[i] =='0') i++;\n fY =newY.substr(i);\n\n i =0;\n while(newM[i] =='0') i++;\n fM =newM.substr(i);\n\n i =0;\n\n while(newD[i] =='0') i++;\n\n fD =newD.substr(i);\n\n \n return fY+'-'+fM+'-'+fD;\n \n }\n};", "memory": "9500" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
2
{ "code": "class Solution {\npublic:\n string solve(string s) {\n string ans = \"\";\n int tmp = 0;\n\n for(int i=0; i<s.size(); i++) {\n tmp = tmp*10 + (s[i]-'0');\n } \n\n while(tmp) {\n ans = ((tmp%2 == 0) ? \"0\" : \"1\") + ans;\n tmp /= 2;\n }\n\n return ans;\n }\n\n string convertDateToBinary(string s) {\n \n return solve(s.substr(0, 4)) + \"-\" + solve(s.substr(5, 2)) + \"-\" + solve(s.substr(8, 2));\n }\n};", "memory": "9500" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
2
{ "code": "class Solution {\npublic:\n string convertDateToBinary(string date) {\n string res = \"\";\n\n auto decimalToBinary = [](int decimal) {\n if (decimal == 0) return string(\"0\");\n string binary = \"\";\n while (decimal > 0) {\n binary = (decimal % 2 == 0 ? '0' : '1') + binary;\n decimal /= 2;\n }\n return binary;\n };\n\n int year = stoi(date.substr(0, 4));\n int month = stoi(date.substr(5, 2));\n int day = stoi(date.substr(8, 2));\n\n res = decimalToBinary(year) + \"-\" + decimalToBinary(month) + \"-\" + decimalToBinary(day);\n\n return res;\n }\n};", "memory": "9600" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
2
{ "code": "class Solution {\npublic:\n string trimLeadingZeros(const string &binary) {\n size_t pos = binary.find('1');\n if (pos != string::npos) {\n return binary.substr(pos);\n }\n return \"0\"; // In case the number is zero, return \"0\"\n }\n string solve(string d)\n {\n string dateParts[3];\n stringstream ss(d);\n string part;\n int i = 0;\n \n while (getline(ss, part, '-')) {\n dateParts[i++] = part;\n }\n \n int y = stoi(dateParts[0]); // Year\n int m = stoi(dateParts[1]); // Month\n int D = stoi(dateParts[2]); // Day\n\n \n string year = trimLeadingZeros(bitset<32>(y).to_string());\n string month = trimLeadingZeros(bitset<4>(m).to_string());\n string day = trimLeadingZeros(bitset<5>(D).to_string());\n\n // Return concatenated binary string with '-'\n return year + \"-\" + month + \"-\" + day;\n }\n string convertDateToBinary(string d) {\n return solve(d); \n }\n};", "memory": "9700" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
2
{ "code": "#include <string>\nusing namespace std;\n\nclass Solution {\npublic:\n string convertDateToBinary(string date) {\n return toBinary(stoi(date.substr(0, 4))) + \"-\" + toBinary(stoi(date.substr(5, 2))) + \"-\" + toBinary(stoi(date.substr(8, 2)));\n }\n\nprivate:\n string toBinary(int num) {\n string result = \"\";\n while (num > 0) {\n if (num & 1) {\n result = \"1\" + result;\n } else {\n result = \"0\" + result;\n }\n num >>= 1;\n }\n return result;\n }\n};\n", "memory": "9700" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
2
{ "code": "class Solution {\npublic:\n string decimalToBinary(int decimal)\n {\n string result;\n while (decimal > 0)\n {\n result.insert(0, to_string(decimal % 2));\n decimal /= 2;\n }\n return result;\n }\n\n string convertDateToBinary(string date)\n {\n string result;\n int number = 0;\n for(int i = 0; i < date.size(); ++i)\n {\n if (date.at(i) == '-')\n {\n result += decimalToBinary(number) + \"-\";\n number = 0;\n } else {\n if (number == 0)\n {\n number = date.at(i) - '0';\n } else\n {\n number *= 10;\n number += date.at(i) - '0';\n }\n }\n }\n result += decimalToBinary(number);\n return result;\n }\n};", "memory": "9800" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
2
{ "code": "class Solution {\n private:\n string f(string &str){\n int num=0,mul=1;\n for(int i=str.size()-1;i>=0;--i){\n num+=mul*(str[i]-'0');\n mul*=10;\n }\n string ans;;\n while(num){\n ans+=to_string(num&1);\n num=num>>1;\n }\n reverse(begin(ans),end(ans));\n return ans;\n }\npublic:\n string convertDateToBinary(string date) {\n string ans,temp;\n for(auto x:date){\n if(x=='-'){\n ans+=f(temp);\n temp=\"\";\n ans+=x;\n }\n else{\n temp+=x;\n }\n }\n ans+=f(temp);\n return ans;\n \n }\n};", "memory": "9800" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
2
{ "code": "class Solution {\n string convert(int num)\n {\n string s;\n while(num)\n {\n s+= to_string(num%2);\n num /= 2;\n }\n reverse(s.begin(), s.end());\n\n return s;\n }\npublic:\n string convertDateToBinary(string date) {\n string ans;\n string temp;\n for(auto c: date)\n {\n if(c == '-')\n {\n if(!ans.empty()) ans += '-';\n ans += convert(stoi(temp));\n temp = \"\";\n }\n else\n temp += c;\n }\n ans += '-';\n ans += convert(stoi(temp));\n return ans;\n }\n};", "memory": "9900" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
2
{ "code": "class Solution {\npublic:\n string convert(int num){\n string ans;\n while(num){\n string k= (num%2==1)? \"1\":\"0\";\n ans=ans+k;\n num=num/2;\n }\n cout<<num<<ans<<endl;\n reverse(ans.begin(),ans.end());\n return ans;\n }\n string convertDateToBinary(string date) {\n string year=date.substr(0,4);\n int yearI=stoi(year);\n cout<<yearI<<endl;\n string ans;\n ans+=convert(yearI);\n ans+=\"-\"+convert(stoi(date.substr(5,2)));\n ans+=\"-\"+convert(stoi(date.substr(8,2)));\n return ans;\n \n }\n};", "memory": "10000" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
2
{ "code": "class Solution {\npublic:\n string convert(int num) \n {\n string s;\n while(num)\n {\n s+= to_string(num%2);\n num /= 2;\n }\n reverse(s.begin(), s.end());\n\n return s;\n }\n string convertDateToBinary(string date) {\n string ans;\n string temp;\n for(auto c: date)\n {\n if(c == '-')\n {\n if(!ans.empty()) ans += '-';\n ans += convert(stoi(temp));\n temp = \"\";\n }\n else\n temp += c;\n }\n ans += '-';\n ans += convert(stoi(temp));\n return ans;\n }\n};", "memory": "10000" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
3
{ "code": "class Solution {\n string convert(int num)\n {\n string s;\n while(num)\n {\n s+= to_string(num%2);\n num /= 2;\n }\n reverse(s.begin(), s.end());\n\n return s;\n }\npublic:\n string convertDateToBinary(string date) {\n string ans;\n string temp;\n for(auto c: date)\n {\n if(c == '-')\n {\n if(!ans.empty()) ans += '-';\n ans += convert(stoi(temp));\n temp = \"\";\n }\n else\n temp += c;\n }\n ans += '-';\n ans += convert(stoi(temp));\n return ans;\n }\n};", "memory": "10100" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
3
{ "code": "class Solution {\npublic:\nstring convert(int num){\n string s;\n while(num){\n s+=to_string(num%2);\n num/=2;\n }\n reverse(s.begin(),s.end());\n return s;\n}\n string convertDateToBinary(string date) {\n string ans;\n string temp;\n for(auto c:date){\n if(c=='-'){\n if(!ans.empty()){\n ans+='-';\n }\n ans+=convert(stoi(temp));\n temp=\"\";\n }\n else{\n temp+=c;\n }\n }\n ans+='-';\n ans+=convert(stoi(temp));\n return ans;\n }\n};", "memory": "10100" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
3
{ "code": "class Solution {\n string convert(int num)\n {\n string s;\n while(num)\n {\n s+= to_string(num%2);\n num /= 2;\n }\n reverse(s.begin(), s.end());\n\n return s;\n }\npublic:\n string convertDateToBinary(string date) {\n string ans;\n string temp;\n for(auto c: date)\n {\n if(c == '-')\n {\n if(!ans.empty()) ans += '-';\n ans += convert(stoi(temp));\n temp = \"\";\n }\n else\n temp += c;\n }\n ans += '-';\n ans += convert(stoi(temp));\n return ans;\n }\n};", "memory": "10200" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
3
{ "code": "class Solution {\npublic:\n string convert(int num) \n {\n string s;\n while(num)\n {\n s+= to_string(num%2);\n num /= 2;\n }\n reverse(s.begin(), s.end());\n\n return s;\n }\n string convertDateToBinary(string date) {\n string ans;\n string temp;\n for(auto c: date)\n {\n if(c == '-')\n {\n if(!ans.empty()) ans += '-';\n ans += convert(stoi(temp));\n temp = \"\";\n }\n else\n temp += c;\n }\n ans += '-';\n ans += convert(stoi(temp));\n return ans;\n }\n};", "memory": "10200" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n string binary(int n){\n string s;\n while(n>0){\n s+=to_string(n%2);\n n=n/2;\n }\n reverse(s.begin(),s.end());\n return s;\n } \n\n string convertDateToBinary(string date) {\n \n string ans=\"\";\n string temp=\"\";\n\n for(int i=0;i<date.size();i++){\n if(date[i]=='-'){\n if(!ans.empty()){\n ans+='-';\n }\n ans+=binary(stoi(temp));\n temp=\"\";\n }\n else{\n temp+=date[i];\n }\n }\n\n ans+='-';\n ans+=binary(stoi(temp));\n return ans;\n }\n};", "memory": "10300" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
3
{ "code": "class Solution {\npublic:\n string bin(int n){\n string s;\n while(n){\n s+=to_string(n%2);\n n/=2;\n }\n reverse(s.begin(),s.end());\n return s;\n }\n string convertDateToBinary(string d) {\n string ans;\n string temp=\"\";\n for(int i=0;i<d.size();i++){\n if(d[i]=='-'){\n if(!ans.empty())\n ans +='-';\n ans+=bin(stoi(temp));\n temp=\"\";\n }\n else\n temp+=d[i];\n }\n ans+='-';\n ans+=bin(stoi(temp));\n return ans;\n }\n};", "memory": "10300" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
3
{ "code": "class Solution {\npublic:\n \n string convertToBinary(int num){\n \n string s=\"\";\n while(num){\n s += to_string(num%2); \n num = num/2; \n }\n reverse(s.begin(),s.end());\n return s; \n }\n\n string convertDateToBinary(string date){\n\n string temp = \"\";\n string ans = \"\";\n\n for(auto s: date){\n if(s=='-'){ \n if(!ans.empty()) ans+='-';\n ans += convertToBinary(stoi(temp)); \n temp = \"\"; \n } \n else{\n temp += s;\n } \n }\n\n ans += '-';\n ans += convertToBinary(stoi(temp));\n\n return ans; \n \n } \n}; ", "memory": "10400" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
3
{ "code": "class Solution {\npublic:\n string decToBin(int n){\n string s=\"\";\n int i=0;\n while(n>0){\n s=to_string(n%2)+s;\n n=n/2;\n i++;\n\n }\n return s;\n }\n string convertDateToBinary(string date) {\n string ans=decToBin(stoi(date.substr(0,4)));\n ans+=\"-\";\n ans+=decToBin(stoi(date.substr(5,7)));\n ans+=\"-\";\n ans+=decToBin(stoi(date.substr(8,10)));\n return ans;\n } \n};\n", "memory": "10400" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
3
{ "code": "class Solution {\npublic:\n string to_binary(int num) {\n string result = \"\";\n while (num > 0) {\n result += to_string(num % 2);\n num /= 2;\n }\n reverse(result.begin(), result.end());\n return result.empty() ? \"0\" : result;\n }\n\n string convertDateToBinary(string date) {\n string year = date.substr(0, 4);\n string month = date.substr(5, 2);\n string day = date.substr(8, 2);\n\n string result = \"\";\n result += to_binary(stoi(year));\n result += \"-\";\n result += to_binary(stoi(month)); \n result += \"-\";\n result += to_binary(stoi(day));\n\n return result;\n }\n};", "memory": "10500" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
3
{ "code": "class Solution {\npublic:\n string convertDateToBinary(string date) {\n int year=stoi(date.substr(0,4));\n int mon=stoi(date.substr(5,2));\n int day=stoi(date.substr(8,2));\n string res=\"\";\n while(year>0)\n {\n int dig=year%2;\n year/=2;\n res=to_string(dig)+res;\n }\n res+=\"-\";\n string temp=\"\";\n while(mon>0)\n {\n int dig=mon%2;\n mon/=2;\n temp=to_string(dig)+temp;\n }\n res+=temp;\n res+=\"-\";\n temp=\"\";\n while(day>0)\n {\n int dig=day%2;\n day/=2;\n temp=to_string(dig)+temp;\n }\n res+=temp;\n return res;\n \n\n }\n};", "memory": "10600" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
3
{ "code": "class Solution {\npublic:\n string decToBin(int n){\n string s=\"\";\n int i=0;\n while(n>0){\n s=to_string(n%2)+s;\n n=n/2;\n i++;\n\n }\n return s;\n }\n string convertDateToBinary(string date) {\n string ans=decToBin(stoi(date.substr(0,4)));\n ans+=\"-\";\n ans+=decToBin(stoi(date.substr(5,2)));\n ans+=\"-\";\n ans+=decToBin(stoi(date.substr(8,2)));\n return ans;\n } \n};\n", "memory": "10600" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
3
{ "code": "class Solution {\npublic:\n string convertDateToBinary(string date) {\n \n string year = date.substr(0, 4); // Correct: start at index 0, take 4 characters\n string month = date.substr(5, 2); // Correct: start at index 5, take 2 characters (for month)\n string day = date.substr(8, 2); // Correct: start at index 8, take 2 characters (for day)\n\n int year_int = stoi(year);\n int month_int = stoi(month);\n int day_int = stoi(day);\n\n // Array to store binary number\n string sum;\n vector<int> v;\n\n // Year binary conversion\n while (year_int > 0) {\n v.push_back(year_int % 2);\n year_int = year_int / 2;\n }\n\n // Reading binary representation of year (in reverse order)\n for (int i = v.size() - 1; i >= 0; i--) {\n sum += to_string(v[i]);\n }\n\n sum += \"-\";\n\n v.clear(); // Clear vector for next use\n\n // Month binary conversion\n while (month_int > 0) {\n v.push_back(month_int % 2);\n month_int = month_int / 2;\n }\n\n // Reading binary representation of month (in reverse order)\n for (int i = v.size() - 1; i >= 0; i--) {\n sum += to_string(v[i]);\n }\n\n sum += \"-\";\n\n v.clear(); // Clear vector for next use\n\n // Day binary conversion\n while (day_int > 0) {\n v.push_back(day_int % 2);\n day_int = day_int / 2;\n }\n\n // Reading binary representation of day (in reverse order)\n for (int i = v.size() - 1; i >= 0; i--) {\n sum += to_string(v[i]);\n }\n\n return sum;\n }\n\n};", "memory": "10700" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
3
{ "code": "class Solution {\npublic:\n string convertDateToBinary(string date) {\n int first=stoi(date.substr(0,4));\n int second=stoi(date.substr(5,2));\n int third=stoi(date.substr(8,2));\n\n string f=\"\";\n while(first){\n int di=first&1;\n f+=to_string(di);\n first=first>>1;\n }\n reverse(f.begin(),f.end());\n string s=\"\";\n while(second){\n int di=second&1;\n s+=to_string(di);\n second=second>>1;\n }\n reverse(s.begin(),s.end());\n string t=\"\";\n while(third){\n int di=third&1;\n t+=to_string(di);\n third=third>>1;\n }\n reverse(t.begin(),t.end());\n string ans=f+\"-\"+s+\"-\"+t;\n return ans;\n\n }\n};", "memory": "10700" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
3
{ "code": "class Solution {\npublic:\n string convertDateToBinary(string date) {\n std::istringstream iss(date);\n std::string word, res;\n while (std::getline(iss, word, '-')) {\n res += to_binary(word);\n res += \"-\";\n }\n\n res.pop_back();\n return res;\n }\n\n std::string to_binary(const std::string& num) {\n auto n = std::stoi(num);\n std::string res;\n while (n) {\n res = std::to_string(n % 2) + res;\n n /= 2;\n }\n return res;\n }\n};", "memory": "10800" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
3
{ "code": "class Solution {\npublic:\n string to_binary(string s){\n int num = stoi(s);\n string result =\"\";\n while(num>0){\n int temp = num % 2;\n num /= 2;\n result += to_string(temp);\n }\n reverse(result.begin(),result.end());\n return result;\n }\n string convertDateToBinary(string date) {\n string year = \"\",month = \"\",day = \"\";\n string result = \"\";\n for(int i = 0;i<4;i++){\n year += date[i];\n }\n result += to_binary(year);\n result += \"-\";\n for(int i = 5;i<7;i++){\n month += date[i];\n }\n result += to_binary(month);\n result +=\"-\";\n for(int i = 8;i<10;i++){\n day += date[i];\n }\n result += to_binary(day);\n return result;\n\n }\n};", "memory": "10900" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
3
{ "code": "class Solution {\npublic:\n string to_binary(string s){\n int num = stoi(s);\n string result =\"\";\n while(num>0){\n int temp = num % 2;\n num /= 2;\n result += to_string(temp);\n }\n reverse(result.begin(),result.end());\n return result;\n }\n string convertDateToBinary(string date) {\n string year = \"\",month = \"\",day = \"\";\n string result = \"\";\n for(int i = 0;i<4;i++){\n year += date[i];\n }\n result += to_binary(year);\n result += \"-\";\n for(int i = 5;i<7;i++){\n month += date[i];\n }\n result += to_binary(month);\n result +=\"-\";\n for(int i = 8;i<10;i++){\n day += date[i];\n }\n result += to_binary(day);\n return result;\n\n }\n};", "memory": "10900" }
3,567
<p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p> <p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p> <p>Return the <strong>binary</strong> representation of <code>date</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;2080-02-29&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;100000100000-10-11101&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">date = &quot;1900-01-01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11101101100-1-1&quot;</span></p> <p><strong>Explanation:</strong></p> <p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>date.length == 10</code></li> <li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits.</li> <li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li> </ul>
3
{ "code": "class Solution {\npublic:\n\nstring convert(int nums){\n if(nums==0){\n return \"\";\n }\n string ans=convert(nums/2);\n ans=to_string(nums%2)+ans;\n\n return ans;\n}\n\nstring rev(string s){\n reverse(s.begin(),s.end());\n return s;\n}\n string convertDateToBinary(string date) {\n int nums=0;\n string ans=\"\";\n\n for(int i=0;i<date.size();i++){\n\n if(date[i]=='-'){\n string s=convert(nums);\n ans+=rev(s)+'-';\n \n nums=0;\n }else{\n nums=nums*10+date[i]-48;\n // cout<<nums;\n }\n }\n \n ans+=rev(convert(nums));\n\n return ans;\n \n }\n};", "memory": "11000" }
3,476
<p>You are given an integer array <code>nums</code>. In one operation, you can add or subtract 1 from <strong>any</strong> element of <code>nums</code>.</p> <p>Return the <strong>minimum</strong> number of operations to make all elements of <code>nums</code> divisible by 3.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>All array elements can be made divisible by 3 using 3 operations:</p> <ul> <li>Subtract 1 from 1.</li> <li>Add 1 to 2.</li> <li>Subtract 1 from 4.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,6,9]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 50</code></li> <li><code>1 &lt;= nums[i] &lt;= 50</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumOperations(vector<int>& nums) {\n int cnt=0;\n for(auto i:nums){\n if(i%3!=0){\n cnt++;\n }\n }\n return cnt;\n }\n};", "memory": "22400" }
3,476
<p>You are given an integer array <code>nums</code>. In one operation, you can add or subtract 1 from <strong>any</strong> element of <code>nums</code>.</p> <p>Return the <strong>minimum</strong> number of operations to make all elements of <code>nums</code> divisible by 3.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>All array elements can be made divisible by 3 using 3 operations:</p> <ul> <li>Subtract 1 from 1.</li> <li>Add 1 to 2.</li> <li>Subtract 1 from 4.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,6,9]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 50</code></li> <li><code>1 &lt;= nums[i] &lt;= 50</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumOperations(vector<int>& nums) {\n int c=0;\n for(auto it:nums)\n {\n if(it==1)c++;\n else if(it%3!=0)c++;\n else continue;\n }\n return c;\n }\n};", "memory": "22400" }
3,476
<p>You are given an integer array <code>nums</code>. In one operation, you can add or subtract 1 from <strong>any</strong> element of <code>nums</code>.</p> <p>Return the <strong>minimum</strong> number of operations to make all elements of <code>nums</code> divisible by 3.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>All array elements can be made divisible by 3 using 3 operations:</p> <ul> <li>Subtract 1 from 1.</li> <li>Add 1 to 2.</li> <li>Subtract 1 from 4.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,6,9]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 50</code></li> <li><code>1 &lt;= nums[i] &lt;= 50</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumOperations(vector<int>& nums) {\n int ans = 0;\n\n for(int i=0; i<nums.size();i++)\n {\n if(nums[i]%3 != 0)\n ans++;\n\n }\n\n return ans;\n }\n};", "memory": "22500" }
3,476
<p>You are given an integer array <code>nums</code>. In one operation, you can add or subtract 1 from <strong>any</strong> element of <code>nums</code>.</p> <p>Return the <strong>minimum</strong> number of operations to make all elements of <code>nums</code> divisible by 3.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>All array elements can be made divisible by 3 using 3 operations:</p> <ul> <li>Subtract 1 from 1.</li> <li>Add 1 to 2.</li> <li>Subtract 1 from 4.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,6,9]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 50</code></li> <li><code>1 &lt;= nums[i] &lt;= 50</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumOperations(vector<int>& nums) { \n \n int op = 0;\n for(int i : nums){\n if (i %3 != 0){\n op ++;\n }\n }\n return op ;\n }\n};", "memory": "22600" }
3,476
<p>You are given an integer array <code>nums</code>. In one operation, you can add or subtract 1 from <strong>any</strong> element of <code>nums</code>.</p> <p>Return the <strong>minimum</strong> number of operations to make all elements of <code>nums</code> divisible by 3.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>All array elements can be made divisible by 3 using 3 operations:</p> <ul> <li>Subtract 1 from 1.</li> <li>Add 1 to 2.</li> <li>Subtract 1 from 4.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,6,9]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 50</code></li> <li><code>1 &lt;= nums[i] &lt;= 50</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumOperations(vector<int>& nums) {\n int min_num_op = 0;\n for (int i = 0; i < nums.size(); i++){\n if (nums[i] % 3 !=0){\n min_num_op++;\n }\n }\n return min_num_op;\n }\n};", "memory": "22600" }
3,476
<p>You are given an integer array <code>nums</code>. In one operation, you can add or subtract 1 from <strong>any</strong> element of <code>nums</code>.</p> <p>Return the <strong>minimum</strong> number of operations to make all elements of <code>nums</code> divisible by 3.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>All array elements can be made divisible by 3 using 3 operations:</p> <ul> <li>Subtract 1 from 1.</li> <li>Add 1 to 2.</li> <li>Subtract 1 from 4.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,6,9]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 50</code></li> <li><code>1 &lt;= nums[i] &lt;= 50</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumOperations(vector<int>& nums) {\n int c=0;\n for(int i=0;i<nums.size();i++){\n if(nums[i]%3!=0){\n c+=1;\n }\n }\n return c;\n }\n};", "memory": "22700" }
3,476
<p>You are given an integer array <code>nums</code>. In one operation, you can add or subtract 1 from <strong>any</strong> element of <code>nums</code>.</p> <p>Return the <strong>minimum</strong> number of operations to make all elements of <code>nums</code> divisible by 3.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>All array elements can be made divisible by 3 using 3 operations:</p> <ul> <li>Subtract 1 from 1.</li> <li>Add 1 to 2.</li> <li>Subtract 1 from 4.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,6,9]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 50</code></li> <li><code>1 &lt;= nums[i] &lt;= 50</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumOperations(vector<int>& nums) {\n int ans = 0;\n\n for(int i{0}; i < nums.size(); i++) {\n ans += min(nums[i]%3, 3-nums[i]%3);\n }\n return ans;\n }\n};", "memory": "22700" }
3,475
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> 3 <strong>consecutive</strong> elements from the array and <strong>flip</strong> <strong>all</strong> of them.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1. If it is impossible, return -1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the elements at indices 0, 1 and 2. The resulting array is <code>nums = [<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>0</strong></u>,1,0,0]</code>.</li> <li>Choose the elements at indices 1, 2 and 3. The resulting array is <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<strong><u>0</u></strong>,0,0]</code>.</li> <li>Choose the elements at indices 3, 4 and 5. The resulting array is <code>nums = [1,1,1,<strong><u>1</u></strong>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong><br /> It is impossible to make all elements equal to 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n \n int n = nums.size();\n int ans = 0;\n\n for(int i =0; i<n; i++){\n\n if(nums[i]==0){ \n\n if(i+3>n) return -1;\n\n for(int j =i; j<i+3; j++){\n if(nums[j]==0) nums[j] = 1;\n else nums[j] = 0;\n }\n\n ans++;\n\n } \n }\n \n return ans;\n }\n};", "memory": "138700" }
3,475
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> 3 <strong>consecutive</strong> elements from the array and <strong>flip</strong> <strong>all</strong> of them.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1. If it is impossible, return -1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the elements at indices 0, 1 and 2. The resulting array is <code>nums = [<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>0</strong></u>,1,0,0]</code>.</li> <li>Choose the elements at indices 1, 2 and 3. The resulting array is <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<strong><u>0</u></strong>,0,0]</code>.</li> <li>Choose the elements at indices 3, 4 and 5. The resulting array is <code>nums = [1,1,1,<strong><u>1</u></strong>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong><br /> It is impossible to make all elements equal to 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int n=nums.size();\n int ans=0;\n for(int i=0;i<n-2;i++){\n if(nums[i]==0){\n nums[i]=1;\n nums[i+1]=(nums[i+1]==0)?1:0;\n nums[i+2]=(nums[i+2]==1)?0:1;\n ans++;\n }\n }\n for(int i=0;i<n;i++){\n if(nums[i]==0)return -1;\n }\n return ans;\n }\n};", "memory": "138800" }
3,475
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> 3 <strong>consecutive</strong> elements from the array and <strong>flip</strong> <strong>all</strong> of them.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1. If it is impossible, return -1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the elements at indices 0, 1 and 2. The resulting array is <code>nums = [<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>0</strong></u>,1,0,0]</code>.</li> <li>Choose the elements at indices 1, 2 and 3. The resulting array is <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<strong><u>0</u></strong>,0,0]</code>.</li> <li>Choose the elements at indices 3, 4 and 5. The resulting array is <code>nums = [1,1,1,<strong><u>1</u></strong>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong><br /> It is impossible to make all elements equal to 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int cnt = 0;\n for(int i = 0;i<nums.size()-2;i++){\n if(nums[i] == 0){\n nums[i] = !nums[i];\n nums[i+1] = !nums[i+1];\n nums[i+2] = !nums[i+2] ;\n cnt++;\n }\n }\n return (nums[nums.size() -1] == 0 or nums[nums.size()-2] == 0 ? -1 : cnt);\n }\n};", "memory": "138800" }
3,475
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> 3 <strong>consecutive</strong> elements from the array and <strong>flip</strong> <strong>all</strong> of them.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1. If it is impossible, return -1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the elements at indices 0, 1 and 2. The resulting array is <code>nums = [<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>0</strong></u>,1,0,0]</code>.</li> <li>Choose the elements at indices 1, 2 and 3. The resulting array is <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<strong><u>0</u></strong>,0,0]</code>.</li> <li>Choose the elements at indices 3, 4 and 5. The resulting array is <code>nums = [1,1,1,<strong><u>1</u></strong>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong><br /> It is impossible to make all elements equal to 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>&nums) {\n int n=nums.size(),ans=0;\n //if(nums[n-2]==0 || nums[n-1]==0)\n //return -1;\n for(int i=0;i<n-2;++i)\n {\n if(nums[i]==0)\n {\n nums[i]=1-nums[i];\n nums[i+1]=1-nums[i+1];\n nums[i+2]=1-nums[i+2];\n ++ans;\n }\n }\n if(nums[n-2]==0 || nums[n-1]==0)\n return -1;\n return ans;\n }\n};", "memory": "138900" }
3,475
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> 3 <strong>consecutive</strong> elements from the array and <strong>flip</strong> <strong>all</strong> of them.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1. If it is impossible, return -1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the elements at indices 0, 1 and 2. The resulting array is <code>nums = [<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>0</strong></u>,1,0,0]</code>.</li> <li>Choose the elements at indices 1, 2 and 3. The resulting array is <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<strong><u>0</u></strong>,0,0]</code>.</li> <li>Choose the elements at indices 3, 4 and 5. The resulting array is <code>nums = [1,1,1,<strong><u>1</u></strong>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong><br /> It is impossible to make all elements equal to 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int n = nums.size(), ans = 0;\n for (int i =0 ; i< n-2; i++){\n if(nums[i] == 0){\n nums[i] = 1-nums[i];\n nums[i+1] = 1-nums[i+1];\n nums[i+2] = 1-nums[i+2];\n ans++;\n }\n }\n if(nums[n-2] == 0 || nums[n-1] == 0) return -1;\n return ans;\n }\n};", "memory": "138900" }
3,475
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> 3 <strong>consecutive</strong> elements from the array and <strong>flip</strong> <strong>all</strong> of them.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1. If it is impossible, return -1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the elements at indices 0, 1 and 2. The resulting array is <code>nums = [<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>0</strong></u>,1,0,0]</code>.</li> <li>Choose the elements at indices 1, 2 and 3. The resulting array is <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<strong><u>0</u></strong>,0,0]</code>.</li> <li>Choose the elements at indices 3, 4 and 5. The resulting array is <code>nums = [1,1,1,<strong><u>1</u></strong>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong><br /> It is impossible to make all elements equal to 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
0
{ "code": "class Solution {\n public:\n int minOperations(vector<int>& nums) {\n const int n = nums.size();\n int ans = 0;\n\n for (int i = 0; i + 2 < n; ++i)\n if (nums[i] == 0) {\n nums[i + 1] ^= 1;\n nums[i + 2] ^= 1;\n ++ans;\n }\n\n return nums[n - 1] == 0 || nums[n - 2] == 0 ? -1 : ans;\n }\n};", "memory": "139000" }
3,475
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> 3 <strong>consecutive</strong> elements from the array and <strong>flip</strong> <strong>all</strong> of them.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1. If it is impossible, return -1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the elements at indices 0, 1 and 2. The resulting array is <code>nums = [<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>0</strong></u>,1,0,0]</code>.</li> <li>Choose the elements at indices 1, 2 and 3. The resulting array is <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<strong><u>0</u></strong>,0,0]</code>.</li> <li>Choose the elements at indices 3, 4 and 5. The resulting array is <code>nums = [1,1,1,<strong><u>1</u></strong>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong><br /> It is impossible to make all elements equal to 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n \n int count=0,n=nums.size();\n \n for(int i=0;i<n;i++){\n if(nums[i]==0){\n if(i+3>n){\n return -1;\n }\n for(int j=i;j<i+3;j++){\n nums[j]=!nums[j];\n } \n count++;\n }\n }\n return count;\n }\n};\n", "memory": "139000" }
3,475
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> 3 <strong>consecutive</strong> elements from the array and <strong>flip</strong> <strong>all</strong> of them.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1. If it is impossible, return -1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the elements at indices 0, 1 and 2. The resulting array is <code>nums = [<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>0</strong></u>,1,0,0]</code>.</li> <li>Choose the elements at indices 1, 2 and 3. The resulting array is <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<strong><u>0</u></strong>,0,0]</code>.</li> <li>Choose the elements at indices 3, 4 and 5. The resulting array is <code>nums = [1,1,1,<strong><u>1</u></strong>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong><br /> It is impossible to make all elements equal to 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int n = nums.size();\n int ans =0;\n for(int i=0;i<n-2;i++){\n if(nums[i]==0){\n nums[i]=1;\n nums[i+1] = (nums[i+1]==1) ? 0:1;\n nums[i+2] = (nums[i+2]==1) ? 0:1;\n ans++;\n }\n }\n for(int i=0;i<n;i++){\n if(nums[i]==0) return -1;\n }\n return ans;\n \n }\n};", "memory": "139100" }
3,475
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> 3 <strong>consecutive</strong> elements from the array and <strong>flip</strong> <strong>all</strong> of them.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1. If it is impossible, return -1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the elements at indices 0, 1 and 2. The resulting array is <code>nums = [<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>0</strong></u>,1,0,0]</code>.</li> <li>Choose the elements at indices 1, 2 and 3. The resulting array is <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<strong><u>0</u></strong>,0,0]</code>.</li> <li>Choose the elements at indices 3, 4 and 5. The resulting array is <code>nums = [1,1,1,<strong><u>1</u></strong>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong><br /> It is impossible to make all elements equal to 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int cnt=0;\n for(int i=0;i<nums.size()-2;i++){\n if(nums[i]==0){\n cnt++;\n for(int j=i;j<i+3;j++){\n if(nums[j]==0){\n nums[j]=1;\n }else{\n nums[j]=0;\n }\n }\n }\n }\n for(int i=0;i<nums.size();i++){\n if(nums[i]==0){\n return -1;\n }\n }\n return cnt;\n }\n};", "memory": "139100" }
3,475
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> 3 <strong>consecutive</strong> elements from the array and <strong>flip</strong> <strong>all</strong> of them.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1. If it is impossible, return -1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the elements at indices 0, 1 and 2. The resulting array is <code>nums = [<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>0</strong></u>,1,0,0]</code>.</li> <li>Choose the elements at indices 1, 2 and 3. The resulting array is <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<strong><u>0</u></strong>,0,0]</code>.</li> <li>Choose the elements at indices 3, 4 and 5. The resulting array is <code>nums = [1,1,1,<strong><u>1</u></strong>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong><br /> It is impossible to make all elements equal to 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int count = 0;\n for (int i = 0; i < nums.size(); i++) {\n if (nums[i] == 0) {\n cout <<\" \"<<i<<\" \";\n if ((i + 3) > nums.size())\n return -1;\n for (int j = i; j < (i+3) ; j++) {\n nums[j] =! nums[j];\n }\n count++;\n }\n }\n return count;\n }\n};", "memory": "139200" }
3,475
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> 3 <strong>consecutive</strong> elements from the array and <strong>flip</strong> <strong>all</strong> of them.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1. If it is impossible, return -1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the elements at indices 0, 1 and 2. The resulting array is <code>nums = [<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>0</strong></u>,1,0,0]</code>.</li> <li>Choose the elements at indices 1, 2 and 3. The resulting array is <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<strong><u>0</u></strong>,0,0]</code>.</li> <li>Choose the elements at indices 3, 4 and 5. The resulting array is <code>nums = [1,1,1,<strong><u>1</u></strong>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong><br /> It is impossible to make all elements equal to 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n vector<int> t(4, numeric_limits<int>::min());\n t[(nums[0] << 1) | nums[1]] = 0;\n\n int n = nums.size();\n vector<int> nt(4, numeric_limits<int>::min());\n for (int i = 2; i < n; i++) {\n if (nums[i]) {\n nt[0b00] = t[0b01] + 1;\n nt[0b10] = t[0b00] + 1;\n nt[0b01] = t[0b10];\n nt[0b11] = t[0b11];\n } else {\n nt[0b00] = t[0b10];\n nt[0b10] = t[0b11];\n nt[0b01] = t[0b01] + 1;\n nt[0b11] = t[0b00] + 1;\n }\n\n t = nt;\n\n bool valid = false;\n for (int j = 0; j < 4; j++) {\n if (t[j] >= 0) valid = true;\n }\n if (!valid) return -1;\n }\n\n if (t[0b11] >= 0) return t[0b11];\n else return -1;\n }\n};", "memory": "139300" }
3,475
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> 3 <strong>consecutive</strong> elements from the array and <strong>flip</strong> <strong>all</strong> of them.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1. If it is impossible, return -1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the elements at indices 0, 1 and 2. The resulting array is <code>nums = [<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>0</strong></u>,1,0,0]</code>.</li> <li>Choose the elements at indices 1, 2 and 3. The resulting array is <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<strong><u>0</u></strong>,0,0]</code>.</li> <li>Choose the elements at indices 3, 4 and 5. The resulting array is <code>nums = [1,1,1,<strong><u>1</u></strong>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong><br /> It is impossible to make all elements equal to 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n vector<int> t(4, numeric_limits<int>::min());\n t[(nums[0] << 1) | nums[1]] = 0;\n\n int n = nums.size();\n vector<int> nt(4, numeric_limits<int>::min());\n for (int i = 2; i < n; i++) {\n int x = nums[i];\n nt[1 - x] = t[0b01] + 1;\n nt[3 - x] = t[0b00] + 1;\n nt[x] = t[0b10];\n nt[2 + x] = t[0b11];\n\n t = nt;\n }\n\n if (t[0b11] >= 0) return t[0b11];\n else return -1;\n }\n};", "memory": "139300" }
3,475
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> 3 <strong>consecutive</strong> elements from the array and <strong>flip</strong> <strong>all</strong> of them.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1. If it is impossible, return -1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the elements at indices 0, 1 and 2. The resulting array is <code>nums = [<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>0</strong></u>,1,0,0]</code>.</li> <li>Choose the elements at indices 1, 2 and 3. The resulting array is <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<strong><u>0</u></strong>,0,0]</code>.</li> <li>Choose the elements at indices 3, 4 and 5. The resulting array is <code>nums = [1,1,1,<strong><u>1</u></strong>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong><br /> It is impossible to make all elements equal to 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n vector<int> t(4, numeric_limits<int>::min());\n t[(nums[0] << 1) | nums[1]] = 0;\n\n int n = nums.size();\n vector<int> nt(4, numeric_limits<int>::min());\n for (int i = 2; i < n; i++) {\n nt[1 - nums[i]] = t[0b01] + 1;\n nt[3 - nums[i]] = t[0b00] + 1;\n nt[nums[i]] = t[0b10];\n nt[2 + nums[i]] = t[0b11];\n\n t = nt;\n }\n\n if (t[0b11] >= 0) return t[0b11];\n else return -1;\n }\n};", "memory": "139400" }
3,475
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> 3 <strong>consecutive</strong> elements from the array and <strong>flip</strong> <strong>all</strong> of them.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1. If it is impossible, return -1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the elements at indices 0, 1 and 2. The resulting array is <code>nums = [<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>0</strong></u>,1,0,0]</code>.</li> <li>Choose the elements at indices 1, 2 and 3. The resulting array is <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<strong><u>0</u></strong>,0,0]</code>.</li> <li>Choose the elements at indices 3, 4 and 5. The resulting array is <code>nums = [1,1,1,<strong><u>1</u></strong>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong><br /> It is impossible to make all elements equal to 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int l = nums.size();\n int a[l];\n memset(a,0,sizeof(a));\n int count=0;\n for(int i=0;i<l-2;i++){\n bool flip = a[i]%2!=0;\n if((flip && nums[i]==0) || (!flip && nums[i]==1)){\n continue;\n }else{\n count++;\n a[i]++;\n a[i+1]++;\n a[i+2]++;\n }\n }\n for(int i=0;i<l;i++){\n cout<<a[i]<<\" \";\n }\n for(int i = l-3;i<l;i++){\n bool flip = a[i]%2!=0;\n if((flip && nums[i]==0) || (!flip && nums[i]==1)){\n continue;\n }else{\n return -1;\n }\n }\n return count;\n }\n};", "memory": "139500" }
3,475
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> 3 <strong>consecutive</strong> elements from the array and <strong>flip</strong> <strong>all</strong> of them.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1. If it is impossible, return -1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the elements at indices 0, 1 and 2. The resulting array is <code>nums = [<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>0</strong></u>,1,0,0]</code>.</li> <li>Choose the elements at indices 1, 2 and 3. The resulting array is <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<strong><u>0</u></strong>,0,0]</code>.</li> <li>Choose the elements at indices 3, 4 and 5. The resulting array is <code>nums = [1,1,1,<strong><u>1</u></strong>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong><br /> It is impossible to make all elements equal to 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int n=nums.size(),np=0,cn=0;\n vector<bool> flip(n,0);\n for(int i=0;i<n;i++){\n if(i-3>=0 and flip[i-3]) cn--;\n if(cn&1) nums[i]=nums[i]^1;\n if(i<n-2 and nums[i]==0){\n nums[i]=1;\n flip[i]=1;\n np++;\n cn++;\n }\n }\n if(nums[n-1]==0 or nums[n-2]==0) return -1;\n return np;\n }\n};", "memory": "139800" }
3,475
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> 3 <strong>consecutive</strong> elements from the array and <strong>flip</strong> <strong>all</strong> of them.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1. If it is impossible, return -1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the elements at indices 0, 1 and 2. The resulting array is <code>nums = [<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>0</strong></u>,1,0,0]</code>.</li> <li>Choose the elements at indices 1, 2 and 3. The resulting array is <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<strong><u>0</u></strong>,0,0]</code>.</li> <li>Choose the elements at indices 3, 4 and 5. The resulting array is <code>nums = [1,1,1,<strong><u>1</u></strong>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong><br /> It is impossible to make all elements equal to 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int k = 3;\n int n = nums.size(), flipCount = 0, result = 0;\n\n vector<bool> isFlipped(n, false);\n\n for(int i = 0; i < n; i++)\n {\n if(i >= k && isFlipped[i - k] == true)flipCount--;\n\n if((flipCount % 2 == 1 && nums[i] == 1) || (flipCount % 2 == 0 && nums[i] == 0))\n {\n if(i + k > n)return -1;\n // we need to flip\n result++;\n flipCount++;\n isFlipped[i] = true;\n }\n }\n\n return result;\n }\n};", "memory": "139900" }
3,475
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> 3 <strong>consecutive</strong> elements from the array and <strong>flip</strong> <strong>all</strong> of them.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1. If it is impossible, return -1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the elements at indices 0, 1 and 2. The resulting array is <code>nums = [<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>0</strong></u>,1,0,0]</code>.</li> <li>Choose the elements at indices 1, 2 and 3. The resulting array is <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<strong><u>0</u></strong>,0,0]</code>.</li> <li>Choose the elements at indices 3, 4 and 5. The resulting array is <code>nums = [1,1,1,<strong><u>1</u></strong>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong><br /> It is impossible to make all elements equal to 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int k = 3;\n int n = nums.size(), flipCount = 0, result = 0;\n\n vector<bool> isFlipped(n, false);\n\n for(int i = 0; i < n; i++)\n {\n if(i >= k && isFlipped[i - k] == true)flipCount--;\n\n if((flipCount % 2 == 1 && nums[i] == 1) || (flipCount % 2 == 0 && nums[i] == 0))\n {\n if(i + k - 1 >= n)return -1;\n // we need to flip\n result++;\n flipCount++;\n isFlipped[i] = true;\n }\n }\n\n return result;\n }\n};", "memory": "139900" }
3,475
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> 3 <strong>consecutive</strong> elements from the array and <strong>flip</strong> <strong>all</strong> of them.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1. If it is impossible, return -1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the elements at indices 0, 1 and 2. The resulting array is <code>nums = [<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>0</strong></u>,1,0,0]</code>.</li> <li>Choose the elements at indices 1, 2 and 3. The resulting array is <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<strong><u>0</u></strong>,0,0]</code>.</li> <li>Choose the elements at indices 3, 4 and 5. The resulting array is <code>nums = [1,1,1,<strong><u>1</u></strong>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong><br /> It is impossible to make all elements equal to 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "\nclass Solution\n{\npublic:\n int minOperations(vector<int> &a)\n {\n int n = a.size();\n vector<bool> b(n);\n int count =0;\n\n for (int i = 0; i < n; i++)\n {\n if (a[i] == 0)\n b[i] = 0;\n else\n {\n b[i] = 1;\n }\n }\n for (int i = 0; i < n-2; i++)\n {\n if (b[i] == 0)\n {\n b[i] = !b[i];\n b[i + 1] = !b[i + 1];\n b[i + 2] = !b[i + 2];\n count++;\n }\n }\n if(b[n-1] != b[n-1] || b[n-2] != b[n-3] || b[n-3] != b[n-1] )\n return -1;\n else\n return count;\n }\n};", "memory": "140400" }
3,475
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> 3 <strong>consecutive</strong> elements from the array and <strong>flip</strong> <strong>all</strong> of them.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1. If it is impossible, return -1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the elements at indices 0, 1 and 2. The resulting array is <code>nums = [<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>0</strong></u>,1,0,0]</code>.</li> <li>Choose the elements at indices 1, 2 and 3. The resulting array is <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<strong><u>0</u></strong>,0,0]</code>.</li> <li>Choose the elements at indices 3, 4 and 5. The resulting array is <code>nums = [1,1,1,<strong><u>1</u></strong>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong><br /> It is impossible to make all elements equal to 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int num_flipped_window = 0;\n deque<bool> d;\n for (int i = 0; i < 3; ++i) {\n if ((nums[i]==0 && num_flipped_window%2==0) || (nums[i]==1 && num_flipped_window%2==1)) {\n if (nums.size()-i < 3) return -1;\n num_flipped_window++;\n d.push_back(true); \n } else {\n d.push_back(false);\n }\n }\n int num_flipped_total = num_flipped_window;\n for (int i = 3; i < nums.size(); ++i) {\n num_flipped_window -= d.front();\n d.pop_front();\n if ((nums[i]==0 && num_flipped_window%2==0) || (nums[i]==1 && num_flipped_window%2==1)) {\n if (nums.size()-i < 3) return -1;\n num_flipped_window++;\n num_flipped_total++;\n d.push_back(true);\n } else {\n d.push_back(false);\n }\n }\n return num_flipped_total;\n }\n};", "memory": "141500" }
3,475
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> 3 <strong>consecutive</strong> elements from the array and <strong>flip</strong> <strong>all</strong> of them.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1. If it is impossible, return -1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the elements at indices 0, 1 and 2. The resulting array is <code>nums = [<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>0</strong></u>,1,0,0]</code>.</li> <li>Choose the elements at indices 1, 2 and 3. The resulting array is <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<strong><u>0</u></strong>,0,0]</code>.</li> <li>Choose the elements at indices 3, 4 and 5. The resulting array is <code>nums = [1,1,1,<strong><u>1</u></strong>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong><br /> It is impossible to make all elements equal to 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n // Almost same as https://leetcode.com/problems/minimum-number-of-k-consecutive-bit-flips/\n int minOperations(vector<int>& nums) {\n int num_flipped_window = 0;\n deque<bool> d;\n for (int i = 0; i < 3; ++i) {\n if ((nums[i]==0 && num_flipped_window%2==0) || (nums[i]==1 && num_flipped_window%2==1)) {\n if (nums.size()-i < 3) return -1;\n num_flipped_window++;\n d.push_back(true); \n } else {\n d.push_back(false);\n }\n }\n int num_flipped_total = num_flipped_window;\n for (int i = 3; i < nums.size(); ++i) {\n num_flipped_window -= d.front();\n d.pop_front();\n if ((nums[i]==0 && num_flipped_window%2==0) || (nums[i]==1 && num_flipped_window%2==1)) {\n if (nums.size()-i < 3) return -1;\n num_flipped_window++;\n num_flipped_total++;\n d.push_back(true);\n } else {\n d.push_back(false);\n }\n }\n return num_flipped_total;\n }\n};", "memory": "141500" }
3,475
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> 3 <strong>consecutive</strong> elements from the array and <strong>flip</strong> <strong>all</strong> of them.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1. If it is impossible, return -1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the elements at indices 0, 1 and 2. The resulting array is <code>nums = [<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>0</strong></u>,1,0,0]</code>.</li> <li>Choose the elements at indices 1, 2 and 3. The resulting array is <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<strong><u>0</u></strong>,0,0]</code>.</li> <li>Choose the elements at indices 3, 4 and 5. The resulting array is <code>nums = [1,1,1,<strong><u>1</u></strong>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong><br /> It is impossible to make all elements equal to 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "\n#define endl '\\n'\n#define pint pair<int, int>\n#define vint vector<int>\n#define vpint vector<pair<int, int>>\n#define vstr vector<string>\n#define uset unordered_set\n#define umap unordered_map\n#define vbool vector<bool>\n#define vvint vector<vector<int>>\n#define ll long long\n#define MOD 1000000007\n\nclass Solution {\npublic:\n int helper(vint& nums, int pos, bool flipN, bool flipNN) {\n int n = nums.size();\n\n if (pos >= n)\n return 0;\n\n int ans = 1000000;\n\n int curr = flipN ? nums[pos] ^ 1 : nums[pos];\n\n if (curr == 1)\n ans = min(ans, helper(nums, pos + 1, flipNN, false));\n else if (pos + 2 < n)\n ans = min(ans, 1 + helper(nums, pos + 1, !flipNN, true));\n\n return ans;\n }\n\n int minOperations(vector<int>& nums) {\n int n = nums.size();\n\n /*int cntZero = 0;\n for (int i = 0; i < n; i++)\n if (nums[i] == 0)\n cntZero++;\n int cntOne = n - cntZero;\n\n if (cntZero % 3)\n return -1;*/\n\n int ans = helper(nums, 0, false, false);\n\n return ans >= 1000000 ? -1 : ans;\n }\n};", "memory": "142000" }
3,475
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> 3 <strong>consecutive</strong> elements from the array and <strong>flip</strong> <strong>all</strong> of them.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1. If it is impossible, return -1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the elements at indices 0, 1 and 2. The resulting array is <code>nums = [<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>0</strong></u>,1,0,0]</code>.</li> <li>Choose the elements at indices 1, 2 and 3. The resulting array is <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<strong><u>0</u></strong>,0,0]</code>.</li> <li>Choose the elements at indices 3, 4 and 5. The resulting array is <code>nums = [1,1,1,<strong><u>1</u></strong>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong><br /> It is impossible to make all elements equal to 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "\n#define endl '\\n'\n#define pint pair<int, int>\n#define vint vector<int>\n#define vpint vector<pair<int, int>>\n#define vstr vector<string>\n#define uset unordered_set\n#define umap unordered_map\n#define vbool vector<bool>\n#define vvint vector<vector<int>>\n#define vvvint vector<vector<vector<int>>>\n#define ll long long\n#define MOD 1000000007\n\nclass Solution {\npublic:\n int helper(vint& nums, int pos, bool flipN, bool flipNN) {\n int n = nums.size();\n\n if (pos >= n)\n return 0;\n\n int ans = 1000000;\n\n int curr = flipN ? nums[pos] ^ 1 : nums[pos];\n\n if (curr == 1)\n ans = min(ans, helper(nums, pos + 1, flipNN, false));\n else if (pos + 2 < n)\n ans = min(ans, 1 + helper(nums, pos + 1, !flipNN, true));\n\n return ans;\n }\n\n int minOperations(vector<int>& nums) {\n int n = nums.size();\n\n int ans = helper(nums, 0, false, false);\n return ans >= 1000000 ? -1 : ans;\n }\n};", "memory": "142200" }
3,475
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> 3 <strong>consecutive</strong> elements from the array and <strong>flip</strong> <strong>all</strong> of them.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1. If it is impossible, return -1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the elements at indices 0, 1 and 2. The resulting array is <code>nums = [<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>0</strong></u>,1,0,0]</code>.</li> <li>Choose the elements at indices 1, 2 and 3. The resulting array is <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<strong><u>0</u></strong>,0,0]</code>.</li> <li>Choose the elements at indices 3, 4 and 5. The resulting array is <code>nums = [1,1,1,<strong><u>1</u></strong>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong><br /> It is impossible to make all elements equal to 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int result = 0;\n deque<int> q;\n for (int i = 0; i < nums.size(); ++i) {\n if (!q.empty() && q.front() + 3 == i) {\n q.pop_front();\n }\n if (q.size() % 2 == nums[i]) {\n ++result;\n q.push_back(i);\n if (i + 2 >= nums.size()) {\n return -1;\n }\n }\n }\n return result;\n }\n};", "memory": "143300" }
3,475
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> 3 <strong>consecutive</strong> elements from the array and <strong>flip</strong> <strong>all</strong> of them.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1. If it is impossible, return -1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the elements at indices 0, 1 and 2. The resulting array is <code>nums = [<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>0</strong></u>,1,0,0]</code>.</li> <li>Choose the elements at indices 1, 2 and 3. The resulting array is <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<strong><u>0</u></strong>,0,0]</code>.</li> <li>Choose the elements at indices 3, 4 and 5. The resulting array is <code>nums = [1,1,1,<strong><u>1</u></strong>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong><br /> It is impossible to make all elements equal to 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n deque<int>dq;\n int ans=0;\n\n for(int i=0;i<nums.size();i++){\n while(!dq.empty() and i>dq.front()+3-1){ // Window has reached more than size of k, so remove front as it longer effects current index\n dq.pop_front();\n }\n\n if( (nums[i]+dq.size()) % 2 == 0){ //True value is zero\n if(i+3>nums.size()){\n return -1;\n }\n ans++;\n dq.push_back(i);\n }\n }\n\n return ans;\n }\n};", "memory": "143400" }
3,475
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> 3 <strong>consecutive</strong> elements from the array and <strong>flip</strong> <strong>all</strong> of them.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1. If it is impossible, return -1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the elements at indices 0, 1 and 2. The resulting array is <code>nums = [<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>0</strong></u>,1,0,0]</code>.</li> <li>Choose the elements at indices 1, 2 and 3. The resulting array is <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<strong><u>0</u></strong>,0,0]</code>.</li> <li>Choose the elements at indices 3, 4 and 5. The resulting array is <code>nums = [1,1,1,<strong><u>1</u></strong>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong><br /> It is impossible to make all elements equal to 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int n=nums.size();\n queue<int> q;\n int ans=0;\n for(int i=0;i<n;i++){\n while(!q.empty() && q.front()<i-2){\n q.pop();\n\n }\n if((nums[i]+q.size())%2==0){\n if(i+3>n){\n return -1;\n }\n ans++;\n q.push(i);\n }\n }\n return ans;\n \n }\n};", "memory": "143500" }
3,475
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> 3 <strong>consecutive</strong> elements from the array and <strong>flip</strong> <strong>all</strong> of them.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1. If it is impossible, return -1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the elements at indices 0, 1 and 2. The resulting array is <code>nums = [<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>0</strong></u>,1,0,0]</code>.</li> <li>Choose the elements at indices 1, 2 and 3. The resulting array is <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<strong><u>0</u></strong>,0,0]</code>.</li> <li>Choose the elements at indices 3, 4 and 5. The resulting array is <code>nums = [1,1,1,<strong><u>1</u></strong>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong><br /> It is impossible to make all elements equal to 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "#pragma GCC optimize(\"03\")\nclass Solution {\npublic:\n int minOperations(vector<int>& nums) {\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n\n int n = nums.size();\n queue<int> q;\n bool flip = false;\n int ans = 0;\n for(int i = 0; i<n; i++){\n \n if(!q.empty() && i == q.front()){\n q.pop();\n flip = !flip;\n }\n\n if(flip){\n nums[i] = !nums[i];\n }\n\n if(i+3 <= n && nums[i] == 0){\n flip = !flip;\n q.push(i+3);\n ans++;\n }\n else{\n if(nums[i] == 0){\n return -1;\n }\n }\n\n }\n return ans; \n }\n};", "memory": "143600" }
3,477
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</strong> the elements from index <code>i</code> to the end of the array.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,0,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 0</code><span class="example-io">. The resulting array will be <code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 4</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 3</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,0,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong><br /> We can do the following operation:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int count = 0;\n for (int i=0; i<nums.size(); i++) {\n if ((count+nums[i])%2==0){\n count++;\n }\n }\n return count;\n }\n};", "memory": "155300" }
3,477
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</strong> the elements from index <code>i</code> to the end of the array.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,0,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 0</code><span class="example-io">. The resulting array will be <code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 4</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 3</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,0,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong><br /> We can do the following operation:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int ans=0;\n // int flag=1;\n for(int i=0;i<nums.size();i++)\n {\n nums[i]=(nums[i]+ans)%2;\n if(nums[i]==0)\n {\n ans++;\n nums[i]=1;\n }\n }\n // cout<<ans<<endl;\n for(int i=0;i<nums.size();i++)\n {\n if(nums[i]==0) return -1; \n }\n return ans;\n }\n};", "memory": "155400" }
3,477
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</strong> the elements from index <code>i</code> to the end of the array.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,0,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 0</code><span class="example-io">. The resulting array will be <code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 4</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 3</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,0,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong><br /> We can do the following operation:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n \n // int operations = 0;\n // for(int i=0; i<nums.size(); i++){\n // replaseBinaryNumbers(i, nums, operations);\n // }\n\n // return operations;\n\n\n int i = 1;\n int j = 0;\n\n int ind = 0;\n int operations = 0;\n while(ind < nums.size()){\n if(nums[ind] == j){\n int temp;\n temp = i;\n i = j;\n j = temp;\n operations++;\n }\n ind++;\n }\n\n return operations;\n }\n\n // void replaseBinaryNumbers(int ind, vector<int>& nums, int& operations){\n // if(nums[ind] == 1) return;\n\n // int n = nums.size();\n // operations++;\n // while(ind < n){\n // nums[ind] = nums[ind] == 0 ? 1 : 0;\n // ind++;\n // }\n // }\n};", "memory": "155500" }
3,477
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</strong> the elements from index <code>i</code> to the end of the array.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,0,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 0</code><span class="example-io">. The resulting array will be <code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 4</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 3</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,0,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong><br /> We can do the following operation:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int j = 0;\n int ind = 0;\n int operations = 0;\n while(ind < nums.size()){\n if(nums[ind] == j){\n j = j == 0? 1 : 0;\n operations++;\n }\n ind++;\n }\n\n return operations;\n }\n};", "memory": "155500" }
3,477
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</strong> the elements from index <code>i</code> to the end of the array.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,0,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 0</code><span class="example-io">. The resulting array will be <code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 4</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 3</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,0,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong><br /> We can do the following operation:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int n=nums.size();\n int ans=0;\n int prev=1,cur=1;\n for(int i=0;i<n;i++){\n if(nums[i]!=cur){\n cur=nums[i];\n }\n ans+=(cur!=prev);\n prev=cur;\n } \n return ans;\n }\n};", "memory": "155600" }
3,477
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</strong> the elements from index <code>i</code> to the end of the array.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,0,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 0</code><span class="example-io">. The resulting array will be <code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 4</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 3</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,0,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong><br /> We can do the following operation:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int n = nums.size();\n int swapCnt = 0;\n for(int i=0; i<n; ++i) {\n if((nums[i] + swapCnt)%2 == 0) {\n ++swapCnt;\n }\n }\n return swapCnt;\n }\n};", "memory": "155600" }
3,477
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</strong> the elements from index <code>i</code> to the end of the array.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,0,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 0</code><span class="example-io">. The resulting array will be <code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 4</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 3</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,0,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong><br /> We can do the following operation:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int op = 0;\n for (int i = 0; i < nums.size(); i++) {\n if (nums[i] == 0 && ((op & 1) == 0)) {\n op++;\n }\n else if (nums[i] == 1 && ((op & 1) == 1)) {\n op++;\n }\n }\n return op;\n }\n};", "memory": "155700" }
3,477
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</strong> the elements from index <code>i</code> to the end of the array.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,0,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 0</code><span class="example-io">. The resulting array will be <code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 4</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 3</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,0,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong><br /> We can do the following operation:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int i=0;\n int flipped = 0;\n int ans = 0;\n while(i < nums.size()) {\n if(nums[i] == flipped%2) {\n flipped++;\n ans++;\n }\n i++;\n }\n return ans;\n }\n};", "memory": "155700" }
3,477
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</strong> the elements from index <code>i</code> to the end of the array.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,0,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 0</code><span class="example-io">. The resulting array will be <code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 4</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 3</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,0,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong><br /> We can do the following operation:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n ios::sync_with_stdio(0);\n cin.tie(NULL);\n cout.tie(NULL);\n int flips = 0;\n int n = nums.size();\n for(int i = 0;i<n;i++){\n if((nums[i] == 0 && (flips%2 == 0)) || (nums[i] == 1 && flips%2 == 1)){\n flips++;\n }\n }\n return flips;\n }\n};", "memory": "155800" }
3,477
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</strong> the elements from index <code>i</code> to the end of the array.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,0,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 0</code><span class="example-io">. The resulting array will be <code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 4</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 3</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,0,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong><br /> We can do the following operation:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n ios_base::sync_with_stdio(false); cin.tie(0);\n //NOTE. nhung bit 1 se bi lat CHAN lan\n //NOTE. nhung bit 0 se bi lat LE lan\n int i=nums.size()-1, cnt=0;\n while (i>=0){\n int j=i;\n while (j>=0 && nums[j]==nums[i]) --j;\n if (nums[i]==0) ++cnt;\n i=j;\n }\n return nums.back()==1 ? 2*cnt:2*cnt-1;\n }\n};", "memory": "155900" }
3,477
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</strong> the elements from index <code>i</code> to the end of the array.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,0,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 0</code><span class="example-io">. The resulting array will be <code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 4</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 3</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,0,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong><br /> We can do the following operation:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n ios_base::sync_with_stdio(false); cin.tie(0);\n //NOTE. nhung bit 1 se bi lat CHAN lan\n //NOTE. nhung bit 0 se bi lat LE lan\n int i=nums.size()-1, cnt=0;\n while (i>=0){\n int j=i;\n while (j>=0 && nums[j]==nums[i]) --j;\n if (nums[i]==0) ++cnt;\n i=j;\n }\n return nums.back()==1 ? 2*cnt:2*cnt-1;\n }\n};", "memory": "155900" }
3,477
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</strong> the elements from index <code>i</code> to the end of the array.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,0,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 0</code><span class="example-io">. The resulting array will be <code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 4</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 3</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,0,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong><br /> We can do the following operation:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int n = nums.size();\n int dp[n];\n dp[0] = 1 - nums[0];\n for (int i = 1; i < n; i++) {\n dp[i] = dp[i-1];\n if ((nums[i] + dp[i]) % 2 == 0) dp[i]++;\n }\n return dp[n-1];\n }\n};", "memory": "156000" }