id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
241
<p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p> <p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> expression = &quot;2-1-1&quot; <strong>Output:</strong> [0,2] <strong>Explanation:</strong> ((2-1)-1) = 0 (2-(1-1)) = 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> expression = &quot;2*3-4*5&quot; <strong>Output:</strong> [-34,-14,-10,-10,10] <strong>Explanation:</strong> (2*(3-(4*5))) = -34 ((2*3)-(4*5)) = -14 ((2*(3-4))*5) = -10 (2*((3-4)*5)) = -10 (((2*3)-4)*5) = 10 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= expression.length &lt;= 20</code></li> <li><code>expression</code> consists of digits and the operator <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;*&#39;</code>.</li> <li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> diffWaysToCompute(string expression) {\n \n vector<int>ans;\n unordered_map<string, vector<int>>memo;\n\n for(int ind=0;ind<expression.length();ind++)\n {\n\n vector<int>left, right;\n char cur= expression[ind];\n\n if(cur=='*' || cur == '-' || cur=='+')\n {\n string e1 = expression.substr(0,ind);\n string e2 = expression.substr(ind+1);\n if(memo.find(e1)!=memo.end())\n {\n left=memo[e1];\n }\n else {\n left=diffWaysToCompute(e1);\n }\n if(memo.find(e2)!=memo.end())\n {\n right=memo[e2];\n }\n else{\n right=diffWaysToCompute(e2);\n }\n for(auto i:left)\n {\n for(auto j:right)\n {\n if(cur=='*') \n {\n ans.push_back(i*j);\n }\n else if(cur=='+')\n {\n ans.push_back(i+j);\n }\n else{\n ans.push_back(i-j);\n }\n }\n }\n }\n }\n if(ans.empty()) ans.push_back(stoi(expression));\n return ans;\n }\n};", "memory": "14300" }
241
<p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p> <p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> expression = &quot;2-1-1&quot; <strong>Output:</strong> [0,2] <strong>Explanation:</strong> ((2-1)-1) = 0 (2-(1-1)) = 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> expression = &quot;2*3-4*5&quot; <strong>Output:</strong> [-34,-14,-10,-10,10] <strong>Explanation:</strong> (2*(3-(4*5))) = -34 ((2*3)-(4*5)) = -14 ((2*(3-4))*5) = -10 (2*((3-4)*5)) = -10 (((2*3)-4)*5) = 10 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= expression.length &lt;= 20</code></li> <li><code>expression</code> consists of digits and the operator <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;*&#39;</code>.</li> <li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> diffWaysToCompute(string expression) {\n int n = expression.size();\n\n vector<int> ans;\n\n for(int i = 0; i < n; i++){\n if(!isdigit(expression[i])){\n string leftExp = expression.substr(0, i);\n string rightExp = expression.substr(i+1);\n\n vector<int> leftComp = diffWaysToCompute(leftExp);\n vector<int> rightComp = diffWaysToCompute(rightExp);\n\n for(auto l: leftComp){\n for(auto r: rightComp){\n if(expression[i] == '-') ans.push_back(l - r);\n if(expression[i] == '+') ans.push_back(l + r);\n if(expression[i] == '*') ans.push_back(l * r);\n }\n }\n }\n }\n\n if(ans.size() == 0) return {stoi(expression)};\n return ans;\n }\n};", "memory": "14400" }
241
<p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p> <p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> expression = &quot;2-1-1&quot; <strong>Output:</strong> [0,2] <strong>Explanation:</strong> ((2-1)-1) = 0 (2-(1-1)) = 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> expression = &quot;2*3-4*5&quot; <strong>Output:</strong> [-34,-14,-10,-10,10] <strong>Explanation:</strong> (2*(3-(4*5))) = -34 ((2*3)-(4*5)) = -14 ((2*(3-4))*5) = -10 (2*((3-4)*5)) = -10 (((2*3)-4)*5) = 10 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= expression.length &lt;= 20</code></li> <li><code>expression</code> consists of digits and the operator <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;*&#39;</code>.</li> <li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> diffWaysToCompute(string expression) {\n std::vector<int> results;\n for (int i = 0; i < expression.size(); ++i) {\n if (expression[i] == '+' || expression[i] == '-' || expression[i] == '*') {\n std::string left = expression.substr(0, i);\n std::string right = expression.substr(i + 1);\n \n std::vector<int> leftResults = diffWaysToCompute(left);\n std::vector<int> rightResults = diffWaysToCompute(right);\n \n for (int l : leftResults) {\n for (int r : rightResults) {\n if (expression[i] == '+') results.push_back(l + r);\n else if (expression[i] == '-') results.push_back(l - r);\n else if (expression[i] == '*') results.push_back(l * r);\n }\n }\n }\n }\n \n if (results.empty()) {\n results.push_back(std::stoi(expression)); // Base case: only a number\n }\n \n return results;\n }\n \n};", "memory": "14500" }
241
<p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p> <p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> expression = &quot;2-1-1&quot; <strong>Output:</strong> [0,2] <strong>Explanation:</strong> ((2-1)-1) = 0 (2-(1-1)) = 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> expression = &quot;2*3-4*5&quot; <strong>Output:</strong> [-34,-14,-10,-10,10] <strong>Explanation:</strong> (2*(3-(4*5))) = -34 ((2*3)-(4*5)) = -14 ((2*(3-4))*5) = -10 (2*((3-4)*5)) = -10 (((2*3)-4)*5) = 10 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= expression.length &lt;= 20</code></li> <li><code>expression</code> consists of digits and the operator <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;*&#39;</code>.</li> <li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li> </ul>
3
{ "code": "#include <vector>\n#include <string>\n#include <unordered_map>\n#include <functional>\n#include <iostream>\n\nclass Solution {\npublic:\n std::vector<int> diffWaysToCompute(std::string expression) {\n std::function<std::vector<int>(std::string)> compute = [&](std::string expr) {\n if (std::all_of(expr.begin(), expr.end(), ::isdigit) || \n (expr[0] == '-' && std::all_of(expr.begin() + 1, expr.end(), ::isdigit))) {\n return std::vector<int>{std::stoi(expr)};\n }\n\n std::vector<int> result;\n\n for (size_t i = 0; i < expr.size(); ++i) {\n char ch = expr[i];\n if (ch == '+' || ch == '-' || ch == '*') {\n std::vector<int> left_results = compute(expr.substr(0, i));\n std::vector<int> right_results = compute(expr.substr(i + 1));\n for (int left : left_results) {\n for (int right : right_results) {\n if (ch == '+') {\n result.push_back(left + right);\n } else if (ch == '-') {\n result.push_back(left - right);\n } else if (ch == '*') {\n result.push_back(left * right);\n }\n }\n }\n }\n }\n\n return result;\n };\n\n return compute(expression);\n }\n};\n", "memory": "14600" }
241
<p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p> <p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> expression = &quot;2-1-1&quot; <strong>Output:</strong> [0,2] <strong>Explanation:</strong> ((2-1)-1) = 0 (2-(1-1)) = 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> expression = &quot;2*3-4*5&quot; <strong>Output:</strong> [-34,-14,-10,-10,10] <strong>Explanation:</strong> (2*(3-(4*5))) = -34 ((2*3)-(4*5)) = -14 ((2*(3-4))*5) = -10 (2*((3-4)*5)) = -10 (((2*3)-4)*5) = 10 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= expression.length &lt;= 20</code></li> <li><code>expression</code> consists of digits and the operator <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;*&#39;</code>.</li> <li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> solve(string& e,int l,int r){\n if(l>r){return {};}\n if(l==r){return {e[l]-'0'};}\n vector<int>ans;\n int f=0;\n for(int i=l;i<=r;i++){\n if(e[i]=='+'){\n f=1;\n vector<int>left=solve(e,l,i-1);\n vector<int>right=solve(e,i+1,r);\n for(int j=0;j<left.size();j++){\n for(int k=0;k<right.size();k++){\n ans.push_back(left[j]+right[k]);\n }\n }\n }\n else if(e[i]=='-'){\n f=1;\n vector<int>left=solve(e,l,i-1);\n vector<int>right=solve(e,i+1,r);\n for(int j=0;j<left.size();j++){\n for(int k=0;k<right.size();k++){\n ans.push_back(left[j]-right[k]);\n }\n }\n }\n else if(e[i]=='*'){\n f=1;\n vector<int>left=solve(e,l,i-1);\n vector<int>right=solve(e,i+1,r);\n for(int j=0;j<left.size();j++){\n for(int k=0;k<right.size();k++){\n ans.push_back(left[j]*right[k]);\n }\n }\n }\n }\n if(f==0){\n int num=0;\n for(int i=l;i<=r;i++){\n num=num*10+(e[i]-'0');\n }\n return {num};\n }\n \n\n return ans;\n }\n vector<int> diffWaysToCompute(string expression) {\n \n return solve(expression,0,expression.size()-1);\n }\n};", "memory": "14700" }
241
<p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p> <p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> expression = &quot;2-1-1&quot; <strong>Output:</strong> [0,2] <strong>Explanation:</strong> ((2-1)-1) = 0 (2-(1-1)) = 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> expression = &quot;2*3-4*5&quot; <strong>Output:</strong> [-34,-14,-10,-10,10] <strong>Explanation:</strong> (2*(3-(4*5))) = -34 ((2*3)-(4*5)) = -14 ((2*(3-4))*5) = -10 (2*((3-4)*5)) = -10 (((2*3)-4)*5) = 10 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= expression.length &lt;= 20</code></li> <li><code>expression</code> consists of digits and the operator <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;*&#39;</code>.</li> <li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li> </ul>
3
{ "code": "vector<int> f(string &s,int l,int r)\n{\n bool t=false;\n vector<int> left;\n vector<int> right;\n vector<int> ans;\n if(l==r) \n { \n ans.push_back(s[l]-'0');\n return ans;\n }\n //if(dp[l][r]!=-1)return dp[l][r];\n int a,b;\n for(int k=l;k<r;k++)\n {\n if(s[k]=='*')\n {\n t=true;\n left=f(s,l,k-1);\n right=f(s,k+1,r);\n int nl=left.size();\n int nr=right.size();\n for(int i=0;i<nl;i++)\n {\n for(int j=0;j<nr;j++)\n {\n \n ans.push_back(left[i]*right[j]);\n }\n }\n }\n if(s[k]=='-')\n {\n t=true;\n left=f(s,l,k-1);\n right=f(s,k+1,r);\n int nl=left.size();\n int nr=right.size();\n for(int i=0;i<nl;i++)\n {\n for(int j=0;j<nr;j++)\n {\n \n ans.push_back(left[i]-right[j]);\n }\n }\n }\n if(s[k]=='+')\n {\n t=true;\n left=f(s,l,k-1);\n right=f(s,k+1,r);\n int nl=left.size();\n int nr=right.size();\n for(int i=0;i<nl;i++)\n {\n for(int j=0;j<nr;j++)\n {\n \n ans.push_back(left[i]+right[j]);\n }\n }\n }\n\n }\n int temp=0;\n if(!t)\n {\n for(int i=l;i<=r;i++)\n temp=temp*10+(s[i]-'0');\n ans.push_back(temp);\n }\n return ans;\n}\nclass Solution {\npublic:\n vector<int> diffWaysToCompute(string expression) {\n vector<int> ans;\n //vector<vector<int>> dp(expression.size(),vector<int>(expression.size(),-1));\n \n return f(expression,0,expression.size()-1);\n \n }\n};", "memory": "14800" }
241
<p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p> <p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> expression = &quot;2-1-1&quot; <strong>Output:</strong> [0,2] <strong>Explanation:</strong> ((2-1)-1) = 0 (2-(1-1)) = 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> expression = &quot;2*3-4*5&quot; <strong>Output:</strong> [-34,-14,-10,-10,10] <strong>Explanation:</strong> (2*(3-(4*5))) = -34 ((2*3)-(4*5)) = -14 ((2*(3-4))*5) = -10 (2*((3-4)*5)) = -10 (((2*3)-4)*5) = 10 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= expression.length &lt;= 20</code></li> <li><code>expression</code> consists of digits and the operator <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;*&#39;</code>.</li> <li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int getNumFromString(string str) {\n int num = 0, n = str.length();\n if (n == 0) return num;\n bool neg = str[0] == '-';\n for (int i = neg ? 1 : 0; i < n; i++) {\n num *= 10;\n num += (int)str[i] - 48;\n }\n return neg ? 0 - num : num;\n }\n\n vector<string> tokenize(string expression) {\n vector<string> tokens;\n string num = \"\";\n for (char c : expression) {\n if (isdigit(c))\n num += c;\n else {\n tokens.push_back(num);\n num = \"\";\n tokens.push_back(string(1, c));\n }\n }\n if (num != \"\")\n tokens.push_back(num);\n return tokens;\n }\n\n vector<int> operate(vector<int> &a, vector<int> &b, string op, vector<int> &res) {\n for (int i = 0; i < (int)a.size(); i++) {\n for (int j = 0; j < (int)b.size(); j++) {\n if (op == \"+\")\n res.push_back(a[i] + b[j]);\n else if (op == \"-\")\n res.push_back(a[i] - b[j]);\n else\n res.push_back(a[i] * b[j]);\n }\n }\n return res;\n }\n\n vector<int> func(vector<string> &tokens, int l, int r) {\n // cout << l << \" \" << r << endl;\n if (l == r)\n return {getNumFromString(tokens[l])};\n vector<int> res;\n for (int i = l; i <= r; i++) {\n char test = tokens[i][0];\n if (!isdigit(test)) {\n string op = tokens[i];\n vector<int> left = func(tokens, l, i - 1);\n vector<int> right = func(tokens, i + 1, r);\n operate(left, right, op, res);\n }\n }\n return res;\n }\n\n vector<int> diffWaysToCompute(string expression) {\n vector<string> tokens = tokenize(expression);\n return func(tokens, 0, tokens.size() - 1);\n }\n};", "memory": "14900" }
241
<p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p> <p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> expression = &quot;2-1-1&quot; <strong>Output:</strong> [0,2] <strong>Explanation:</strong> ((2-1)-1) = 0 (2-(1-1)) = 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> expression = &quot;2*3-4*5&quot; <strong>Output:</strong> [-34,-14,-10,-10,10] <strong>Explanation:</strong> (2*(3-(4*5))) = -34 ((2*3)-(4*5)) = -14 ((2*(3-4))*5) = -10 (2*((3-4)*5)) = -10 (((2*3)-4)*5) = 10 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= expression.length &lt;= 20</code></li> <li><code>expression</code> consists of digits and the operator <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;*&#39;</code>.</li> <li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> f(int i,int j,string expression){\n vector<int> vec;\n if(i>j){\n return vec;\n }\n if(i==j){\n string str;\n str+=expression[i];\n vec.push_back(stoi(str));\n return vec;\n }\n if(i+1==j){\n string str;\n str+=expression[i];\n str+=expression[j];\n vec.push_back(stoi(str));\n return vec;\n }\n for(int ptr=i;ptr<=j;ptr++){\n vector<int> left,right;\n if(expression[ptr]=='+' ||expression[ptr]=='*'||expression[ptr]=='-'){\n vector<int> left=f(i,ptr-1,expression);\n vector<int> right=f(ptr+1,j,expression);\n for(auto val1:left){\n for(auto val2:right){\n if(expression[ptr]=='+'){\n vec.push_back(val1+val2);\n }\n else if(expression[ptr]=='*'){\n vec.push_back(val1*val2);\n }\n else if(expression[ptr]=='-'){\n vec.push_back(val1-val2);\n }\n }\n }\n }\n }\n return vec;\n }\n vector<int> diffWaysToCompute(string expression) {\n return f(0,expression.size()-1,expression);\n }\n};", "memory": "15000" }
241
<p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p> <p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> expression = &quot;2-1-1&quot; <strong>Output:</strong> [0,2] <strong>Explanation:</strong> ((2-1)-1) = 0 (2-(1-1)) = 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> expression = &quot;2*3-4*5&quot; <strong>Output:</strong> [-34,-14,-10,-10,10] <strong>Explanation:</strong> (2*(3-(4*5))) = -34 ((2*3)-(4*5)) = -14 ((2*(3-4))*5) = -10 (2*((3-4)*5)) = -10 (((2*3)-4)*5) = 10 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= expression.length &lt;= 20</code></li> <li><code>expression</code> consists of digits and the operator <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;*&#39;</code>.</li> <li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> helper(int start, int end, string exp) {\n int length = end - start +1;\n if(length <= 2) {\n return { stoi(exp.substr(start)) };\n }\n vector<int> possibleVals;\n for(int i = start; i <= end; i++) {\n if(exp[i] == '+' || exp[i] == '-' || exp[i] == '*') {\n vector<int> leftVal = helper(start, i-1,exp);\n vector<int> rightVal = helper(i+1, end,exp);\n\n for(auto& x: leftVal) {\n for(auto& y: rightVal) {\n if(exp[i] == '+') possibleVals.push_back(x+y);\n if(exp[i] == '-') possibleVals.push_back(x-y);\n if(exp[i] == '*') possibleVals.push_back(x*y);\n\n }\n }\n }\n }\n return possibleVals;\n }\n vector<int> diffWaysToCompute(string expression) {\n return helper(0,expression.size()-1,expression);\n }\n};", "memory": "15100" }
242
<p>Given two strings <code>s</code> and <code>t</code>, return <code>true</code> if <code>t</code> is an <span data-keyword="anagram">anagram</span> of <code>s</code>, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;anagram&quot;, t = &quot;nagaram&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;rat&quot;, t = &quot;car&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>s</code> and <code>t</code> consist of lowercase English letters.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> What if the inputs contain Unicode characters? How would you adapt your solution to such a case?</p>
0
{ "code": "class Solution {\npublic:\n bool isAnagram(string &s, string &t) {\n sort(s.begin(),s.end());\n sort(t.begin(),t.end());\n if(s==t) return true;\n else return false;\n }\n};\nstatic const int speedup = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();", "memory": "8300" }
242
<p>Given two strings <code>s</code> and <code>t</code>, return <code>true</code> if <code>t</code> is an <span data-keyword="anagram">anagram</span> of <code>s</code>, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;anagram&quot;, t = &quot;nagaram&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;rat&quot;, t = &quot;car&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>s</code> and <code>t</code> consist of lowercase English letters.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> What if the inputs contain Unicode characters? How would you adapt your solution to such a case?</p>
0
{ "code": "class Solution {\npublic:\n bool isAnagram(string &s, string &t) {\n sort(s.begin(),s.end());\n sort(t.begin(),t.end());\n if(s==t){\n return 1;\n }\n else return 0;\n }\n};\n", "memory": "8300" }
242
<p>Given two strings <code>s</code> and <code>t</code>, return <code>true</code> if <code>t</code> is an <span data-keyword="anagram">anagram</span> of <code>s</code>, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;anagram&quot;, t = &quot;nagaram&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;rat&quot;, t = &quot;car&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>s</code> and <code>t</code> consist of lowercase English letters.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> What if the inputs contain Unicode characters? How would you adapt your solution to such a case?</p>
0
{ "code": "class Solution {\npublic:\n bool isAnagram(string &s, string &t) {\n sort(s.begin(),s.end());\n sort(t.begin(),t.end());\n if(s==t) return true;\n else return false;\n }\n};\n", "memory": "8400" }
242
<p>Given two strings <code>s</code> and <code>t</code>, return <code>true</code> if <code>t</code> is an <span data-keyword="anagram">anagram</span> of <code>s</code>, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;anagram&quot;, t = &quot;nagaram&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;rat&quot;, t = &quot;car&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>s</code> and <code>t</code> consist of lowercase English letters.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> What if the inputs contain Unicode characters? How would you adapt your solution to such a case?</p>
0
{ "code": "class Solution {\npublic:\n bool isAnagram(string &s, string &t) {\n sort(s.begin(),s.end());\n sort(t.begin(),t.end());\n if(s==t) return true;\n else return false;\n }\n};\n", "memory": "8400" }
242
<p>Given two strings <code>s</code> and <code>t</code>, return <code>true</code> if <code>t</code> is an <span data-keyword="anagram">anagram</span> of <code>s</code>, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;anagram&quot;, t = &quot;nagaram&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;rat&quot;, t = &quot;car&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>s</code> and <code>t</code> consist of lowercase English letters.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> What if the inputs contain Unicode characters? How would you adapt your solution to such a case?</p>
0
{ "code": "class Solution {\npublic:\n bool isAnagram(string s, string t) {\n\n sort(s.begin(), s.end());\n sort(t.begin(), t.end());\n return s==t;\n }\n};", "memory": "8500" }
242
<p>Given two strings <code>s</code> and <code>t</code>, return <code>true</code> if <code>t</code> is an <span data-keyword="anagram">anagram</span> of <code>s</code>, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;anagram&quot;, t = &quot;nagaram&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;rat&quot;, t = &quot;car&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>s</code> and <code>t</code> consist of lowercase English letters.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> What if the inputs contain Unicode characters? How would you adapt your solution to such a case?</p>
0
{ "code": "class Solution {\npublic:\n bool isAnagram(string s, string t) {\n sort(s.begin(),s.end());\n sort(t.begin(),t.end());\n if(s==t){\n return true;\n }\n return false;\n \n }\n};", "memory": "8500" }
242
<p>Given two strings <code>s</code> and <code>t</code>, return <code>true</code> if <code>t</code> is an <span data-keyword="anagram">anagram</span> of <code>s</code>, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;anagram&quot;, t = &quot;nagaram&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;rat&quot;, t = &quot;car&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>s</code> and <code>t</code> consist of lowercase English letters.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> What if the inputs contain Unicode characters? How would you adapt your solution to such a case?</p>
0
{ "code": "class Solution {\npublic:\n bool isAnagram(string s, string t) {\n sort(s.begin(),s.end());\n sort(t.begin(),t.end());\n if(s==t){\n return true;\n }else{\n return false;\n }\n }\n};", "memory": "8600" }
242
<p>Given two strings <code>s</code> and <code>t</code>, return <code>true</code> if <code>t</code> is an <span data-keyword="anagram">anagram</span> of <code>s</code>, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;anagram&quot;, t = &quot;nagaram&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;rat&quot;, t = &quot;car&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>s</code> and <code>t</code> consist of lowercase English letters.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> What if the inputs contain Unicode characters? How would you adapt your solution to such a case?</p>
0
{ "code": "class Solution {\npublic:\n bool isAnagram(string s, string t) {\n sort(s.begin(), s.end());\n sort(t.begin(), t.end());\n return s == t;\n }\n};", "memory": "8600" }
242
<p>Given two strings <code>s</code> and <code>t</code>, return <code>true</code> if <code>t</code> is an <span data-keyword="anagram">anagram</span> of <code>s</code>, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;anagram&quot;, t = &quot;nagaram&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;rat&quot;, t = &quot;car&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>s</code> and <code>t</code> consist of lowercase English letters.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> What if the inputs contain Unicode characters? How would you adapt your solution to such a case?</p>
0
{ "code": "class Solution {\npublic:\n bool isAnagram(string s, string t) {\n if(s.size()!=t.size()){\n return false;\n }\n int l=s.size();\n int ar1[26];\n int ar2[26];\n for(int i=0;i<l;i++){\n ar1[s[i]-97]++;\n ar2[t[i]-97]++;\n }\n for(int i=0;i<26;i++){\n if(ar1[i]!=ar2[i]){\n return false;\n }\n }\n return true;\n }\n};", "memory": "8700" }
242
<p>Given two strings <code>s</code> and <code>t</code>, return <code>true</code> if <code>t</code> is an <span data-keyword="anagram">anagram</span> of <code>s</code>, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;anagram&quot;, t = &quot;nagaram&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;rat&quot;, t = &quot;car&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>s</code> and <code>t</code> consist of lowercase English letters.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> What if the inputs contain Unicode characters? How would you adapt your solution to such a case?</p>
0
{ "code": "class Solution {\npublic:\n bool isAnagram(string s, string t) {\n sort(s.begin(),s.end());\n sort(t.begin(),t.end());\n if(s==t)\n return true;\n else\n return false;\n }\n};", "memory": "8700" }
242
<p>Given two strings <code>s</code> and <code>t</code>, return <code>true</code> if <code>t</code> is an <span data-keyword="anagram">anagram</span> of <code>s</code>, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;anagram&quot;, t = &quot;nagaram&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;rat&quot;, t = &quot;car&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>s</code> and <code>t</code> consist of lowercase English letters.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> What if the inputs contain Unicode characters? How would you adapt your solution to such a case?</p>
0
{ "code": "class Solution {\npublic:\n bool isAnagram(string s, string t) {\n vector<int> freq(26);\n for(auto& x : s) freq[x - 'a']++;\n for(auto& x : t) {\n if(freq[x - 'a'] == 0) return false;\n freq[x - 'a']--;\n }\n return !(*max_element(freq.begin(), freq.end()));\n }\n};", "memory": "8800" }
242
<p>Given two strings <code>s</code> and <code>t</code>, return <code>true</code> if <code>t</code> is an <span data-keyword="anagram">anagram</span> of <code>s</code>, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;anagram&quot;, t = &quot;nagaram&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;rat&quot;, t = &quot;car&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>s</code> and <code>t</code> consist of lowercase English letters.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> What if the inputs contain Unicode characters? How would you adapt your solution to such a case?</p>
0
{ "code": "class Solution {\npublic:\n bool isAnagram(string s, string t) {\n if(s.length() != t.length()) return false;\n sort(s.begin(), s.end());\n sort(t.begin(), t.end());\n for(int i = 0; i < s.length(); i++){\n if(s[i] != t[i]) return false;\n }\n return true;\n }\n};", "memory": "8800" }
242
<p>Given two strings <code>s</code> and <code>t</code>, return <code>true</code> if <code>t</code> is an <span data-keyword="anagram">anagram</span> of <code>s</code>, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;anagram&quot;, t = &quot;nagaram&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;rat&quot;, t = &quot;car&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>s</code> and <code>t</code> consist of lowercase English letters.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> What if the inputs contain Unicode characters? How would you adapt your solution to such a case?</p>
1
{ "code": "class Solution {\npublic:\n bool isAnagram(string s, string t) {\n if (s.length() == t.length()) {\n std::unordered_map<char, int> map;\n for (unsigned int i = 0; i < s.length(); i++) {\n auto it = map.find(s[i]);\n if (it != map.end()) {\n it->second++;\n } else {\n map.insert({s[i], 1});\n }\n }\n\n for (unsigned int i = 0; i < s.length(); i++) {\n auto it = map.find(t[i]);\n if (it != map.end()) {\n it->second--;\n if (it->second == 0) {\n map.erase(t[i]);\n }\n } else {\n return false;\n }\n } \n if (map.size() == 0) {\n return true;\n } \n }\n return false;\n } \n \n};", "memory": "8900" }
242
<p>Given two strings <code>s</code> and <code>t</code>, return <code>true</code> if <code>t</code> is an <span data-keyword="anagram">anagram</span> of <code>s</code>, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;anagram&quot;, t = &quot;nagaram&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;rat&quot;, t = &quot;car&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>s</code> and <code>t</code> consist of lowercase English letters.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> What if the inputs contain Unicode characters? How would you adapt your solution to such a case?</p>
1
{ "code": "class Solution {\npublic:\n bool isAnagram(string s, string t) {\n sort(s.begin(), s.end());\n sort(t.begin(), t.end());\n\n return s == t;\n }\n};", "memory": "8900" }
242
<p>Given two strings <code>s</code> and <code>t</code>, return <code>true</code> if <code>t</code> is an <span data-keyword="anagram">anagram</span> of <code>s</code>, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;anagram&quot;, t = &quot;nagaram&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;rat&quot;, t = &quot;car&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>s</code> and <code>t</code> consist of lowercase English letters.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> What if the inputs contain Unicode characters? How would you adapt your solution to such a case?</p>
2
{ "code": "class Solution {\npublic:\n bool isAnagram(string s, string t) {\n int hash[256]={0};\n for(int i=0;i<s.size();i++){\n hash[s[i]]++;\n }\n for(int i=0;i<t.size();i++){\n hash[t[i]]--;\n }\n for(int i=0;i<256;i++){\n if(hash[i]!=0) return false;\n }\n return true;\n }\n};", "memory": "9000" }
242
<p>Given two strings <code>s</code> and <code>t</code>, return <code>true</code> if <code>t</code> is an <span data-keyword="anagram">anagram</span> of <code>s</code>, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;anagram&quot;, t = &quot;nagaram&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;rat&quot;, t = &quot;car&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>s</code> and <code>t</code> consist of lowercase English letters.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> What if the inputs contain Unicode characters? How would you adapt your solution to such a case?</p>
2
{ "code": "class Solution {\npublic:\n bool isAnagram(string s, string t) {\n int freqtable[256] = {0};\n for(int i =0; i<s.length(); i++){\n freqtable[s[i]]++;\n } \n for(int i =0; i<t.length(); i++){\n freqtable[t[i]]--;\n }\n for(int i =0; i<256; i++){\n if(freqtable[i]!=0){\n return false;\n }\n }\n return true;\n }\n};", "memory": "9000" }
242
<p>Given two strings <code>s</code> and <code>t</code>, return <code>true</code> if <code>t</code> is an <span data-keyword="anagram">anagram</span> of <code>s</code>, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;anagram&quot;, t = &quot;nagaram&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;rat&quot;, t = &quot;car&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>s</code> and <code>t</code> consist of lowercase English letters.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> What if the inputs contain Unicode characters? How would you adapt your solution to such a case?</p>
2
{ "code": "class Solution {\npublic:\n bool isAnagram(string s, string t) {\n if (s.size() != t.size()) {\n return false;\n }\n\n // Sort both strings\n string sorted_s = s;\n string sorted_t = t;\n sort(sorted_s.begin(), sorted_s.end());\n sort(sorted_t.begin(), sorted_t.end());\n\n // Compare the sorted strings\n return sorted_s == sorted_t;\n}\n};", "memory": "9100" }
242
<p>Given two strings <code>s</code> and <code>t</code>, return <code>true</code> if <code>t</code> is an <span data-keyword="anagram">anagram</span> of <code>s</code>, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;anagram&quot;, t = &quot;nagaram&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;rat&quot;, t = &quot;car&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>s</code> and <code>t</code> consist of lowercase English letters.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> What if the inputs contain Unicode characters? How would you adapt your solution to such a case?</p>
2
{ "code": "class Solution {\npublic:\n bool isAnagram(std::string s, std::string t) {\n std::vector<char> s1(s.begin(), s.end());\n std::vector<char> t1(t.begin(), t.end());\n\n int slength = s1.size();\n int tlength = t1.size();\n\n if (slength!=tlength) return false;\n\n int i = 0;\n for (int j = 0; j <= tlength; j++){\n if(i < slength && j < t1.size() && s1[i] == t1[j]){\n i++;\n t1.erase(t1.begin() + j);\n j=-1;\n if ( i >= slength) return true;\n }\n }\n return false;\n }\n};\n", "memory": "9100" }
242
<p>Given two strings <code>s</code> and <code>t</code>, return <code>true</code> if <code>t</code> is an <span data-keyword="anagram">anagram</span> of <code>s</code>, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;anagram&quot;, t = &quot;nagaram&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;rat&quot;, t = &quot;car&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>s</code> and <code>t</code> consist of lowercase English letters.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> What if the inputs contain Unicode characters? How would you adapt your solution to such a case?</p>
2
{ "code": "class Solution {\npublic:\n bool isAnagram(string s, string t) {\n \n string ss = s;\n string tt = t;\n sort(ss.begin(),ss.end());\n sort(tt.begin(),tt.end());\n\n if(ss==tt){\n return true;\n }\n return false;\n \n }\n};", "memory": "9200" }
242
<p>Given two strings <code>s</code> and <code>t</code>, return <code>true</code> if <code>t</code> is an <span data-keyword="anagram">anagram</span> of <code>s</code>, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;anagram&quot;, t = &quot;nagaram&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;rat&quot;, t = &quot;car&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>s</code> and <code>t</code> consist of lowercase English letters.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> What if the inputs contain Unicode characters? How would you adapt your solution to such a case?</p>
2
{ "code": "class Solution {\npublic:\n vector<int> convTonum(string b){\n vector<int> v(26,0);\n for (char j: b){\n v[j-'a']++;\n }\n return v;\n }\n bool isAnagram(string s, string t) {\n if(s.length() != t.length()){\n return false;\n }\n return convTonum(s) == convTonum(t);\n }\n};", "memory": "9200" }
242
<p>Given two strings <code>s</code> and <code>t</code>, return <code>true</code> if <code>t</code> is an <span data-keyword="anagram">anagram</span> of <code>s</code>, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;anagram&quot;, t = &quot;nagaram&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;rat&quot;, t = &quot;car&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>s</code> and <code>t</code> consist of lowercase English letters.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> What if the inputs contain Unicode characters? How would you adapt your solution to such a case?</p>
2
{ "code": "class Solution {\npublic:\n bool isAnagram(string s, string t) {\n if (s.size()!=t.size()){\n return false;\n }\n if (s==t){\n return true;\n }\n vector<char> svec;\n vector<char> tvec;\n\n for (int i=0; i<s.size(); i++){\n svec.push_back(s[i]);\n tvec.push_back(t[i]);\n }\n\n sort(svec.begin(),svec.end());\n sort(tvec.begin(),tvec.end());\n\n if (svec==tvec){\n return true;\n }\n return false;\n }\n};", "memory": "9300" }
242
<p>Given two strings <code>s</code> and <code>t</code>, return <code>true</code> if <code>t</code> is an <span data-keyword="anagram">anagram</span> of <code>s</code>, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;anagram&quot;, t = &quot;nagaram&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;rat&quot;, t = &quot;car&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>s</code> and <code>t</code> consist of lowercase English letters.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> What if the inputs contain Unicode characters? How would you adapt your solution to such a case?</p>
2
{ "code": "class Solution {\npublic:\n bool isAnagram(string s, string t) {\n if(s.size() != t.size()) return false;\n int n = s.length(); \n vector<char>ss, tt;\n for(int i = 0;i<n;i++)\n {\n ss.push_back(s[i]);\n tt.push_back(t[i]); \n }\n sort(ss.begin(),ss.end());\n sort(tt.begin(),tt.end());\n for(int i = 0; i < n;i++)\n {\n if(ss[i] != tt[i]) return false; \n }\n return true; \n }\n};", "memory": "9400" }
242
<p>Given two strings <code>s</code> and <code>t</code>, return <code>true</code> if <code>t</code> is an <span data-keyword="anagram">anagram</span> of <code>s</code>, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;anagram&quot;, t = &quot;nagaram&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;rat&quot;, t = &quot;car&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>s</code> and <code>t</code> consist of lowercase English letters.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> What if the inputs contain Unicode characters? How would you adapt your solution to such a case?</p>
2
{ "code": "class Solution {\npublic:\n bool isAnagram(string s, string t) {\n if(s.length()!=t.length())\n return false;\n vector <char> s2;\n vector <char> t2;\n for(int i=0;i<s.length();i++)\n {\n s2.push_back(s[i]);\n t2.push_back(t[i]);\n }\n sort(s2.begin(),s2.end());\n sort(t2.begin(),t2.end());\n if(s2==t2)\n return true;\n else\n return false;\n }\n};", "memory": "9500" }
242
<p>Given two strings <code>s</code> and <code>t</code>, return <code>true</code> if <code>t</code> is an <span data-keyword="anagram">anagram</span> of <code>s</code>, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;anagram&quot;, t = &quot;nagaram&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;rat&quot;, t = &quot;car&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>s</code> and <code>t</code> consist of lowercase English letters.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> What if the inputs contain Unicode characters? How would you adapt your solution to such a case?</p>
2
{ "code": "class Solution {\npublic:\n #include <iostream>\n#include <string>\n#include <unordered_map>\n\n// Function to determine if two strings are anagrams of each other\nbool isAnagram(const std::string& firstWord, const std::string& secondWord) {\n\n if (firstWord.length() != secondWord.length()) {\n return false;\n }\n\n std::unordered_map<char, int> charCount;\n\n for (char c : firstWord) {\n charCount[c]++;\n }\n\n for (char c : secondWord) {\n charCount[c]--;\n if (charCount[c] < 0) { //Check if negative\n return false;\n }\n }\n\n return true;\n}\n\nint main() {\n\n std::string s = \"computerscience\";\n std::string t = \"sciencecomputer\";\n\n if (isAnagram(s, t)) {\n std::cout << \"True\" << std::endl;\n } else {\n std::cout << \"False\" << std::endl;\n }\n\n return 0;\n}\n};", "memory": "9600" }
242
<p>Given two strings <code>s</code> and <code>t</code>, return <code>true</code> if <code>t</code> is an <span data-keyword="anagram">anagram</span> of <code>s</code>, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;anagram&quot;, t = &quot;nagaram&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;rat&quot;, t = &quot;car&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>s</code> and <code>t</code> consist of lowercase English letters.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> What if the inputs contain Unicode characters? How would you adapt your solution to such a case?</p>
2
{ "code": "class Solution {\npublic:\n bool isAnagram(string s, string t) {\n vector<char>a;\n vector<char>b;\n \n if(s.length()!=t.length())\n {\n return false;\n }\n for(int i=0;i<s.length();i++)\n {\n a.push_back(s[i]);\n b.push_back(t[i]);\n }\n\n sort(a.begin(),a.end());\n sort(b.begin(),b.end());\n \n for(int i=0;i<s.length();i++)\n {\n if(a[i]!=b[i])\n {\n return false;\n }\n }\n\n return true;\n \n }\n};", "memory": "9600" }
242
<p>Given two strings <code>s</code> and <code>t</code>, return <code>true</code> if <code>t</code> is an <span data-keyword="anagram">anagram</span> of <code>s</code>, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;anagram&quot;, t = &quot;nagaram&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;rat&quot;, t = &quot;car&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>s</code> and <code>t</code> consist of lowercase English letters.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> What if the inputs contain Unicode characters? How would you adapt your solution to such a case?</p>
2
{ "code": "class Solution {\npublic:\n bool isAnagram(string s, string t) {\n map<char,int> mp;\n \n for(char &c : s){\n mp[c-'a']++;\n }\n for(char &c : t){\n mp[c-'a']--;\n // If any character count goes negative, the strings are not anagrams\n \n }\n // After processing both strings, check if all counts are zero\n for (auto &entry : mp) {\n if (entry.second != 0) {\n return false;\n }\n }\n\n return true;\n \n \n\n\n \n }\n};", "memory": "9700" }
242
<p>Given two strings <code>s</code> and <code>t</code>, return <code>true</code> if <code>t</code> is an <span data-keyword="anagram">anagram</span> of <code>s</code>, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;anagram&quot;, t = &quot;nagaram&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;rat&quot;, t = &quot;car&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>s</code> and <code>t</code> consist of lowercase English letters.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> What if the inputs contain Unicode characters? How would you adapt your solution to such a case?</p>
2
{ "code": "class Solution {\npublic:\n bool isAnagram(string s, string t) {\n unordered_map<char, int> mpp;\n if (s.size() != t.size()) return false;\n\n for (int i=0; i<s.size(); i++)\n {\n mpp[s[i]]++;\n }\n for (int i=0; i<t.size(); i++)\n {\n if (mpp.find(t[i]) != mpp.end())\n {\n mpp[t[i]]--;\n }\n else\n return false;\n }\n for (const auto& entry : mpp) {\n if (entry.second != 0) {\n return false;\n }\n }\n return true;\n }\n};", "memory": "9700" }
242
<p>Given two strings <code>s</code> and <code>t</code>, return <code>true</code> if <code>t</code> is an <span data-keyword="anagram">anagram</span> of <code>s</code>, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;anagram&quot;, t = &quot;nagaram&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;rat&quot;, t = &quot;car&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>s</code> and <code>t</code> consist of lowercase English letters.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> What if the inputs contain Unicode characters? How would you adapt your solution to such a case?</p>
2
{ "code": "class Solution {\npublic:\n bool isAnagram(string s, string t) {\n map<char,int>mp;\n int n = s.size();\n int m = t.size();\n for(int i=0;i<n;i++){\n mp[s[i]]++;\n }\n if(n==m){\n for(int j=0;j<m;j++){\n mp[t[j]]--;\n \n }\n }\n else return false;\n for(auto it : mp){\n if(it.second!=0)return false;\n }\n return true;\n }\n}; ", "memory": "9800" }
242
<p>Given two strings <code>s</code> and <code>t</code>, return <code>true</code> if <code>t</code> is an <span data-keyword="anagram">anagram</span> of <code>s</code>, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;anagram&quot;, t = &quot;nagaram&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;rat&quot;, t = &quot;car&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>s</code> and <code>t</code> consist of lowercase English letters.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> What if the inputs contain Unicode characters? How would you adapt your solution to such a case?</p>
2
{ "code": "class Solution {\npublic:\n bool isAnagram(const string& s, const string& t) {\n if (s.length() != t.length()) {\n return false;\n }\n unordered_map<char, int> mp;\n for (char c : s) {\n mp[c]++;\n }\n for (char c : t) {\n mp[c]--;\n }\n for (auto& i : mp) {\n if (i.second != 0) {\n return false;\n }\n }\n return true;\n}\n};", "memory": "9800" }
242
<p>Given two strings <code>s</code> and <code>t</code>, return <code>true</code> if <code>t</code> is an <span data-keyword="anagram">anagram</span> of <code>s</code>, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;anagram&quot;, t = &quot;nagaram&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;rat&quot;, t = &quot;car&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>s</code> and <code>t</code> consist of lowercase English letters.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> What if the inputs contain Unicode characters? How would you adapt your solution to such a case?</p>
2
{ "code": "class Solution {\npublic:\n bool isAnagram(string s, string t) {\n std::map<char, int> m;\n for(int i=0;i<s.size(); i++) {\n if (m.count(s[i]) > 0) {\n m[s[i]]++;\n } else {\n m[s[i]] = 1;\n }\n }\n for(int i=0;i<t.size();i++) {\n if(m.count(t[i]) > 0) {\n if(--m[t[i]] < 0) return false;\n } else return false;\n }\n map<char, int>::iterator iter;\n for(iter = m.begin(); iter != m.end(); iter++) {\n if (iter->second != 0)\n return false;\n }\n return true;\n }\n};", "memory": "9900" }
242
<p>Given two strings <code>s</code> and <code>t</code>, return <code>true</code> if <code>t</code> is an <span data-keyword="anagram">anagram</span> of <code>s</code>, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;anagram&quot;, t = &quot;nagaram&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;rat&quot;, t = &quot;car&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>s</code> and <code>t</code> consist of lowercase English letters.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> What if the inputs contain Unicode characters? How would you adapt your solution to such a case?</p>
2
{ "code": "class Solution {\npublic:\n bool isAnagram(string s, string t) {\n if(s.size()!=t.size())return false;\n unordered_map<char,int>map;\n for(int i = 0;i<s.size();i++)map[s[i]]++;\n for(int i = 0;i<t.size();i++)map[t[i]]--;\n for(auto& pair:map)if(pair.second !=0)return false;\n return true;\n }\n};", "memory": "9900" }
242
<p>Given two strings <code>s</code> and <code>t</code>, return <code>true</code> if <code>t</code> is an <span data-keyword="anagram">anagram</span> of <code>s</code>, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;anagram&quot;, t = &quot;nagaram&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;rat&quot;, t = &quot;car&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>s</code> and <code>t</code> consist of lowercase English letters.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> What if the inputs contain Unicode characters? How would you adapt your solution to such a case?</p>
2
{ "code": "class Solution {\npublic:\n bool isAnagram(string s, string t) {\n int n = s.length();\n int l = t.length();\n if(n != l) return false;\n\n unordered_map<char, int> m, m1;\n for(int i = 0; i < n; i++){\n m[s[i]]++;\n }\n\n for(int i = 0; i< l; i++){\n m1[t[i]]++;\n }\n return m == m1;\n }\n};", "memory": "10000" }
242
<p>Given two strings <code>s</code> and <code>t</code>, return <code>true</code> if <code>t</code> is an <span data-keyword="anagram">anagram</span> of <code>s</code>, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;anagram&quot;, t = &quot;nagaram&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;rat&quot;, t = &quot;car&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>s</code> and <code>t</code> consist of lowercase English letters.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> What if the inputs contain Unicode characters? How would you adapt your solution to such a case?</p>
2
{ "code": "class Solution {\npublic:\n bool isAnagram(string s, string t) {\n unordered_map<char,int>count;\n for( auto x :s){\n count[x]++;\n }\n for(auto x : t){\n count[x]--;\n }\n\n for(auto x : count){\n if(x.second != 0){\n return false;\n }\n }\n return true;\n }\n};", "memory": "10000" }
3,262
<p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p> <p>A <strong>polygon</strong> is a closed plane figure that has at least <code>3</code> sides. The <strong>longest side</strong> of a polygon is <strong>smaller</strong> than the sum of its other sides.</p> <p>Conversely, if you have <code>k</code> (<code>k &gt;= 3</code>) <strong>positive</strong> real numbers <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code> where <code>a<sub>1</sub> &lt;= a<sub>2</sub> &lt;= a<sub>3</sub> &lt;= ... &lt;= a<sub>k</sub></code> <strong>and</strong> <code>a<sub>1</sub> + a<sub>2</sub> + a<sub>3</sub> + ... + a<sub>k-1</sub> &gt; a<sub>k</sub></code>, then there <strong>always</strong> exists a polygon with <code>k</code> sides whose lengths are <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code>.</p> <p>The <strong>perimeter</strong> of a polygon is the sum of lengths of its sides.</p> <p>Return <em>the <strong>largest</strong> possible <strong>perimeter</strong> of a <strong>polygon</strong> whose sides can be formed from</em> <code>nums</code>, <em>or</em> <code>-1</code> <em>if it is not possible to create a polygon</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,5] <strong>Output:</strong> 15 <strong>Explanation:</strong> The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,12,1,2,5,50,3] <strong>Output:</strong> 12 <strong>Explanation:</strong> The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12. We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them. It can be shown that the largest possible perimeter is 12. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,50] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 &gt; 5 + 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "#pragma GCC optimize(\"O3\", \"unroll-loops\")\nauto init = []()\n{ \n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();\nclass Solution {\npublic:\n long long largestPerimeter(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n long long sum = nums[0] + nums[1];\n long long ans = -1;\n int n_size = nums.size();\n for (int i = 2; i < n_size; i++){\n if (sum > nums[i]) ans = sum + nums[i];\n sum += nums[i];\n }\n return ans;\n }\n};", "memory": "83900" }
3,262
<p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p> <p>A <strong>polygon</strong> is a closed plane figure that has at least <code>3</code> sides. The <strong>longest side</strong> of a polygon is <strong>smaller</strong> than the sum of its other sides.</p> <p>Conversely, if you have <code>k</code> (<code>k &gt;= 3</code>) <strong>positive</strong> real numbers <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code> where <code>a<sub>1</sub> &lt;= a<sub>2</sub> &lt;= a<sub>3</sub> &lt;= ... &lt;= a<sub>k</sub></code> <strong>and</strong> <code>a<sub>1</sub> + a<sub>2</sub> + a<sub>3</sub> + ... + a<sub>k-1</sub> &gt; a<sub>k</sub></code>, then there <strong>always</strong> exists a polygon with <code>k</code> sides whose lengths are <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code>.</p> <p>The <strong>perimeter</strong> of a polygon is the sum of lengths of its sides.</p> <p>Return <em>the <strong>largest</strong> possible <strong>perimeter</strong> of a <strong>polygon</strong> whose sides can be formed from</em> <code>nums</code>, <em>or</em> <code>-1</code> <em>if it is not possible to create a polygon</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,5] <strong>Output:</strong> 15 <strong>Explanation:</strong> The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,12,1,2,5,50,3] <strong>Output:</strong> 12 <strong>Explanation:</strong> The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12. We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them. It can be shown that the largest possible perimeter is 12. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,50] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 &gt; 5 + 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long largestPerimeter(vector<int>& nums){\n sort(nums.begin(), nums.end());\n long long perimeter=nums[0]+nums[1], ans=-1;\n for(int i=2;i<nums.size();i++){\n if(perimeter>nums[i]) ans=perimeter+nums[i];\n perimeter+=nums[i];\n }\n return ans;\n }\n};", "memory": "84000" }
3,262
<p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p> <p>A <strong>polygon</strong> is a closed plane figure that has at least <code>3</code> sides. The <strong>longest side</strong> of a polygon is <strong>smaller</strong> than the sum of its other sides.</p> <p>Conversely, if you have <code>k</code> (<code>k &gt;= 3</code>) <strong>positive</strong> real numbers <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code> where <code>a<sub>1</sub> &lt;= a<sub>2</sub> &lt;= a<sub>3</sub> &lt;= ... &lt;= a<sub>k</sub></code> <strong>and</strong> <code>a<sub>1</sub> + a<sub>2</sub> + a<sub>3</sub> + ... + a<sub>k-1</sub> &gt; a<sub>k</sub></code>, then there <strong>always</strong> exists a polygon with <code>k</code> sides whose lengths are <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code>.</p> <p>The <strong>perimeter</strong> of a polygon is the sum of lengths of its sides.</p> <p>Return <em>the <strong>largest</strong> possible <strong>perimeter</strong> of a <strong>polygon</strong> whose sides can be formed from</em> <code>nums</code>, <em>or</em> <code>-1</code> <em>if it is not possible to create a polygon</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,5] <strong>Output:</strong> 15 <strong>Explanation:</strong> The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,12,1,2,5,50,3] <strong>Output:</strong> 12 <strong>Explanation:</strong> The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12. We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them. It can be shown that the largest possible perimeter is 12. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,50] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 &gt; 5 + 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\n\npublic:\n\n long long largestPerimeter(vector<int>& nums) {\n\n sort(nums.begin(), nums.end());\n\n long long previousElementsSum = 0;\n\n long long ans = -1;\n\n for (int num : nums) {\n\n if (num < previousElementsSum) {\n\n ans = num + previousElementsSum;\n\n }\n\n previousElementsSum += num;\n\n }\n\n return ans;\n\n }\n\n};\n\n\n\n\n\n \n\n \n\n \n\n\n \n\n \n\n \n\n \n\n \n\n \n\n\n \n \n", "memory": "84000" }
3,262
<p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p> <p>A <strong>polygon</strong> is a closed plane figure that has at least <code>3</code> sides. The <strong>longest side</strong> of a polygon is <strong>smaller</strong> than the sum of its other sides.</p> <p>Conversely, if you have <code>k</code> (<code>k &gt;= 3</code>) <strong>positive</strong> real numbers <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code> where <code>a<sub>1</sub> &lt;= a<sub>2</sub> &lt;= a<sub>3</sub> &lt;= ... &lt;= a<sub>k</sub></code> <strong>and</strong> <code>a<sub>1</sub> + a<sub>2</sub> + a<sub>3</sub> + ... + a<sub>k-1</sub> &gt; a<sub>k</sub></code>, then there <strong>always</strong> exists a polygon with <code>k</code> sides whose lengths are <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code>.</p> <p>The <strong>perimeter</strong> of a polygon is the sum of lengths of its sides.</p> <p>Return <em>the <strong>largest</strong> possible <strong>perimeter</strong> of a <strong>polygon</strong> whose sides can be formed from</em> <code>nums</code>, <em>or</em> <code>-1</code> <em>if it is not possible to create a polygon</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,5] <strong>Output:</strong> 15 <strong>Explanation:</strong> The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,12,1,2,5,50,3] <strong>Output:</strong> 12 <strong>Explanation:</strong> The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12. We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them. It can be shown that the largest possible perimeter is 12. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,50] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 &gt; 5 + 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long largestPerimeter(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n long long prev_sum = 0;\n long long max_peri = -1; \n for(int i = 0; i< nums.size(); i++)\n {\n if(prev_sum > nums[i])\n {\n max_peri = max(max_peri, prev_sum + nums[i]);\n }\n prev_sum += nums[i];\n }\n return max_peri;\n }\n};", "memory": "84100" }
3,262
<p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p> <p>A <strong>polygon</strong> is a closed plane figure that has at least <code>3</code> sides. The <strong>longest side</strong> of a polygon is <strong>smaller</strong> than the sum of its other sides.</p> <p>Conversely, if you have <code>k</code> (<code>k &gt;= 3</code>) <strong>positive</strong> real numbers <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code> where <code>a<sub>1</sub> &lt;= a<sub>2</sub> &lt;= a<sub>3</sub> &lt;= ... &lt;= a<sub>k</sub></code> <strong>and</strong> <code>a<sub>1</sub> + a<sub>2</sub> + a<sub>3</sub> + ... + a<sub>k-1</sub> &gt; a<sub>k</sub></code>, then there <strong>always</strong> exists a polygon with <code>k</code> sides whose lengths are <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code>.</p> <p>The <strong>perimeter</strong> of a polygon is the sum of lengths of its sides.</p> <p>Return <em>the <strong>largest</strong> possible <strong>perimeter</strong> of a <strong>polygon</strong> whose sides can be formed from</em> <code>nums</code>, <em>or</em> <code>-1</code> <em>if it is not possible to create a polygon</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,5] <strong>Output:</strong> 15 <strong>Explanation:</strong> The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,12,1,2,5,50,3] <strong>Output:</strong> 12 <strong>Explanation:</strong> The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12. We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them. It can be shown that the largest possible perimeter is 12. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,50] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 &gt; 5 + 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long largestPerimeter(vector<int>& nums) {\n // step 1 : find the sum of all the element present in the array\n long long sum = 0 ;\n for (auto it:nums){\n sum+=it ;\n }\n // step ##: sort the nums in accending order\n sort(nums.begin(),nums.end()) ;\n // step 2: traverse the array from backward \n for (int i= nums.size()-1 ; i>=0 ; i--){\n // step 3: remove the last element \n sum -= nums[i] ;\n\n // step 4: Check current sum is greater than the current element \n if(sum>nums[i]){ // if this condition is true then return the sum + current element\n return sum+nums[i] ;\n }\n }\n // step 5: Finally return the -1 showing invalad polygon \n return -1 ;\n }\n};", "memory": "84100" }
3,262
<p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p> <p>A <strong>polygon</strong> is a closed plane figure that has at least <code>3</code> sides. The <strong>longest side</strong> of a polygon is <strong>smaller</strong> than the sum of its other sides.</p> <p>Conversely, if you have <code>k</code> (<code>k &gt;= 3</code>) <strong>positive</strong> real numbers <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code> where <code>a<sub>1</sub> &lt;= a<sub>2</sub> &lt;= a<sub>3</sub> &lt;= ... &lt;= a<sub>k</sub></code> <strong>and</strong> <code>a<sub>1</sub> + a<sub>2</sub> + a<sub>3</sub> + ... + a<sub>k-1</sub> &gt; a<sub>k</sub></code>, then there <strong>always</strong> exists a polygon with <code>k</code> sides whose lengths are <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code>.</p> <p>The <strong>perimeter</strong> of a polygon is the sum of lengths of its sides.</p> <p>Return <em>the <strong>largest</strong> possible <strong>perimeter</strong> of a <strong>polygon</strong> whose sides can be formed from</em> <code>nums</code>, <em>or</em> <code>-1</code> <em>if it is not possible to create a polygon</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,5] <strong>Output:</strong> 15 <strong>Explanation:</strong> The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,12,1,2,5,50,3] <strong>Output:</strong> 12 <strong>Explanation:</strong> The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12. We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them. It can be shown that the largest possible perimeter is 12. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,50] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 &gt; 5 + 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n long long largestPerimeter(vector<int>& nums)\n {\n sort(nums.begin(),nums.end());\n\n long long sum = 0;\n\n for (int i = 0; i < nums.size(); i++)\n sum += nums[i];\n\n for (int i = nums.size()-1; i >= 0; i--)\n {\n if(sum - nums[i] > nums[i])\n return sum;\n\n sum = sum - nums[i];\n }\n\n return -1;\n }\n};", "memory": "84200" }
3,262
<p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p> <p>A <strong>polygon</strong> is a closed plane figure that has at least <code>3</code> sides. The <strong>longest side</strong> of a polygon is <strong>smaller</strong> than the sum of its other sides.</p> <p>Conversely, if you have <code>k</code> (<code>k &gt;= 3</code>) <strong>positive</strong> real numbers <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code> where <code>a<sub>1</sub> &lt;= a<sub>2</sub> &lt;= a<sub>3</sub> &lt;= ... &lt;= a<sub>k</sub></code> <strong>and</strong> <code>a<sub>1</sub> + a<sub>2</sub> + a<sub>3</sub> + ... + a<sub>k-1</sub> &gt; a<sub>k</sub></code>, then there <strong>always</strong> exists a polygon with <code>k</code> sides whose lengths are <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code>.</p> <p>The <strong>perimeter</strong> of a polygon is the sum of lengths of its sides.</p> <p>Return <em>the <strong>largest</strong> possible <strong>perimeter</strong> of a <strong>polygon</strong> whose sides can be formed from</em> <code>nums</code>, <em>or</em> <code>-1</code> <em>if it is not possible to create a polygon</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,5] <strong>Output:</strong> 15 <strong>Explanation:</strong> The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,12,1,2,5,50,3] <strong>Output:</strong> 12 <strong>Explanation:</strong> The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12. We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them. It can be shown that the largest possible perimeter is 12. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,50] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 &gt; 5 + 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long largestPerimeter(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n long long sum = nums[0] + nums[1];\n long long res = -1;\n for (int i = 2; i <nums.size(); i++){\n if (sum > nums[i]){\n res = max(res, sum + nums[i]);\n }\n sum += nums[i];\n }\n\n return res;\n }\n};", "memory": "84300" }
3,262
<p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p> <p>A <strong>polygon</strong> is a closed plane figure that has at least <code>3</code> sides. The <strong>longest side</strong> of a polygon is <strong>smaller</strong> than the sum of its other sides.</p> <p>Conversely, if you have <code>k</code> (<code>k &gt;= 3</code>) <strong>positive</strong> real numbers <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code> where <code>a<sub>1</sub> &lt;= a<sub>2</sub> &lt;= a<sub>3</sub> &lt;= ... &lt;= a<sub>k</sub></code> <strong>and</strong> <code>a<sub>1</sub> + a<sub>2</sub> + a<sub>3</sub> + ... + a<sub>k-1</sub> &gt; a<sub>k</sub></code>, then there <strong>always</strong> exists a polygon with <code>k</code> sides whose lengths are <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code>.</p> <p>The <strong>perimeter</strong> of a polygon is the sum of lengths of its sides.</p> <p>Return <em>the <strong>largest</strong> possible <strong>perimeter</strong> of a <strong>polygon</strong> whose sides can be formed from</em> <code>nums</code>, <em>or</em> <code>-1</code> <em>if it is not possible to create a polygon</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,5] <strong>Output:</strong> 15 <strong>Explanation:</strong> The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,12,1,2,5,50,3] <strong>Output:</strong> 12 <strong>Explanation:</strong> The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12. We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them. It can be shown that the largest possible perimeter is 12. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,50] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 &gt; 5 + 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long largestPerimeter(vector<int>& nums) {\n sort(nums.rbegin(), nums.rend());\n long long sum = 0;\n for (int i = 0; i < nums.size(); i++) {\n sum += nums[i];\n }\n for (int i = 0; i < nums.size(); i++) {\n if (nums[i] < sum - nums[i]) return sum;\n else {\n sum -= nums[i];\n }\n }\n return -1;\n }\n};", "memory": "84800" }
3,262
<p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p> <p>A <strong>polygon</strong> is a closed plane figure that has at least <code>3</code> sides. The <strong>longest side</strong> of a polygon is <strong>smaller</strong> than the sum of its other sides.</p> <p>Conversely, if you have <code>k</code> (<code>k &gt;= 3</code>) <strong>positive</strong> real numbers <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code> where <code>a<sub>1</sub> &lt;= a<sub>2</sub> &lt;= a<sub>3</sub> &lt;= ... &lt;= a<sub>k</sub></code> <strong>and</strong> <code>a<sub>1</sub> + a<sub>2</sub> + a<sub>3</sub> + ... + a<sub>k-1</sub> &gt; a<sub>k</sub></code>, then there <strong>always</strong> exists a polygon with <code>k</code> sides whose lengths are <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code>.</p> <p>The <strong>perimeter</strong> of a polygon is the sum of lengths of its sides.</p> <p>Return <em>the <strong>largest</strong> possible <strong>perimeter</strong> of a <strong>polygon</strong> whose sides can be formed from</em> <code>nums</code>, <em>or</em> <code>-1</code> <em>if it is not possible to create a polygon</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,5] <strong>Output:</strong> 15 <strong>Explanation:</strong> The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,12,1,2,5,50,3] <strong>Output:</strong> 12 <strong>Explanation:</strong> The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12. We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them. It can be shown that the largest possible perimeter is 12. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,50] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 &gt; 5 + 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long largestPerimeter(vector<int>& nums) {\n sort(nums.begin(),nums.end());\n int n=nums.size();\n long long prefix[n+1];\n prefix[0]=1LL*nums[0];\n for(int i=1;i<n;i++){\n prefix[i]=prefix[i-1]+1LL*nums[i];\n }\n for(int i=n-1;i>=2;i--){\n if(nums[i]<prefix[i-1]) return prefix[i];\n }\n\n\n return -1;\n\n\n }\n};", "memory": "85000" }
3,262
<p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p> <p>A <strong>polygon</strong> is a closed plane figure that has at least <code>3</code> sides. The <strong>longest side</strong> of a polygon is <strong>smaller</strong> than the sum of its other sides.</p> <p>Conversely, if you have <code>k</code> (<code>k &gt;= 3</code>) <strong>positive</strong> real numbers <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code> where <code>a<sub>1</sub> &lt;= a<sub>2</sub> &lt;= a<sub>3</sub> &lt;= ... &lt;= a<sub>k</sub></code> <strong>and</strong> <code>a<sub>1</sub> + a<sub>2</sub> + a<sub>3</sub> + ... + a<sub>k-1</sub> &gt; a<sub>k</sub></code>, then there <strong>always</strong> exists a polygon with <code>k</code> sides whose lengths are <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code>.</p> <p>The <strong>perimeter</strong> of a polygon is the sum of lengths of its sides.</p> <p>Return <em>the <strong>largest</strong> possible <strong>perimeter</strong> of a <strong>polygon</strong> whose sides can be formed from</em> <code>nums</code>, <em>or</em> <code>-1</code> <em>if it is not possible to create a polygon</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,5] <strong>Output:</strong> 15 <strong>Explanation:</strong> The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,12,1,2,5,50,3] <strong>Output:</strong> 12 <strong>Explanation:</strong> The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12. We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them. It can be shown that the largest possible perimeter is 12. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,50] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 &gt; 5 + 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long largestPerimeter(vector<int>& nums) {\n sort(nums.begin(),nums.end());\n int n= nums.size();\n int i=n-1;\n long long s=0;\n long long arr[n];\n for(int j=0;j<n;j++){\n s+=nums[j];\n arr[j]=s;\n }\n while(i>1){\n int a = nums[i];\n long long b=arr[i-1];\n if(a<b){\n b=b+a;\n return b;\n }\n i--;\n }\n return -1;\n }\n};", "memory": "85100" }
3,262
<p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p> <p>A <strong>polygon</strong> is a closed plane figure that has at least <code>3</code> sides. The <strong>longest side</strong> of a polygon is <strong>smaller</strong> than the sum of its other sides.</p> <p>Conversely, if you have <code>k</code> (<code>k &gt;= 3</code>) <strong>positive</strong> real numbers <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code> where <code>a<sub>1</sub> &lt;= a<sub>2</sub> &lt;= a<sub>3</sub> &lt;= ... &lt;= a<sub>k</sub></code> <strong>and</strong> <code>a<sub>1</sub> + a<sub>2</sub> + a<sub>3</sub> + ... + a<sub>k-1</sub> &gt; a<sub>k</sub></code>, then there <strong>always</strong> exists a polygon with <code>k</code> sides whose lengths are <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code>.</p> <p>The <strong>perimeter</strong> of a polygon is the sum of lengths of its sides.</p> <p>Return <em>the <strong>largest</strong> possible <strong>perimeter</strong> of a <strong>polygon</strong> whose sides can be formed from</em> <code>nums</code>, <em>or</em> <code>-1</code> <em>if it is not possible to create a polygon</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,5] <strong>Output:</strong> 15 <strong>Explanation:</strong> The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,12,1,2,5,50,3] <strong>Output:</strong> 12 <strong>Explanation:</strong> The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12. We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them. It can be shown that the largest possible perimeter is 12. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,50] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 &gt; 5 + 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\n public:\n auto largestPerimeter(std::vector<int> nums) -> long long {\n int n = static_cast<int>(nums.size());\n long long perimeter = std::accumulate(nums.begin(), nums.end(), 0LL);\n for (int i = 0; i < n - 2; i++) {\n std::nth_element(nums.begin() + i, nums.begin() + i, nums.end(),\n std::greater{});\n for (int var : nums) {\n cout << var << \", \";\n }\n cout << endl;\n int largest = nums[i];\n if (largest < perimeter - largest) {\n return perimeter;\n }\n perimeter -= largest;\n }\n return -1;\n }\n};", "memory": "85500" }
3,262
<p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p> <p>A <strong>polygon</strong> is a closed plane figure that has at least <code>3</code> sides. The <strong>longest side</strong> of a polygon is <strong>smaller</strong> than the sum of its other sides.</p> <p>Conversely, if you have <code>k</code> (<code>k &gt;= 3</code>) <strong>positive</strong> real numbers <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code> where <code>a<sub>1</sub> &lt;= a<sub>2</sub> &lt;= a<sub>3</sub> &lt;= ... &lt;= a<sub>k</sub></code> <strong>and</strong> <code>a<sub>1</sub> + a<sub>2</sub> + a<sub>3</sub> + ... + a<sub>k-1</sub> &gt; a<sub>k</sub></code>, then there <strong>always</strong> exists a polygon with <code>k</code> sides whose lengths are <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code>.</p> <p>The <strong>perimeter</strong> of a polygon is the sum of lengths of its sides.</p> <p>Return <em>the <strong>largest</strong> possible <strong>perimeter</strong> of a <strong>polygon</strong> whose sides can be formed from</em> <code>nums</code>, <em>or</em> <code>-1</code> <em>if it is not possible to create a polygon</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,5] <strong>Output:</strong> 15 <strong>Explanation:</strong> The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,12,1,2,5,50,3] <strong>Output:</strong> 12 <strong>Explanation:</strong> The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12. We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them. It can be shown that the largest possible perimeter is 12. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,50] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 &gt; 5 + 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "#pragma GCC optimize(\"O3\", \"unroll-loops\")\nclass Solution {\n public:\n auto largestPerimeter(std::vector<int> nums) -> long long {\n int n = static_cast<int>(nums.size());\n long long perimeter = std::accumulate(nums.begin(), nums.end(), 0LL);\n for (int i = 0; i < n - 2; i++) {\n std::nth_element(nums.begin() + i, nums.begin() + i, nums.end(),\n std::greater{});\n for (int var : nums) {\n cout << var << \", \";\n }\n cout << endl;\n int largest = nums[i];\n if (largest < perimeter - largest) {\n return perimeter;\n }\n perimeter -= largest;\n }\n return -1;\n }\n};", "memory": "85700" }
3,262
<p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p> <p>A <strong>polygon</strong> is a closed plane figure that has at least <code>3</code> sides. The <strong>longest side</strong> of a polygon is <strong>smaller</strong> than the sum of its other sides.</p> <p>Conversely, if you have <code>k</code> (<code>k &gt;= 3</code>) <strong>positive</strong> real numbers <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code> where <code>a<sub>1</sub> &lt;= a<sub>2</sub> &lt;= a<sub>3</sub> &lt;= ... &lt;= a<sub>k</sub></code> <strong>and</strong> <code>a<sub>1</sub> + a<sub>2</sub> + a<sub>3</sub> + ... + a<sub>k-1</sub> &gt; a<sub>k</sub></code>, then there <strong>always</strong> exists a polygon with <code>k</code> sides whose lengths are <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code>.</p> <p>The <strong>perimeter</strong> of a polygon is the sum of lengths of its sides.</p> <p>Return <em>the <strong>largest</strong> possible <strong>perimeter</strong> of a <strong>polygon</strong> whose sides can be formed from</em> <code>nums</code>, <em>or</em> <code>-1</code> <em>if it is not possible to create a polygon</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,5] <strong>Output:</strong> 15 <strong>Explanation:</strong> The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,12,1,2,5,50,3] <strong>Output:</strong> 12 <strong>Explanation:</strong> The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12. We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them. It can be shown that the largest possible perimeter is 12. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,50] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 &gt; 5 + 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\n public:\n auto largestPerimeter(std::vector<int> nums) -> long long {\n int n = static_cast<int>(nums.size());\n long long perimeter = std::accumulate(nums.begin(), nums.end(), 0LL);\n for (int i = 0; i < n - 2; i++) {\n std::nth_element(nums.begin() + i, nums.begin() + i, nums.end(),\n std::greater{});\n int largest = nums[i];\n if (largest < perimeter - largest) {\n return perimeter;\n }\n perimeter -= largest;\n }\n return -1;\n }\n};", "memory": "85800" }
3,262
<p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p> <p>A <strong>polygon</strong> is a closed plane figure that has at least <code>3</code> sides. The <strong>longest side</strong> of a polygon is <strong>smaller</strong> than the sum of its other sides.</p> <p>Conversely, if you have <code>k</code> (<code>k &gt;= 3</code>) <strong>positive</strong> real numbers <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code> where <code>a<sub>1</sub> &lt;= a<sub>2</sub> &lt;= a<sub>3</sub> &lt;= ... &lt;= a<sub>k</sub></code> <strong>and</strong> <code>a<sub>1</sub> + a<sub>2</sub> + a<sub>3</sub> + ... + a<sub>k-1</sub> &gt; a<sub>k</sub></code>, then there <strong>always</strong> exists a polygon with <code>k</code> sides whose lengths are <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code>.</p> <p>The <strong>perimeter</strong> of a polygon is the sum of lengths of its sides.</p> <p>Return <em>the <strong>largest</strong> possible <strong>perimeter</strong> of a <strong>polygon</strong> whose sides can be formed from</em> <code>nums</code>, <em>or</em> <code>-1</code> <em>if it is not possible to create a polygon</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,5] <strong>Output:</strong> 15 <strong>Explanation:</strong> The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,12,1,2,5,50,3] <strong>Output:</strong> 12 <strong>Explanation:</strong> The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12. We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them. It can be shown that the largest possible perimeter is 12. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,50] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 &gt; 5 + 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\n public:\n auto largestPerimeter(std::vector<int> nums) -> long long {\n int n = static_cast<int>(nums.size());\n long long perimeter = std::accumulate(nums.begin(), nums.end(), 0LL);\n for (int i = 0; i < n - 2; i++) {\n std::nth_element(nums.begin() + i, nums.begin() + i, nums.end(),\n std::greater{});\n for (int var : nums) {\n cout << var << \", \";\n }\n cout << endl;\n int largest = nums[i];\n if (largest < perimeter - largest) {\n return perimeter;\n }\n perimeter -= largest;\n }\n return -1;\n }\n};", "memory": "85800" }
3,262
<p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p> <p>A <strong>polygon</strong> is a closed plane figure that has at least <code>3</code> sides. The <strong>longest side</strong> of a polygon is <strong>smaller</strong> than the sum of its other sides.</p> <p>Conversely, if you have <code>k</code> (<code>k &gt;= 3</code>) <strong>positive</strong> real numbers <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code> where <code>a<sub>1</sub> &lt;= a<sub>2</sub> &lt;= a<sub>3</sub> &lt;= ... &lt;= a<sub>k</sub></code> <strong>and</strong> <code>a<sub>1</sub> + a<sub>2</sub> + a<sub>3</sub> + ... + a<sub>k-1</sub> &gt; a<sub>k</sub></code>, then there <strong>always</strong> exists a polygon with <code>k</code> sides whose lengths are <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code>.</p> <p>The <strong>perimeter</strong> of a polygon is the sum of lengths of its sides.</p> <p>Return <em>the <strong>largest</strong> possible <strong>perimeter</strong> of a <strong>polygon</strong> whose sides can be formed from</em> <code>nums</code>, <em>or</em> <code>-1</code> <em>if it is not possible to create a polygon</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,5] <strong>Output:</strong> 15 <strong>Explanation:</strong> The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,12,1,2,5,50,3] <strong>Output:</strong> 12 <strong>Explanation:</strong> The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12. We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them. It can be shown that the largest possible perimeter is 12. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,50] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 &gt; 5 + 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\n public:\n auto largestPerimeter(std::vector<int> nums) -> long long {\n sort(nums.begin(), nums.end());\n long long res = -1;\n long long curr = 0;\n for (int num : nums) {\n if ((curr += num) > num * 2) {\n res = curr;\n }\n }\n return res;\n }\n};\n", "memory": "86600" }
3,262
<p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p> <p>A <strong>polygon</strong> is a closed plane figure that has at least <code>3</code> sides. The <strong>longest side</strong> of a polygon is <strong>smaller</strong> than the sum of its other sides.</p> <p>Conversely, if you have <code>k</code> (<code>k &gt;= 3</code>) <strong>positive</strong> real numbers <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code> where <code>a<sub>1</sub> &lt;= a<sub>2</sub> &lt;= a<sub>3</sub> &lt;= ... &lt;= a<sub>k</sub></code> <strong>and</strong> <code>a<sub>1</sub> + a<sub>2</sub> + a<sub>3</sub> + ... + a<sub>k-1</sub> &gt; a<sub>k</sub></code>, then there <strong>always</strong> exists a polygon with <code>k</code> sides whose lengths are <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code>.</p> <p>The <strong>perimeter</strong> of a polygon is the sum of lengths of its sides.</p> <p>Return <em>the <strong>largest</strong> possible <strong>perimeter</strong> of a <strong>polygon</strong> whose sides can be formed from</em> <code>nums</code>, <em>or</em> <code>-1</code> <em>if it is not possible to create a polygon</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,5] <strong>Output:</strong> 15 <strong>Explanation:</strong> The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,12,1,2,5,50,3] <strong>Output:</strong> 12 <strong>Explanation:</strong> The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12. We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them. It can be shown that the largest possible perimeter is 12. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,50] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 &gt; 5 + 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long largestPerimeter(vector<int>& nums) {\n priority_queue<int, vector<int>, greater<int>> pq(nums.begin(), nums.end());\n long long sum = pq.top();\n pq.pop();\n sum += pq.top();\n pq.pop();\n long long ans = -1;\n while (!pq.empty()){\n if (sum > pq.top()){\n ans = sum + pq.top();\n }\n sum += pq.top();\n pq.pop();\n }\n return ans;\n \n }\n};", "memory": "86700" }
3,262
<p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p> <p>A <strong>polygon</strong> is a closed plane figure that has at least <code>3</code> sides. The <strong>longest side</strong> of a polygon is <strong>smaller</strong> than the sum of its other sides.</p> <p>Conversely, if you have <code>k</code> (<code>k &gt;= 3</code>) <strong>positive</strong> real numbers <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code> where <code>a<sub>1</sub> &lt;= a<sub>2</sub> &lt;= a<sub>3</sub> &lt;= ... &lt;= a<sub>k</sub></code> <strong>and</strong> <code>a<sub>1</sub> + a<sub>2</sub> + a<sub>3</sub> + ... + a<sub>k-1</sub> &gt; a<sub>k</sub></code>, then there <strong>always</strong> exists a polygon with <code>k</code> sides whose lengths are <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code>.</p> <p>The <strong>perimeter</strong> of a polygon is the sum of lengths of its sides.</p> <p>Return <em>the <strong>largest</strong> possible <strong>perimeter</strong> of a <strong>polygon</strong> whose sides can be formed from</em> <code>nums</code>, <em>or</em> <code>-1</code> <em>if it is not possible to create a polygon</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,5] <strong>Output:</strong> 15 <strong>Explanation:</strong> The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,12,1,2,5,50,3] <strong>Output:</strong> 12 <strong>Explanation:</strong> The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12. We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them. It can be shown that the largest possible perimeter is 12. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,50] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 &gt; 5 + 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long largestPerimeter(vector<int>& nums) {\n long long sum = accumulate(nums.begin(), nums.end(), 0LL);\n priority_queue<int> pq(nums.begin(), nums.end());\n while (pq.size() > 2) {\n int num = pq.top();\n if (sum > 2*num) return sum;\n pq.pop();\n sum -= num;\n }\n return -1;\n }\n};", "memory": "86800" }
3,262
<p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p> <p>A <strong>polygon</strong> is a closed plane figure that has at least <code>3</code> sides. The <strong>longest side</strong> of a polygon is <strong>smaller</strong> than the sum of its other sides.</p> <p>Conversely, if you have <code>k</code> (<code>k &gt;= 3</code>) <strong>positive</strong> real numbers <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code> where <code>a<sub>1</sub> &lt;= a<sub>2</sub> &lt;= a<sub>3</sub> &lt;= ... &lt;= a<sub>k</sub></code> <strong>and</strong> <code>a<sub>1</sub> + a<sub>2</sub> + a<sub>3</sub> + ... + a<sub>k-1</sub> &gt; a<sub>k</sub></code>, then there <strong>always</strong> exists a polygon with <code>k</code> sides whose lengths are <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code>.</p> <p>The <strong>perimeter</strong> of a polygon is the sum of lengths of its sides.</p> <p>Return <em>the <strong>largest</strong> possible <strong>perimeter</strong> of a <strong>polygon</strong> whose sides can be formed from</em> <code>nums</code>, <em>or</em> <code>-1</code> <em>if it is not possible to create a polygon</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,5] <strong>Output:</strong> 15 <strong>Explanation:</strong> The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,12,1,2,5,50,3] <strong>Output:</strong> 12 <strong>Explanation:</strong> The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12. We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them. It can be shown that the largest possible perimeter is 12. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,50] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 &gt; 5 + 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\nlong long findsum(vector<int>nums,int idx){\n long long ans=0;\n for(int i=0;i<=idx;i++){\n ans+=nums[i];\n }\n return ans;\n}\n long long largestPerimeter(vector<int>& nums) {\n sort(nums.begin(),nums.end());\n long long ans=-1;\n for(int i=nums.size()-1;i>=0;i--){\n long long sum=findsum(nums,i-1);\n \n if(sum>nums[i] && i+1>=3){\n ans=sum+nums[i];\n break; \n }\n }\n return ans;\n }\n};", "memory": "87000" }
3,262
<p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p> <p>A <strong>polygon</strong> is a closed plane figure that has at least <code>3</code> sides. The <strong>longest side</strong> of a polygon is <strong>smaller</strong> than the sum of its other sides.</p> <p>Conversely, if you have <code>k</code> (<code>k &gt;= 3</code>) <strong>positive</strong> real numbers <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code> where <code>a<sub>1</sub> &lt;= a<sub>2</sub> &lt;= a<sub>3</sub> &lt;= ... &lt;= a<sub>k</sub></code> <strong>and</strong> <code>a<sub>1</sub> + a<sub>2</sub> + a<sub>3</sub> + ... + a<sub>k-1</sub> &gt; a<sub>k</sub></code>, then there <strong>always</strong> exists a polygon with <code>k</code> sides whose lengths are <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code>.</p> <p>The <strong>perimeter</strong> of a polygon is the sum of lengths of its sides.</p> <p>Return <em>the <strong>largest</strong> possible <strong>perimeter</strong> of a <strong>polygon</strong> whose sides can be formed from</em> <code>nums</code>, <em>or</em> <code>-1</code> <em>if it is not possible to create a polygon</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,5] <strong>Output:</strong> 15 <strong>Explanation:</strong> The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,12,1,2,5,50,3] <strong>Output:</strong> 12 <strong>Explanation:</strong> The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12. We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them. It can be shown that the largest possible perimeter is 12. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,50] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 &gt; 5 + 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long largestPerimeter(vector<int>& a) \n {\n sort(a.begin(),a.end());\n int n=a.size();\n vector<long long>pre(n);\n pre[0]=a[0];\n for(int i=1;i<n;i++)\n {\n pre[i]=pre[i-1]+a[i];\n }\n long long ans=-1;\n for(int i=n-1;i>=2;i--)\n {\n if(pre[i-1]>a[i])\n {\n ans=pre[i-1]+a[i];\n break;\n }\n }\n return ans;\n }\n};\nauto init = []()\n{ \n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();", "memory": "88800" }
3,262
<p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p> <p>A <strong>polygon</strong> is a closed plane figure that has at least <code>3</code> sides. The <strong>longest side</strong> of a polygon is <strong>smaller</strong> than the sum of its other sides.</p> <p>Conversely, if you have <code>k</code> (<code>k &gt;= 3</code>) <strong>positive</strong> real numbers <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code> where <code>a<sub>1</sub> &lt;= a<sub>2</sub> &lt;= a<sub>3</sub> &lt;= ... &lt;= a<sub>k</sub></code> <strong>and</strong> <code>a<sub>1</sub> + a<sub>2</sub> + a<sub>3</sub> + ... + a<sub>k-1</sub> &gt; a<sub>k</sub></code>, then there <strong>always</strong> exists a polygon with <code>k</code> sides whose lengths are <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code>.</p> <p>The <strong>perimeter</strong> of a polygon is the sum of lengths of its sides.</p> <p>Return <em>the <strong>largest</strong> possible <strong>perimeter</strong> of a <strong>polygon</strong> whose sides can be formed from</em> <code>nums</code>, <em>or</em> <code>-1</code> <em>if it is not possible to create a polygon</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,5] <strong>Output:</strong> 15 <strong>Explanation:</strong> The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,12,1,2,5,50,3] <strong>Output:</strong> 12 <strong>Explanation:</strong> The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12. We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them. It can be shown that the largest possible perimeter is 12. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,50] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 &gt; 5 + 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long largestPerimeter(vector<int>& nums) {\n int n=nums.size();\n sort(nums.begin(),nums.end());\n vector<long long> prefix(n);\n for(int i=0;i<n;++i){\n if(i)prefix[i]=prefix[i-1]+nums[i];\n else prefix[i]=nums[i];\n }\n long long ans=-1;\n for(int i=2;i<n;++i){\n if(nums[i]<prefix[i-1])ans=max(ans,prefix[i]);\n }\n return ans;\n }\n};", "memory": "88900" }
3,262
<p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p> <p>A <strong>polygon</strong> is a closed plane figure that has at least <code>3</code> sides. The <strong>longest side</strong> of a polygon is <strong>smaller</strong> than the sum of its other sides.</p> <p>Conversely, if you have <code>k</code> (<code>k &gt;= 3</code>) <strong>positive</strong> real numbers <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code> where <code>a<sub>1</sub> &lt;= a<sub>2</sub> &lt;= a<sub>3</sub> &lt;= ... &lt;= a<sub>k</sub></code> <strong>and</strong> <code>a<sub>1</sub> + a<sub>2</sub> + a<sub>3</sub> + ... + a<sub>k-1</sub> &gt; a<sub>k</sub></code>, then there <strong>always</strong> exists a polygon with <code>k</code> sides whose lengths are <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code>.</p> <p>The <strong>perimeter</strong> of a polygon is the sum of lengths of its sides.</p> <p>Return <em>the <strong>largest</strong> possible <strong>perimeter</strong> of a <strong>polygon</strong> whose sides can be formed from</em> <code>nums</code>, <em>or</em> <code>-1</code> <em>if it is not possible to create a polygon</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,5] <strong>Output:</strong> 15 <strong>Explanation:</strong> The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,12,1,2,5,50,3] <strong>Output:</strong> 12 <strong>Explanation:</strong> The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12. We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them. It can be shown that the largest possible perimeter is 12. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,50] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 &gt; 5 + 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long largestPerimeter(vector<int>& nums) {\n std::sort(nums.begin(), nums.end());\n vector<long long> sum(nums.size() + 1);\n for (int i = 0; i < nums.size(); ++i) {\n sum[i + 1] = sum[i] + nums[i];\n }\n for (int i = nums.size(); i >= 3; --i) {\n if (nums[i - 1] < sum[i - 1]) return sum[i];\n }\n return -1;\n }\n};", "memory": "89000" }
3,262
<p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p> <p>A <strong>polygon</strong> is a closed plane figure that has at least <code>3</code> sides. The <strong>longest side</strong> of a polygon is <strong>smaller</strong> than the sum of its other sides.</p> <p>Conversely, if you have <code>k</code> (<code>k &gt;= 3</code>) <strong>positive</strong> real numbers <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code> where <code>a<sub>1</sub> &lt;= a<sub>2</sub> &lt;= a<sub>3</sub> &lt;= ... &lt;= a<sub>k</sub></code> <strong>and</strong> <code>a<sub>1</sub> + a<sub>2</sub> + a<sub>3</sub> + ... + a<sub>k-1</sub> &gt; a<sub>k</sub></code>, then there <strong>always</strong> exists a polygon with <code>k</code> sides whose lengths are <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code>.</p> <p>The <strong>perimeter</strong> of a polygon is the sum of lengths of its sides.</p> <p>Return <em>the <strong>largest</strong> possible <strong>perimeter</strong> of a <strong>polygon</strong> whose sides can be formed from</em> <code>nums</code>, <em>or</em> <code>-1</code> <em>if it is not possible to create a polygon</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,5] <strong>Output:</strong> 15 <strong>Explanation:</strong> The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,12,1,2,5,50,3] <strong>Output:</strong> 12 <strong>Explanation:</strong> The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12. We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them. It can be shown that the largest possible perimeter is 12. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,50] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 &gt; 5 + 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "#define ll long long\nclass Solution {\npublic:\n long long largestPerimeter(vector<int>& nums) {\n int n = nums.size();\n sort(nums.begin(),nums.end());\n vector<ll> prefix(n);\n prefix[0] = nums[0];\n for(int i=1;i<n;i++)\n {\n prefix[i] = prefix[i-1] + nums[i];\n }\n for(int i=n-1;i>0;i--)\n {\n if(nums[i]<prefix[i-1])\n return nums[i] + prefix[i-1];\n }\n return -1;\n\n }\n};", "memory": "89100" }
3,262
<p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p> <p>A <strong>polygon</strong> is a closed plane figure that has at least <code>3</code> sides. The <strong>longest side</strong> of a polygon is <strong>smaller</strong> than the sum of its other sides.</p> <p>Conversely, if you have <code>k</code> (<code>k &gt;= 3</code>) <strong>positive</strong> real numbers <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code> where <code>a<sub>1</sub> &lt;= a<sub>2</sub> &lt;= a<sub>3</sub> &lt;= ... &lt;= a<sub>k</sub></code> <strong>and</strong> <code>a<sub>1</sub> + a<sub>2</sub> + a<sub>3</sub> + ... + a<sub>k-1</sub> &gt; a<sub>k</sub></code>, then there <strong>always</strong> exists a polygon with <code>k</code> sides whose lengths are <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code>.</p> <p>The <strong>perimeter</strong> of a polygon is the sum of lengths of its sides.</p> <p>Return <em>the <strong>largest</strong> possible <strong>perimeter</strong> of a <strong>polygon</strong> whose sides can be formed from</em> <code>nums</code>, <em>or</em> <code>-1</code> <em>if it is not possible to create a polygon</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,5] <strong>Output:</strong> 15 <strong>Explanation:</strong> The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,12,1,2,5,50,3] <strong>Output:</strong> 12 <strong>Explanation:</strong> The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12. We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them. It can be shown that the largest possible perimeter is 12. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,50] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 &gt; 5 + 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long largestPerimeter(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n int n = nums.size();\n vector<long long> prefix(n,0LL);\n\n for(int i=0; i<n; i++){\n if(i==0) prefix[i] = nums[i];\n else prefix[i] = prefix[i-1] + nums[i];\n }\n long long res = -1;\n for(int i=n-1; i>=2; i--){\n if(prefix[i]-nums[i] > nums[i]){\n res = prefix[i];\n break;\n }\n }\n return res;\n }\n};", "memory": "89200" }
3,262
<p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p> <p>A <strong>polygon</strong> is a closed plane figure that has at least <code>3</code> sides. The <strong>longest side</strong> of a polygon is <strong>smaller</strong> than the sum of its other sides.</p> <p>Conversely, if you have <code>k</code> (<code>k &gt;= 3</code>) <strong>positive</strong> real numbers <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code> where <code>a<sub>1</sub> &lt;= a<sub>2</sub> &lt;= a<sub>3</sub> &lt;= ... &lt;= a<sub>k</sub></code> <strong>and</strong> <code>a<sub>1</sub> + a<sub>2</sub> + a<sub>3</sub> + ... + a<sub>k-1</sub> &gt; a<sub>k</sub></code>, then there <strong>always</strong> exists a polygon with <code>k</code> sides whose lengths are <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code>.</p> <p>The <strong>perimeter</strong> of a polygon is the sum of lengths of its sides.</p> <p>Return <em>the <strong>largest</strong> possible <strong>perimeter</strong> of a <strong>polygon</strong> whose sides can be formed from</em> <code>nums</code>, <em>or</em> <code>-1</code> <em>if it is not possible to create a polygon</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,5] <strong>Output:</strong> 15 <strong>Explanation:</strong> The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,12,1,2,5,50,3] <strong>Output:</strong> 12 <strong>Explanation:</strong> The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12. We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them. It can be shown that the largest possible perimeter is 12. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,50] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 &gt; 5 + 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long largestPerimeter(vector<int>& nums) {\n sort(nums.begin(),nums.end());\n vector<long long>preSum(nums.size(),0);\n preSum[0] = nums[0];\n for(int i=1;i<nums.size();i++){\n preSum[i] = preSum[i-1]+nums[i];\n }\n\n int i = nums.size()-1;\n int j = preSum.size()-2;\n while(j>=0 && i>=0){\n if(preSum[j]>nums[i]){\n return preSum[j+1];\n }\n i--; j--;\n }\n\n return -1;\n }\n};", "memory": "89300" }
3,262
<p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p> <p>A <strong>polygon</strong> is a closed plane figure that has at least <code>3</code> sides. The <strong>longest side</strong> of a polygon is <strong>smaller</strong> than the sum of its other sides.</p> <p>Conversely, if you have <code>k</code> (<code>k &gt;= 3</code>) <strong>positive</strong> real numbers <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code> where <code>a<sub>1</sub> &lt;= a<sub>2</sub> &lt;= a<sub>3</sub> &lt;= ... &lt;= a<sub>k</sub></code> <strong>and</strong> <code>a<sub>1</sub> + a<sub>2</sub> + a<sub>3</sub> + ... + a<sub>k-1</sub> &gt; a<sub>k</sub></code>, then there <strong>always</strong> exists a polygon with <code>k</code> sides whose lengths are <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code>.</p> <p>The <strong>perimeter</strong> of a polygon is the sum of lengths of its sides.</p> <p>Return <em>the <strong>largest</strong> possible <strong>perimeter</strong> of a <strong>polygon</strong> whose sides can be formed from</em> <code>nums</code>, <em>or</em> <code>-1</code> <em>if it is not possible to create a polygon</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,5] <strong>Output:</strong> 15 <strong>Explanation:</strong> The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,12,1,2,5,50,3] <strong>Output:</strong> 12 <strong>Explanation:</strong> The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12. We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them. It can be shown that the largest possible perimeter is 12. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,50] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 &gt; 5 + 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "#define lli long long\nclass Solution {\npublic:\n vector<int> arr;\n vector<lli> prefix;\n long long largestPerimeter(vector<int>& nums) {\n arr=nums;\n sort(arr.begin(),arr.end());\n int n=arr.size();\n prefix.resize(n+1);prefix[0]=0;\n for(int i=0;i<n;i++){\n prefix[i+1]=arr[i];\n if(i)prefix[i+1]+=prefix[i];\n } \n lli mx=-1;\n for(int i=2;i<n;i++){\n if(prefix[i]>arr[i]){\n mx=max(mx,prefix[i+1]);\n }\n }\n return mx;\n }\n};", "memory": "91700" }
3,262
<p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p> <p>A <strong>polygon</strong> is a closed plane figure that has at least <code>3</code> sides. The <strong>longest side</strong> of a polygon is <strong>smaller</strong> than the sum of its other sides.</p> <p>Conversely, if you have <code>k</code> (<code>k &gt;= 3</code>) <strong>positive</strong> real numbers <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code> where <code>a<sub>1</sub> &lt;= a<sub>2</sub> &lt;= a<sub>3</sub> &lt;= ... &lt;= a<sub>k</sub></code> <strong>and</strong> <code>a<sub>1</sub> + a<sub>2</sub> + a<sub>3</sub> + ... + a<sub>k-1</sub> &gt; a<sub>k</sub></code>, then there <strong>always</strong> exists a polygon with <code>k</code> sides whose lengths are <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code>.</p> <p>The <strong>perimeter</strong> of a polygon is the sum of lengths of its sides.</p> <p>Return <em>the <strong>largest</strong> possible <strong>perimeter</strong> of a <strong>polygon</strong> whose sides can be formed from</em> <code>nums</code>, <em>or</em> <code>-1</code> <em>if it is not possible to create a polygon</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,5] <strong>Output:</strong> 15 <strong>Explanation:</strong> The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,12,1,2,5,50,3] <strong>Output:</strong> 12 <strong>Explanation:</strong> The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12. We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them. It can be shown that the largest possible perimeter is 12. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,50] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 &gt; 5 + 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long largestPerimeter(vector<int>& nums) {\n priority_queue<long long> pq;\n long long sum=0;\n for(int i=0;i<nums.size();i++){\n sum+=nums[i];\n pq.push(nums[i]);\n }\n while(!pq.empty()){\n long long top = pq.top();\n pq.pop();\n long long diff = sum-top;\n if(diff>top){\n if(pq.size()>=2){\n return sum;\n }\n else{\n return -1;\n }\n } \n else{\n sum=sum-top;\n }\n }\n return -1;\n }\n};", "memory": "95400" }
2,696
<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p> <p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p> <p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p> <p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,6], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6]. It can be proved that there are only 4 beautiful subsets in the array [2,4,6]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1], k = 1 <strong>Output:</strong> 1 <strong>Explanation:</strong> The beautiful subset of the array nums is [1]. It can be proved that there is only 1 beautiful subset in the array [1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 20</code></li> <li><code>1 &lt;= nums[i], k &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\n public:\n int solve(vector<int>&nums,int index,int k,unordered_map<int,int>&mp){\n if(index>=nums.size())return 1;\n int notTake=solve(nums,index+1,k,mp);\n int take=0;\n if(!mp[nums[index] - k] && !mp[nums[index] + k]){\n mp[nums[index]]++;\n take=solve(nums,index+1,k,mp);\n mp[nums[index]]--;\n \n }\n return take+notTake;\n } \n public:\n int solve(int i,vector<int>&a,int n,int k,int prev,vector<vector<int>>& dp){\n if(i>=n)return 1;\n if(dp[i][prev]!=-1)return dp[i][prev];\n int notTake=solve(i+1,a,n,k,prev,dp);\n int take=0;\n if(prev==0||abs(a[i]-prev)!=k){\n take=solve(i+1,a,n,k,a[i],dp);\n }\n return dp[i][prev]= take+notTake;\n }\npublic:\n int beautifulSubsets(vector<int>& nums, int k) {\n int n=nums.size();\n sort(nums.begin(),nums.end());\n vector<vector<int>> dp(k);\n for(auto it:nums){\n dp[it%k].push_back(it);\n }\n int ans=1;\n for(int i=0;i<k;i++){\n if(dp[i].size()>0){\n int m=dp[i].size();\n vector<vector<int>> dp1(m,vector<int>(dp[i][m-1]+1,-1));\n ans=ans*solve(0,dp[i],m,k,0,dp1);\n }\n }\n return ans-1;\n }\n};", "memory": "63782" }
2,696
<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p> <p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p> <p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p> <p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,6], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6]. It can be proved that there are only 4 beautiful subsets in the array [2,4,6]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1], k = 1 <strong>Output:</strong> 1 <strong>Explanation:</strong> The beautiful subset of the array nums is [1]. It can be proved that there is only 1 beautiful subset in the array [1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 20</code></li> <li><code>1 &lt;= nums[i], k &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int help(map<int,int>& arr,int k){\n int next1=1;int next2=1;\n for(auto it=arr.rbegin();it!=arr.rend();it++){\n int pick=0;\n \n if(it!=arr.rbegin()){\n if(arr.find(it->first+k)!=arr.end()){\n pick=(pow(2,it->second)-1)*next2;\n }\n else{\n pick=(pow(2,it->second)-1)*next1;\n }\n }\n else{\n pick=(pow(2,it->second)-1);\n }\n int notpick=next1;\n int curr=pick+notpick;\n next2=next1;\n next1=curr;\n }\n return next1;\n }\n int beautifulSubsets(vector<int>& nums, int k) {\n int ans=1;\n sort(nums.begin(),nums.end());\n unordered_map<int,map<int,int>> mp;\n for(int i=0;i<nums.size();i++){\n mp[nums[i]%k][nums[i]]++;\n }\n for(int i=0;i<k;i++){\n int res=help(mp[i],k);\n // cout<<i<<\" \"<<res<<endl;\n ans=ans*res;\n }\n return ans-1;\n }\n};", "memory": "69867" }
2,696
<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p> <p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p> <p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p> <p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,6], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6]. It can be proved that there are only 4 beautiful subsets in the array [2,4,6]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1], k = 1 <strong>Output:</strong> 1 <strong>Explanation:</strong> The beautiful subset of the array nums is [1]. It can be proved that there is only 1 beautiful subset in the array [1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 20</code></li> <li><code>1 &lt;= nums[i], k &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int beautifulSubsets(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> valid(1 << n, 0);\n valid[0] = 1;\n int res = 0;\n for (int state = 1; state < (1 << n); state += 1) {\n int i = n - 1;\n while (i >= 0 && !(state & (1 << i))) i -= 1;\n bool flag = true;\n for (int j = 0; j < i; j += 1) {\n if (!(state & (1 << j))) continue;\n if (abs(nums[i] - nums[j]) == k) {\n flag = false;\n break;\n }\n }\n if (flag) {\n int prev = state ^ (1 << i);\n valid[state] = valid[prev];\n }\n if (valid[state]) res += 1;\n }\n return res;\n \n }\n};", "memory": "75952" }
2,696
<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p> <p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p> <p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p> <p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,6], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6]. It can be proved that there are only 4 beautiful subsets in the array [2,4,6]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1], k = 1 <strong>Output:</strong> 1 <strong>Explanation:</strong> The beautiful subset of the array nums is [1]. It can be proved that there is only 1 beautiful subset in the array [1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 20</code></li> <li><code>1 &lt;= nums[i], k &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int dp[21][1<<20];\n int help(int i,int n,vector<int>&nums,int k,int mask){\n if(i==n) return 1;\n if(dp[i][mask]!=-1) return dp[i][mask];\n int t=0,nt=0;\n nt=help(i+1,n,nums,k,mask);\n bool c=1;\n for(int j=0; j<n; j++){\n if(mask&(1<<j) and abs(nums[j]-nums[i])==k){\n c=0;\n break;\n }\n }\n if(c) t=help(i+1,n,nums,k,mask|(1<<i));\n return dp[i][mask]=t+nt;\n }\n int beautifulSubsets(vector<int>& nums, int k) {\n int n=nums.size();\n memset(dp,-1,sizeof(dp));\n return help(0,n,nums,k,0)-1;\n }\n};", "memory": "82037" }
2,696
<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p> <p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p> <p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p> <p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,6], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6]. It can be proved that there are only 4 beautiful subsets in the array [2,4,6]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1], k = 1 <strong>Output:</strong> 1 <strong>Explanation:</strong> The beautiful subset of the array nums is [1]. It can be proved that there is only 1 beautiful subset in the array [1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 20</code></li> <li><code>1 &lt;= nums[i], k &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int beautifulSubsets(vector<int>& nums, int k) {\n int n =int(nums.size());\n\n int r=n;\n vector<int> nums2;\n\n for(int i=0;i<n;++i)\n {\n nums2.clear();\n \n for(int j=i+1;j<n;++j)\n if(abs(nums[i]-nums[j])!=k)\n nums2.push_back(nums[j]);\n\n r+= beautifulSubsets(nums2,k);\n }\n\n return r;\n }\n\n};", "memory": "88122" }
2,696
<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p> <p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p> <p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p> <p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,6], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6]. It can be proved that there are only 4 beautiful subsets in the array [2,4,6]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1], k = 1 <strong>Output:</strong> 1 <strong>Explanation:</strong> The beautiful subset of the array nums is [1]. It can be proved that there is only 1 beautiful subset in the array [1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 20</code></li> <li><code>1 &lt;= nums[i], k &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int beautifulSubsets(vector<int>& nums, int k) {\n\n //sort(nums.begin(),nums.end());\n\n int n =int(nums.size());\n\n int r=n;\n\n\n vector<int> nums2;\n for(int i=0;i<n;++i)\n {\n nums2.clear();\n \n for(int j=i+1;j<n;++j)\n if(abs(nums[i]-nums[j])!=k)\n nums2.push_back(nums[j]);\n\n r+= beautifulSubsets(nums2,k);\n }\n\n return r;\n }\n\n};", "memory": "94207" }
2,696
<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p> <p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p> <p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p> <p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,6], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6]. It can be proved that there are only 4 beautiful subsets in the array [2,4,6]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1], k = 1 <strong>Output:</strong> 1 <strong>Explanation:</strong> The beautiful subset of the array nums is [1]. It can be proved that there is only 1 beautiful subset in the array [1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 20</code></li> <li><code>1 &lt;= nums[i], k &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int beautifulSubsets(vector<int>& nums, int k) {\n\n //sort(nums.begin(),nums.end());\n\n int n =int(nums.size());\n\n int r=n;\n\n\n vector<int> nums2;\n for(int i=0;i<n;++i)\n {\n nums2.clear();\n \n for(int j=i+1;j<n;++j)\n if(abs(nums[i]-nums[j])!=k)\n nums2.push_back(nums[j]);\n\n r+= beautifulSubsets(nums2,k);\n }\n\n return r;\n }\n\n};", "memory": "100292" }
2,696
<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p> <p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p> <p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p> <p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,6], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6]. It can be proved that there are only 4 beautiful subsets in the array [2,4,6]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1], k = 1 <strong>Output:</strong> 1 <strong>Explanation:</strong> The beautiful subset of the array nums is [1]. It can be proved that there is only 1 beautiful subset in the array [1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 20</code></li> <li><code>1 &lt;= nums[i], k &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int beautifulSubsets(vector<int>& nums, int k) {\n int n =int(nums.size());\n\n int r=n;\n vector<int> nums2;\n nums2.reserve(n);\n \n for(int i=0;i<n;++i)\n {\n nums2.clear();\n \n for(int j=i+1;j<n;++j)\n if(abs(nums[i]-nums[j])!=k)\n nums2.push_back(nums[j]);\n\n r+= beautifulSubsets(nums2,k);\n }\n\n return r;\n }\n\n};", "memory": "106377" }
2,696
<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p> <p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p> <p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p> <p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,6], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6]. It can be proved that there are only 4 beautiful subsets in the array [2,4,6]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1], k = 1 <strong>Output:</strong> 1 <strong>Explanation:</strong> The beautiful subset of the array nums is [1]. It can be proved that there is only 1 beautiful subset in the array [1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 20</code></li> <li><code>1 &lt;= nums[i], k &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int beautifulSubsets(vector<int>& nums, int k) {\n int n =int(nums.size());\n\n int r=n;\n vector<int> nums2;\n nums2.reserve(n);\n \n for(int i=0;i<n;++i)\n {\n nums2.clear();\n \n for(int j=i+1;j<n;++j)\n if(abs(nums[i]-nums[j])!=k)\n nums2.push_back(nums[j]);\n\n r+= beautifulSubsets(nums2,k);\n }\n\n return r;\n }\n\n};", "memory": "106377" }
2,696
<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p> <p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p> <p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p> <p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,6], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6]. It can be proved that there are only 4 beautiful subsets in the array [2,4,6]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1], k = 1 <strong>Output:</strong> 1 <strong>Explanation:</strong> The beautiful subset of the array nums is [1]. It can be proved that there is only 1 beautiful subset in the array [1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 20</code></li> <li><code>1 &lt;= nums[i], k &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\n unordered_map<int, int> mp;\n int ans = 0;\n\npublic:\n void solve(vector<int>& nums, int k, int start) {\n // Increment ans for each valid subset\n if (!mp.empty()) ans++;\n\n // Iterate through the remaining elements\n for (int i = start; i < nums.size(); i++) {\n // Check if current element can be added\n if (mp.find(nums[i] - k) == mp.end() && mp.find(nums[i] + k) == mp.end()) {\n mp[nums[i]]++;\n solve(nums, k, i + 1);\n mp[nums[i]]--;\n if (mp[nums[i]] == 0) mp.erase(nums[i]);\n }\n }\n }\n\n int beautifulSubsets(vector<int>& nums, int k) {\n solve(nums, k, 0);\n return ans;\n }\n};\n", "memory": "112462" }
2,696
<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p> <p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p> <p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p> <p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,6], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6]. It can be proved that there are only 4 beautiful subsets in the array [2,4,6]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1], k = 1 <strong>Output:</strong> 1 <strong>Explanation:</strong> The beautiful subset of the array nums is [1]. It can be proved that there is only 1 beautiful subset in the array [1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 20</code></li> <li><code>1 &lt;= nums[i], k &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int beautifulSubsets(vector<int>& nums, int k) {\n \n dfs(nums, k, 0);\n return _ct;\n }\n\nprivate:\n int _ct = 0;\n unordered_map<int, int> _map;\n\n void dfs(const vector<int>& nums, int k, int start) {\n if (_map.size() != 0) _ct++;\n \n for (int i = start; i <nums.size(); i++) {\n if (_map.find(nums[i] - k) != _map.end() || _map.find(nums[i] + k) != _map.end()) {\n continue;\n }\n _map[nums[i]]++;\n dfs(nums, k, i + 1);\n _map[nums[i]]--;\n if (_map[nums[i]] == 0) {\n _map.erase(nums[i]);\n }\n }\n }\n\n};", "memory": "118547" }
2,696
<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p> <p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p> <p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p> <p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,6], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6]. It can be proved that there are only 4 beautiful subsets in the array [2,4,6]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1], k = 1 <strong>Output:</strong> 1 <strong>Explanation:</strong> The beautiful subset of the array nums is [1]. It can be proved that there is only 1 beautiful subset in the array [1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 20</code></li> <li><code>1 &lt;= nums[i], k &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\n unordered_map<int, int> mp;\n int ans = 0;\n\npublic:\n void solve(vector<int>& nums, int k, int start) {\n // Increment ans for each valid subset\n if (!mp.empty()) ans++;\n\n // Iterate through the remaining elements\n for (int i = start; i < nums.size(); i++) {\n // Check if current element can be added\n if (mp.find(nums[i] - k) == mp.end() && mp.find(nums[i] + k) == mp.end()) {\n mp[nums[i]]++;\n solve(nums, k, i + 1);\n mp[nums[i]]--;\n if (mp[nums[i]] == 0) mp.erase(nums[i]);\n }\n }\n }\n\n int beautifulSubsets(vector<int>& nums, int k) {\n solve(nums, k, 0);\n return ans;\n }\n};", "memory": "118547" }
2,696
<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p> <p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p> <p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p> <p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,6], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6]. It can be proved that there are only 4 beautiful subsets in the array [2,4,6]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1], k = 1 <strong>Output:</strong> 1 <strong>Explanation:</strong> The beautiful subset of the array nums is [1]. It can be proved that there is only 1 beautiful subset in the array [1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 20</code></li> <li><code>1 &lt;= nums[i], k &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n void helper(vector<int> &nums,int k,int &ans,int idx,unordered_map<int,int> &m1){\n if(idx==nums.size()){\n return;\n }\n for(int i=idx;i<nums.size();i++){\n if((m1.find(nums[i]+k)==m1.end() && m1.find(nums[i]-k)==m1.end())){\n m1[nums[i]]++;\n ans++;\n helper(nums,k,ans,i+1,m1);\n m1[nums[i]]--;\n if (m1[nums[i]] == 0) {\n m1.erase(nums[i]);\n }\n }\n }\n }\n int beautifulSubsets(vector<int>& nums, int k) {\n int ans=0;\n unordered_map<int,int> m1;\n helper(nums,k,ans,0,m1);\n return ans;\n }\n};", "memory": "124632" }
2,696
<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p> <p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p> <p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p> <p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,6], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6]. It can be proved that there are only 4 beautiful subsets in the array [2,4,6]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1], k = 1 <strong>Output:</strong> 1 <strong>Explanation:</strong> The beautiful subset of the array nums is [1]. It can be proved that there is only 1 beautiful subset in the array [1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 20</code></li> <li><code>1 &lt;= nums[i], k &lt;= 1000</code></li> </ul>
3
{ "code": "// Tag: Backtracking\n\n// Create Date: 2024/5/23\n// Review Date: [2024/5/30, 2024/6/25]\n\n#include <bits/stdc++.h> \nusing namespace std;\n\n// O(2^n)Time, O(n)Space, 因为potentially需要generate all subsets\nclass Solution {\npublic:\n void backtracking(const vector<int>& nums, const int k,\n const int idx, unordered_map<int, int>& um, int& ans)\n {\n if (!um.empty())\n {\n ++ans;\n }\n\n for (int i = idx; i < nums.size(); ++i)\n {\n const int a = nums[i] + k;\n const int b = nums[i] - k;\n if (um.find(a) == um.end() && um.find(b) == um.end())\n {\n // !!! 必须用um而不是unordered_set,否则此时的call有可能erase掉之前的,\n // which is the case if there is any duplicate\n ++um[nums[i]];\n backtracking(nums, k, i + 1, um, ans);\n\n if (um[nums[i]] == 1)\n {\n um.erase(nums[i]);\n }\n else\n {\n --um[nums[i]];\n }\n }\n }\n }\n\n int beautifulSubsets(vector<int>& nums, int k) {\n // unordered_set<int> us;\n unordered_map<int, int> um;\n int ans = 0;\n backtracking(nums, k, 0, um, ans);\n\n return ans;\n }\n};", "memory": "130717" }
2,696
<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p> <p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p> <p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p> <p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,6], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6]. It can be proved that there are only 4 beautiful subsets in the array [2,4,6]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1], k = 1 <strong>Output:</strong> 1 <strong>Explanation:</strong> The beautiful subset of the array nums is [1]. It can be proved that there is only 1 beautiful subset in the array [1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 20</code></li> <li><code>1 &lt;= nums[i], k &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n void helper(vector<int> &nums,int k,int &ans,int idx,unordered_map<int,int> &m1){\n if(idx==nums.size()){\n return;\n }\n for(int i=idx;i<nums.size();i++){\n if((m1.find(nums[i]+k)==m1.end() && m1.find(nums[i]-k)==m1.end())){\n m1[nums[i]]++;\n ans++;\n helper(nums,k,ans,i+1,m1);\n m1[nums[i]]--;\n if (m1[nums[i]] == 0) {\n m1.erase(nums[i]);\n }\n }\n }\n }\n int beautifulSubsets(vector<int>& nums, int k) {\n int ans=0;\n vector<int>v1;\n unordered_map<int,int> m1;\n sort(nums.begin(),nums.end());\n helper(nums,k,ans,0,m1);\n return ans;\n }\n};", "memory": "136802" }
2,696
<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p> <p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p> <p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p> <p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,6], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6]. It can be proved that there are only 4 beautiful subsets in the array [2,4,6]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1], k = 1 <strong>Output:</strong> 1 <strong>Explanation:</strong> The beautiful subset of the array nums is [1]. It can be proved that there is only 1 beautiful subset in the array [1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 20</code></li> <li><code>1 &lt;= nums[i], k &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int beautifulSubsets(vector<int>& nums, int k) {\n\n sort(nums.begin(),nums.end());\n\n int n =int(nums.size());\n\n int r=n;\n for(int i=0;i<n;++i)\n {\n vector<int> nums2;\n \n for(int j=i+1;j<n;++j)\n if(abs(nums[i]-nums[j])!=k)\n nums2.push_back(nums[j]);\n\n r+= beautifulSubsets(nums2,k);\n }\n\n return r;\n }\n\n};", "memory": "142887" }
2,696
<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p> <p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p> <p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p> <p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,6], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6]. It can be proved that there are only 4 beautiful subsets in the array [2,4,6]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1], k = 1 <strong>Output:</strong> 1 <strong>Explanation:</strong> The beautiful subset of the array nums is [1]. It can be proved that there is only 1 beautiful subset in the array [1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 20</code></li> <li><code>1 &lt;= nums[i], k &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n /**s contiene gli indici degli elementi appartenenti alla subset\n subset distinte !!!**/\n void dfs(vector<int> &s,int x,vector<int> &A,int k,int &number,unordered_map<int,int> &cnt){\n \n vector<int> candidates;\n\n /**se la differenza tra cnt[A[i] -k] è maggiore di 0 vuol dire \n che non lo posso inserire,altrimenti se vale 0 lo posso includere**/\n int pos = (x==0) ? 0 : s[x-1]+1;\n \n for(int i=pos;i<A.size();i++){\n if(cnt[A[i]-k] == 0 && cnt[A[i]+k] == 0){\n candidates.push_back(i);\n }\n }\n\n x++;\n for(int i=0;i<candidates.size();i++){\n s.push_back(candidates[i]);\n cnt[A[s[x-1]]]++;\n number++;\n dfs(s,x,A,k,number,cnt);\n cnt[A[s[x-1]]]--;\n s.pop_back();\n }\n return ;\n }\n\n\n int beautifulSubsets(vector<int>& nums, int k) {\n \n sort(nums.begin(),nums.end());\n vector<int> s;\n int number = 0;\n\n unordered_map<int, int> cnt;\n\n dfs(s,0,nums,k,number,cnt);\n\n return number;\n }\n};", "memory": "148972" }
2,696
<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p> <p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p> <p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p> <p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,6], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6]. It can be proved that there are only 4 beautiful subsets in the array [2,4,6]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1], k = 1 <strong>Output:</strong> 1 <strong>Explanation:</strong> The beautiful subset of the array nums is [1]. It can be proved that there is only 1 beautiful subset in the array [1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 20</code></li> <li><code>1 &lt;= nums[i], k &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n /**s contiene gli indici degli elementi appartenenti alla subset\n subset distinte !!!**/\n void dfs(vector<int> &s,int x,vector<int> &A,int k,int &number,vector<int> &cnt){\n \n vector<int> candidates;\n\n /**se la differenza tra cnt[A[i] -k] è maggiore di 0 vuol dire \n che non lo posso inserire,altrimenti se vale 0 lo posso includere**/\n int pos = (x==0) ? 0 : s[x-1]+1;\n \n for(int i=pos;i<A.size();i++){\n if((A[i] - k) < 0){\n candidates.push_back(i);\n }else{\n if(cnt[A[i]-k]==0){\n candidates.push_back(i);\n }\n }\n }\n\n x++;\n for(int i=0;i<candidates.size();i++){\n s.push_back(candidates[i]);\n cnt[A[s[x-1]]]++;\n number++;\n dfs(s,x,A,k,number,cnt);\n cnt[A[s[x-1]]]--;\n s.pop_back();\n }\n\n return ;\n }\n\n\n int beautifulSubsets(vector<int>& nums, int k) {\n \n sort(nums.begin(),nums.end());\n vector<int> s;\n int number = 0;\n\n vector<int> cnt(1001,0);\n\n dfs(s,0,nums,k,number,cnt);\n\n return number;\n }\n};", "memory": "155057" }
2,696
<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p> <p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p> <p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p> <p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,6], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6]. It can be proved that there are only 4 beautiful subsets in the array [2,4,6]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1], k = 1 <strong>Output:</strong> 1 <strong>Explanation:</strong> The beautiful subset of the array nums is [1]. It can be proved that there is only 1 beautiful subset in the array [1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 20</code></li> <li><code>1 &lt;= nums[i], k &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int beautifulSubsets(vector<int>& nums, int k) {\n unordered_multiset<int> st;\n int res = 0;\n for (int i = 0; i < nums.size(); ++i)\n dfs(nums, i, st, k, res);\n return res;\n }\n\nprivate:\n void dfs(const vector<int>& nums, int i, unordered_multiset<int>& st, int k, int& res) {\n if (i == nums.size() || st.find(nums[i] - k) != st.end() || st.find(nums[i] + k) != st.end())\n return;\n st.insert(nums[i]);\n ++res;\n for (int j = i + 1; j < nums.size(); ++j)\n dfs(nums, j, st, k, res);\n st.erase(st.find(nums[i]));\n }\n};", "memory": "161142" }
2,696
<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p> <p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p> <p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p> <p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,6], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6]. It can be proved that there are only 4 beautiful subsets in the array [2,4,6]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1], k = 1 <strong>Output:</strong> 1 <strong>Explanation:</strong> The beautiful subset of the array nums is [1]. It can be proved that there is only 1 beautiful subset in the array [1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 20</code></li> <li><code>1 &lt;= nums[i], k &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n /**s contiene gli indici degli elementi appartenenti alla subset\n subset distinte !!!**/\n void dfs(vector<int> &s,int x,vector<int> &A,int k,int &number,vector<int> &cnt){\n \n vector<int> candidates;\n\n /**se la differenza tra cnt[A[i] -k] è maggiore di 0 vuol dire \n che non lo posso inserire,altrimenti se vale 0 lo posso includere**/\n int pos = (x==0) ? 0 : s[x-1]+1;\n \n for(int i=pos;i<A.size();i++){\n if((A[i] - k) < 0){\n candidates.push_back(i);\n }else{\n if(cnt[A[i]-k]==0){\n candidates.push_back(i);\n }\n }\n }\n\n x++;\n for(int i=0;i<candidates.size();i++){\n s.push_back(candidates[i]);\n cnt[A[s[x-1]]]++;\n number++;\n dfs(s,x,A,k,number,cnt);\n cnt[A[s[x-1]]]--;\n s.pop_back();\n }\n\n return ;\n }\n\n\n int beautifulSubsets(vector<int>& nums, int k) {\n \n sort(nums.begin(),nums.end());\n vector<int> s;\n int number = 0;\n\n vector<int> cnt(2001,0);\n\n dfs(s,0,nums,k,number,cnt);\n\n return number;\n }\n};", "memory": "167227" }
2,696
<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p> <p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p> <p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p> <p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,6], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6]. It can be proved that there are only 4 beautiful subsets in the array [2,4,6]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1], k = 1 <strong>Output:</strong> 1 <strong>Explanation:</strong> The beautiful subset of the array nums is [1]. It can be proved that there is only 1 beautiful subset in the array [1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 20</code></li> <li><code>1 &lt;= nums[i], k &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n /**s contiene gli indici degli elementi appartenenti alla subset\n subset distinte !!!**/\n void dfs(vector<int> &s,int x,vector<int> &A,int k,int &number,vector<int> &cnt){\n \n vector<int> candidates;\n\n /**se la differenza tra cnt[A[i] -k] è maggiore di 0 vuol dire \n che non lo posso inserire,altrimenti se vale 0 lo posso includere**/\n int pos = (x==0) ? 0 : s[x-1]+1;\n \n for(int i=pos;i<A.size();i++){\n if((A[i] - k) < 0){\n if(cnt[A[i]+k] == 0){\n candidates.push_back(i);\n }\n }else{\n if(cnt[A[i]-k]==0 && cnt[A[i]+k] == 0){\n candidates.push_back(i);\n }\n }\n }\n\n x++;\n for(int i=0;i<candidates.size();i++){\n s.push_back(candidates[i]);\n cnt[A[s[x-1]]]++;\n number++;\n dfs(s,x,A,k,number,cnt);\n cnt[A[s[x-1]]]--;\n s.pop_back();\n }\n return ;\n }\n\n\n int beautifulSubsets(vector<int>& nums, int k) {\n \n sort(nums.begin(),nums.end());\n vector<int> s;\n int number = 0;\n\n vector<int> cnt(2001,0);\n\n dfs(s,0,nums,k,number,cnt);\n\n return number;\n }\n};", "memory": "173312" }
2,696
<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p> <p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p> <p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p> <p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,6], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6]. It can be proved that there are only 4 beautiful subsets in the array [2,4,6]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1], k = 1 <strong>Output:</strong> 1 <strong>Explanation:</strong> The beautiful subset of the array nums is [1]. It can be proved that there is only 1 beautiful subset in the array [1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 20</code></li> <li><code>1 &lt;= nums[i], k &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int beautifulSubsets(vector<int>& nums, int k) {\n int count = -1;\n vector<int> curr;\n unordered_multiset<int> s;\n makeSubsets(0, curr, nums, k, count, s);\n\n return count;\n\n }\n\n void makeSubsets(int index, vector<int> &curr, vector<int> &nums, int k, int &count, unordered_multiset<int> &s){\n count++;\n\n for(int end = index; end<nums.size(); end++){\n if(!s.contains((nums[end]-k)) and !s.contains(nums[end]+k)){\n curr.push_back(nums[end]);\n s.insert(nums[end]);\n makeSubsets(end+1, curr, nums, k, count ,s);\n curr.pop_back();\n s.erase(s.find(nums[end]));\n }\n }\n } \n};", "memory": "179397" }
2,696
<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p> <p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p> <p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p> <p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,6], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6]. It can be proved that there are only 4 beautiful subsets in the array [2,4,6]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1], k = 1 <strong>Output:</strong> 1 <strong>Explanation:</strong> The beautiful subset of the array nums is [1]. It can be proved that there is only 1 beautiful subset in the array [1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 20</code></li> <li><code>1 &lt;= nums[i], k &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int f(int i,int mask,vector<int>&nums,vector<vector<int>>&dp,int &k){\n if(i<0){\n \n if(mask!=0)return 1;\n return 0;\n }\n if(dp[i][mask]!=-1)return dp[i][mask];\n int nottake=f(i-1,mask,nums,dp,k);\n int take=0;\n int f1=0;\n for(int j=0;j<31;j++){\n if((mask&(1<<j))!=0){\n if(abs(nums[i]-nums[j])==k){\n f1=1;\n break;\n }\n }\n }\n if(f1==0){\n mask=mask|(1<<i);\n take=f(i-1,mask,nums,dp,k);\n mask=mask^(1<<i);\n }\n return dp[i][mask]=take+nottake;\n }\n int beautifulSubsets(vector<int>& nums, int k) {\n int n=nums.size();\n int mask=0;\n vector<vector<int>> dp(n,vector<int>(((1<<n)),-1));\n return f(n-1,mask,nums,dp,k);\n \n }\n};", "memory": "185482" }
2,696
<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p> <p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p> <p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p> <p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,6], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6]. It can be proved that there are only 4 beautiful subsets in the array [2,4,6]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1], k = 1 <strong>Output:</strong> 1 <strong>Explanation:</strong> The beautiful subset of the array nums is [1]. It can be proved that there is only 1 beautiful subset in the array [1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 20</code></li> <li><code>1 &lt;= nums[i], k &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int f(int i,int mask,vector<int>&nums,vector<vector<int>>&dp,int &k){\n if(i<0){\n \n if(mask!=0)return 1;\n return 0;\n }\n if(dp[i][mask]!=-1)return dp[i][mask];\n int nottake=f(i-1,mask,nums,dp,k);\n int take=0;\n int f1=0;\n for(int j=0;j<31;j++){\n if((mask&(1<<j))!=0){\n if(abs(nums[i]-nums[j])==k){\n f1=1;\n break;\n }\n }\n }\n if(f1==0){\n mask=mask|(1<<i);\n take=f(i-1,mask,nums,dp,k);\n }\n return dp[i][mask]=take+nottake;\n }\n int beautifulSubsets(vector<int>& nums, int k) {\n int n=nums.size();\n int mask=0;\n vector<vector<int>> dp(n,vector<int>(((1<<n)),-1));\n return f(n-1,mask,nums,dp,k);\n \n }\n};", "memory": "191567" }
2,696
<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p> <p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p> <p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p> <p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,6], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6]. It can be proved that there are only 4 beautiful subsets in the array [2,4,6]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1], k = 1 <strong>Output:</strong> 1 <strong>Explanation:</strong> The beautiful subset of the array nums is [1]. It can be proved that there is only 1 beautiful subset in the array [1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 20</code></li> <li><code>1 &lt;= nums[i], k &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int beautifulSubsets(vector<int>& nums, int k) {\n map<vector<int>,int> cache;\n\n vector<bool> marked(nums.size(),false);\n\n return imp(nums,nums.size(),k,0,cache,marked);\n }\n\n int imp(vector<int>& nums, int n, int k,int start,map<vector<int>,int>& cache,vector<bool>& marked) {\n // auto f=cache.find(nums);\n // if(f!=cache.end()) return f->second;\n\n int r=0;\n\n for(int i=start;i<n;++i) {\n if(marked[i]) continue;\n\n r++;\n\n auto m2=marked;\n for(int j=i+1;j<n;++j)\n if(abs(nums[j]-nums[i])==k)\n marked[j]=true;\n\n r+= imp(nums,n, k,i+1, cache,marked);\n\n marked = m2;\n }\n\n // cache[nums]=r;\n return r;\n }\n};", "memory": "197652" }
2,696
<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p> <p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p> <p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p> <p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,6], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6]. It can be proved that there are only 4 beautiful subsets in the array [2,4,6]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1], k = 1 <strong>Output:</strong> 1 <strong>Explanation:</strong> The beautiful subset of the array nums is [1]. It can be proved that there is only 1 beautiful subset in the array [1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 20</code></li> <li><code>1 &lt;= nums[i], k &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int dfs(vector<int> &nums, int idx, int mask, int k, vector<vector<int>> &dp) {\n int n = nums.size();\n if (idx == n) return 1; // Base case: consider all elements\n\n if (dp[idx][mask] != -1) return dp[idx][mask]; // Return memoized result\n\n // Option 1: Do not take the current element\n int notTaken = dfs(nums, idx + 1, mask, k, dp);\n\n // Option 2: Take the current element\n bool canTake = true;\n for (int j = 0; j < n; ++j) {\n if ((mask & (1 << j)) && (abs(nums[idx] - nums[j]) == k)) {\n canTake = false;\n break;\n }\n }\n\n int taken = 0;\n if (canTake) {\n taken = dfs(nums, idx + 1, mask | (1 << idx), k, dp);\n }\n\n return dp[idx][mask] = taken + notTaken; // Store and return result\n }\n\n int beautifulSubsets(vector<int> &nums, int k) {\n int n = nums.size();\n vector<vector<int>> dp(n + 1, vector<int>(1 << n, -1)); // DP table\n int result = dfs(nums, 0, 0, k, dp);\n return result - 1; // Subtract 1 to exclude the empty subset\n }\n};\n", "memory": "203737" }
2,696
<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p> <p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p> <p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p> <p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,6], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6]. It can be proved that there are only 4 beautiful subsets in the array [2,4,6]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1], k = 1 <strong>Output:</strong> 1 <strong>Explanation:</strong> The beautiful subset of the array nums is [1]. It can be proved that there is only 1 beautiful subset in the array [1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 20</code></li> <li><code>1 &lt;= nums[i], k &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int beautifulSubsets(vector<int>& nums, int k) {\n int n = nums.size();\n vector<vector<int>> dp(n + 1, vector<int>(1 << n, -1)); // dp table to store results\n sort(nums.begin(), nums.end()); // Sort nums to ensure we handle subsets properly\n\n return countBeautifulSubsets(nums, k, 0, 0, dp) - 1; // Subtract 1 to exclude the empty subset\n }\n\nprivate:\n int countBeautifulSubsets(vector<int>& nums, int k, int index, int mask, vector<vector<int>>& dp) {\n if (index == nums.size()) {\n return 1; // Return 1 for each valid subset (including empty subset initially)\n }\n\n if (dp[index][mask] != -1) {\n return dp[index][mask];\n }\n\n // Exclude current element\n int result = countBeautifulSubsets(nums, k, index + 1, mask, dp);\n\n // Include current element if it does not violate the beautiful subset condition\n bool canInclude = true;\n for (int i = 0; i < nums.size(); ++i) {\n if (mask & (1 << i)) { // Check if element i is included in the current subset\n if (abs(nums[index] - nums[i]) == k) {\n canInclude = false;\n break;\n }\n }\n }\n\n if (canInclude) {\n result += countBeautifulSubsets(nums, k, index + 1, mask | (1 << index), dp);\n }\n\n return dp[index][mask] = result;\n }\n};\n", "memory": "209822" }
2,696
<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p> <p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p> <p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p> <p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,6], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6]. It can be proved that there are only 4 beautiful subsets in the array [2,4,6]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1], k = 1 <strong>Output:</strong> 1 <strong>Explanation:</strong> The beautiful subset of the array nums is [1]. It can be proved that there is only 1 beautiful subset in the array [1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 20</code></li> <li><code>1 &lt;= nums[i], k &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\nprivate:\n\n void subsets(vector<vector<int> >& res, vector<int>& curr, vector<int>& nums, int k, int idx){\n\n\n for(int i=idx ;i<nums.size() ;i++){\n\n bool isvalid = true;\n\n for(int num : curr){\n if(abs(num - nums[i]) == k){\n isvalid = false;\n break;\n\n }\n }\n\n if(isvalid){\n curr.push_back(nums[i]);\n res.push_back(curr);\n subsets(res, curr, nums,k , i+1);\n curr.pop_back();\n\n }\n }\n }\n\npublic:\n int beautifulSubsets(vector<int>& nums, int k) {\n \n vector<vector<int> > res;\n vector<int> curr;\n\n subsets(res, curr, nums, k , 0);\n\n return res.size();\n }\n};", "memory": "215907" }
2,696
<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p> <p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p> <p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p> <p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,6], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6]. It can be proved that there are only 4 beautiful subsets in the array [2,4,6]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1], k = 1 <strong>Output:</strong> 1 <strong>Explanation:</strong> The beautiful subset of the array nums is [1]. It can be proved that there is only 1 beautiful subset in the array [1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 20</code></li> <li><code>1 &lt;= nums[i], k &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool valid(vector<int> &arr, int k)\n {\n for (int i = 0; i < arr.size(); i++)\n {\n for (int j = i + 1; j < arr.size(); j++)\n {\n if (abs(arr[i] - arr[j]) == k) return false;\n }\n }\n return true;\n }\n void backtrack(vector<int> &nums, vector<vector<int>> &sum, vector<int> &arr, int k, vector<int> &visited, int t)\n {\n if (arr.size() == nums.size()) return;\n\n for (int i = t; i < nums.size(); i++)\n {\n if (visited[i]) continue;\n visited[i] = 1;\n arr.push_back(nums[i]);\n if (valid(arr, k))\n {\n sum.push_back(arr);\n backtrack(nums, sum, arr, k, visited, i);\n }\n arr.pop_back();\n visited[i] = 0;\n }\n }\n int beautifulSubsets(vector<int>& nums, int k) {\n vector<vector<int>> sum;\n vector<int> arr;\n vector<int> visited(nums.size(), 0);\n backtrack(nums, sum, arr, k, visited, 0);\n // for (vector<int> &a : sum)\n // {\n // for (int i : a) cout << i << \" \";\n // cout << endl;\n // }\n return sum.size();\n }\n};", "memory": "221992" }
2,696
<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p> <p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p> <p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p> <p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,6], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6]. It can be proved that there are only 4 beautiful subsets in the array [2,4,6]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1], k = 1 <strong>Output:</strong> 1 <strong>Explanation:</strong> The beautiful subset of the array nums is [1]. It can be proved that there is only 1 beautiful subset in the array [1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 20</code></li> <li><code>1 &lt;= nums[i], k &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int beautifulSubsets(vector<int>& nums, int k) {\n vector<vector<int>> res;\n vector<int> arr;\n dfs(nums, k, 0, res, arr);\n return res.size();\n }\n void dfs(vector<int>& nums, int k, int i, vector<vector<int>>& res, vector<int>& arr) {\n if (arr.size() > 0) {\n for (int a = 0; a < arr.size(); a++) {\n for (int b = a + 1; b < arr.size(); b++) {\n if (abs(arr[a] - arr[b]) == k) {\n return;\n }\n }\n }\n res.push_back(arr);\n }\n for (int j = i; j < nums.size(); j++) {\n arr.push_back(nums[j]);\n dfs(nums, k, j + 1, res, arr);\n arr.pop_back();\n }\n }\n};", "memory": "228077" }
2,696
<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p> <p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p> <p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p> <p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,6], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6]. It can be proved that there are only 4 beautiful subsets in the array [2,4,6]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1], k = 1 <strong>Output:</strong> 1 <strong>Explanation:</strong> The beautiful subset of the array nums is [1]. It can be proved that there is only 1 beautiful subset in the array [1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 20</code></li> <li><code>1 &lt;= nums[i], k &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> result;\n void backtrack(vector<int>& nums, int index, int k, unordered_map<int, int>& count, vector<int>& subsets) {\n for(int i = index; i < nums.size(); i ++) {\n if(count[nums[i] - k] || count[nums[i]+k]) continue;\n subsets.push_back(nums[i]);\n result.push_back(subsets);\n count[nums[i]]++;\n backtrack(nums, i+1, k, count, subsets);\n subsets.pop_back();\n count[nums[i]]--;\n }\n }\n \n void print() {\n for(auto& ele: result) {\n cout<<\"[\";\n for(auto& num: ele) {\n cout<<num<<\", \";\n }\n cout<<\"]\";\n }\n }\n int beautifulSubsets(vector<int>& nums, int k) {\n result.clear();\n unordered_map<int, int> count;\n vector<int> subsets;\n // int total_result = 0;\n for(int i = 0; i < nums.size(); i ++) {\n count[nums[i]] = 1;\n subsets.push_back(nums[i]);\n result.push_back(subsets);\n backtrack(nums, i+1, k, count, subsets);\n subsets.pop_back();\n count.clear();\n }\n\n // print();\n return result.size();\n }\n};", "memory": "234162" }
2,696
<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p> <p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p> <p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p> <p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,6], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6]. It can be proved that there are only 4 beautiful subsets in the array [2,4,6]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1], k = 1 <strong>Output:</strong> 1 <strong>Explanation:</strong> The beautiful subset of the array nums is [1]. It can be proved that there is only 1 beautiful subset in the array [1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 20</code></li> <li><code>1 &lt;= nums[i], k &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int beautifulSubsets(vector<int>& nums, int k) {\n int cnt = 0;\n\n for (int bitmask = 1; bitmask < (1 << nums.size()); ++bitmask) {\n bool valid = true;\n vector<bool> is_here(1000, false);\n\n for (int i = 0; i < nums.size(); ++i) {\n if (bitmask & (1 << i)) {\n int n = nums[nums.size() - 1 - i];\n\n if (n + k < 1000 && is_here[n + k] || n - k >= 0 && is_here[n - k]) {\n valid = false;\n break;\n }\n\n is_here[n] = true;\n }\n }\n\n if (valid)\n ++cnt;\n }\n\n return cnt;\n }\n};\n", "memory": "240247" }