id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\nset<vector<int>>res;\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n int n=nums.size(),idx=0;\n vector<int>path;\n backtrack(path,nums,idx,n);\n vector<vector<int>>ans(begin(res),end(res));\n return ans;\n }\n void backtrack(vector<int>path, vector<int>& nums,int idx,int n){\n if(idx==n){\n if(path.size()>1)res.insert(path);\n return;\n }\n backtrack(path,nums,idx+1,n);\n if(path.empty()||path.back()<=nums[idx]){\n path.emplace_back(nums[idx]);\n backtrack(path,nums,idx+1,n);\n path.pop_back();\n }\n }\n};",
"memory": "91310"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n set<vector<int>> ans;\n int n;\n\n void rec(int id, vector<int> curr, vector<int>& nums){\n if(id>=n){\n return;\n }\n int l = curr.size();\n rec(id+1, curr, nums);\n\n if(l==0 || curr[l-1]<=nums[id]){\n curr.push_back(nums[id]);\n if(l>0) ans.insert(curr);\n rec(id+1, curr, nums);\n }\n }\n\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n ans.clear();\n n = nums.size();\n vector<int> curr;\n rec(0, curr, nums);\n vector<vector<int>> a;\n for(auto el: ans) a.push_back(el);\n return a;\n }\n};",
"memory": "92451"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n set<vector<int>> ans;\n\n void helper(vector<int> temp_ans,int i,vector<int>& nums)\n {\n \n if(temp_ans.size() >= 2)\n {\n ans.insert(temp_ans);\n\n }\n if(i == nums.size())\n return;\n\n helper(temp_ans,i+1,nums);\n \n if(temp_ans.size()==0 || temp_ans[temp_ans.size() - 1] <= nums[i])\n {\n temp_ans.push_back(nums[i]);\n helper(temp_ans,i+1,nums);\n\n }\n \n \n \n\n\n return;\n }\n\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n\n vector<int> temp_ans;\n helper(temp_ans,0,nums);\n vector<vector<int>> orig_ans;\n\n for(auto s : ans)\n {\n orig_ans.push_back(s);\n }\n\n return orig_ans;\n \n }\n};",
"memory": "93593"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n \n void solve(vector<int> &nums, set<vector<int>> &s, int idx, vector<int> temp){\n if(temp.size()>=2){\n s.insert(temp);\n }\n \n if(idx>= nums.size()){\n return;\n }\n\n if(temp.empty() || nums[idx]>= temp.back()){\n temp.push_back(nums[idx]);\n solve(nums, s, idx+1, temp); // take the element\n temp.pop_back();\n }\n solve(nums, s, idx+ 1, temp); //do not take the element\n }\n\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n set<vector<int>> s;\n vector<int> temp; \n solve(nums, s, 0, temp);\n vector<vector<int>> ans;\n\n for(auto str: s){\n ans.push_back(str);\n }\n return ans;\n }\n};",
"memory": "93593"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n void solve(vector<int>temp,set<vector<int>>& st,vector<int>& nums,int i)\n {\n if(i == nums.size())\n {\n if(temp.size() >= 2) st.insert(temp);\n return;\n }\n solve(temp,st,nums,i+1);\n if(temp.size() == 0 || nums[i] >= temp.back()) \n {\n temp.push_back(nums[i]);\n solve(temp,st,nums,i+1);\n }\n }\n\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<vector<int>>ans;\n vector<int>temp;\n set<vector<int>>st;\n solve(temp,st,nums,0);\n for(auto it : st)\n {\n ans.push_back(it);\n }\n return ans;\n }\n};",
"memory": "94734"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nint n;\nvoid solve(vector<int>& nums,set<vector<int>>&ans,vector<int>output,int i,int prev){\nif(i==n){\n if(output.size()>1)ans.insert(output);\n return;\n}\nsolve(nums,ans,output,i+1,prev);\nif(prev==-1 || (output.size()>0 and nums[i]>=output.back())){\n output.push_back(nums[i]);\n solve(nums,ans,output,i+1,0);\n output.pop_back();\n}\n\n}\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n n=nums.size();\n set<vector<int>>ans;\nvector<int>output;\n solve(nums,ans,output,0,-1);\n vector<vector<int>>res;\n for(auto it:ans){\n res.push_back(it);\n }\nreturn res;\n }\n};",
"memory": "94734"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nvector<vector<int>> ans;\n void solve(vector<int>& nums, vector<int>& op, int idx, int n, set<vector<int>>& st)\n {\n if(idx == n)\n {\n if(op.size() > 1)\n {\n st.insert(op);\n }\n return;\n }\n\n vector<int> op1 = op;\n vector<int> op2 = op;\n\n if(op1.size() == 0 || op1.back() <= nums[idx])\n {\n op1.push_back(nums[idx]);\n solve(nums, op1, idx+1, n, st);\n op1.pop_back();\n }\n\n solve(nums,op2,idx+1,n,st);\n }\n\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n int n = nums.size();\n set<vector<int>> st;\n vector<int> op;\n\n solve(nums, op, 0, n, st);\n\n ans.assign(st.begin(), st.end());\n\n return ans;\n }\n};",
"memory": "95875"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nvector<vector<int>> ans;\n void solve(vector<int>& nums, vector<int>& op, int idx, int n, set<vector<int>>& st)\n {\n if(idx == n)\n {\n if(op.size() > 1)\n {\n st.insert(op);\n }\n return;\n }\n\n vector<int> op1 = op;\n vector<int> op2 = op;\n\n if(op1.size() == 0 || op1.back() <= nums[idx])\n {\n op1.push_back(nums[idx]);\n solve(nums, op1, idx+1, n, st);\n op1.pop_back();\n }\n\n solve(nums,op2,idx+1,n,st);\n }\n\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n int n = nums.size();\n set<vector<int>> st;\n vector<int> op;\n\n solve(nums, op, 0, n, st);\n\n ans.assign(st.begin(), st.end());\n\n return ans;\n }\n};",
"memory": "95875"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n int n = nums.size();\n vector<vector<int>> res;\n for(int i = 0; i < (1 << n); i++) {\n vector<int> subs;\n for(int j = 0; j < n; j++) {\n if(i & (1 << j)) {\n if (!subs.empty() && subs.back() > nums[j]) {\n continue;\n }\n subs.push_back(nums[j]);\n }\n }\n if(subs.size() > 1) {\n res.push_back(subs);\n }\n }\n sort(res.begin(), res.end());\n res.erase(unique(res.begin(), res.end()), res.end());\n\n\n return res; \n }\n};",
"memory": "97016"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\n void computeRes(int idx, vector<int> &nums, vector<int> cur, \n vector<vector<int>> &result, set<vector<int>> &st) {\n if(idx == nums.size()) {\n if(cur.size() > 1 && st.count(cur) == 0) {\n result.push_back(cur);\n st.insert(cur);\n }\n return;\n }\n\n // notTake\n computeRes(idx + 1, nums, cur, result, st);\n\n // take\n if(cur.empty() || cur.back() <= nums[idx]){\n cur.push_back(nums[idx]);\n computeRes(idx+1, nums, cur, result, st);\n }\n }\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<vector<int>> result;\n vector<int> cur;\n set<vector<int>> st;\n computeRes(0, nums, cur, result, st);\n\n return result;\n }\n};",
"memory": "97016"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void rec(vector<int>& nums, int i, vector<vector<int>>& ans, vector<int> curr, set<vector<int>>& mp) {\n if (i == nums.size()) {\n if (curr.size() > 1 && mp.find(curr) == mp.end()) {\n ans.push_back(curr);\n mp.insert(curr); // Store unique subsequences\n }\n return;\n }\n\n // Case when we don't pick the current number\n rec(nums, i + 1, ans, curr, mp);\n\n // Case when we pick the current number if it maintains the non-decreasing property\n if (curr.size() == 0 || nums[i] >= curr.back()) {\n curr.push_back(nums[i]);\n rec(nums, i + 1, ans, curr, mp);\n }\n }\n\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<vector<int>> ans;\n vector<int> curr;\n set<vector<int>> mp; // Set to ensure unique subsequences\n rec(nums, 0, ans, curr, mp);\n return ans;\n }\n};\n",
"memory": "98158"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void rec(vector<int>& nums, int i, vector<vector<int>>& ans, vector<int> curr, set<vector<int>>& mp) {\n if (i == nums.size()) {\n if (curr.size() > 1 && mp.find(curr) == mp.end()) {\n ans.push_back(curr);\n mp.insert(curr); // Store unique subsequences\n }\n return;\n }\n\n // Case when we don't pick the current number\n rec(nums, i + 1, ans, curr, mp);\n\n // Case when we pick the current number if it maintains the non-decreasing property\n if (curr.size() == 0 || nums[i] >= curr.back()) {\n curr.push_back(nums[i]);\n rec(nums, i + 1, ans, curr, mp);\n }\n }\n\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<vector<int>> ans;\n vector<int> curr;\n set<vector<int>> mp; // Set to ensure unique subsequences\n rec(nums, 0, ans, curr, mp);\n return ans;\n }\n};\n",
"memory": "98158"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>>ans;\n set<vector<int>>st;\n void f(int i,int n,vector<int>&arr,int c,vector<int>v)\n {\n if(i >= n)\n {\n if(c >= 2)\n st.insert(v);\n return;\n }\n if(c >= 2)\n {\n st.insert(v);\n }\n f(i+1,n,arr,c,v);\n if(v.empty() || v.back() <= arr[i])\n {\n v.push_back(arr[i]);\n f(i+1,n,arr,c+1,v);\n v.pop_back();\n }\n }\n vector<vector<int>> findSubsequences(vector<int>& arr) {\n int n = arr.size();\n f(0,n,arr,0,{});\n for(auto i : st) ans.push_back(i);\n return ans;\n }\n};\n#pragma GCC optimize(\"Ofast\")\nstatic auto _ = [](){\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return nullptr;\n}();",
"memory": "99299"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>>ans;\n set<vector<int>>st;\n void f(int i,int n,vector<int>&arr,int c,vector<int>v)\n {\n if(i >= n)\n {\n if(c >= 2)\n st.insert(v);\n return;\n }\n if(c >= 2)\n {\n st.insert(v);\n }\n f(i+1,n,arr,c,v);\n if(v.empty() || v.back() <= arr[i])\n {\n v.push_back(arr[i]);\n f(i+1,n,arr,c+1,v);\n v.pop_back();\n }\n }\n vector<vector<int>> findSubsequences(vector<int>& arr) {\n int n = arr.size();\n f(0,n,arr,0,{});\n for(auto i : st) ans.push_back(i);\n return ans;\n }\n};\n#pragma GCC optimize(\"Ofast\")\nstatic auto _ = [](){\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return nullptr;\n}();",
"memory": "99299"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n map<vector<int>,int> m;\n void solve(vector<vector<int>> &ans,vector<int> v,vector<int>& nums,int i,int prev){\n if(v.size() >= 2){\n if(m.find(v)==m.end()){\n m[v]++;\n ans.push_back(v);\n }\n }\n if(i>=nums.size()){\n return;\n }\n //inc\n if(prev == -1 or nums[i] >= nums[prev]){\n v.push_back(nums[i]);\n solve(ans,v,nums,i+1,i);\n v.pop_back();\n }\n //exc\n solve(ans,v,nums,i+1,prev);\n }\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<vector<int>> ans;\n vector<int> v;\n\n solve(ans,v,nums,0,-1);\n return ans;\n }\n};",
"memory": "100440"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n set<vector<int>> ans;\n vector<vector<int>> a;\n int n;\n\n void rec(int id, vector<int> curr, vector<int>& nums){\n if(id>=n){\n return;\n }\n int l = curr.size();\n rec(id+1, curr, nums);\n\n if(l==0 || curr[l-1]<=nums[id]){\n curr.push_back(nums[id]);\n if(l>0 && ans.find(curr)==ans.end()) {a.push_back(curr); ans.insert(curr);}\n rec(id+1, curr, nums);\n }\n }\n\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n ans.clear();\n n = nums.size();\n vector<int> curr;\n rec(0, curr, nums);\n return a;\n }\n};",
"memory": "100440"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>>ans;\n vector<vector<int>>dp;\n void f(int i,int prev,vector<int>v,vector<int>& nums,int n){\n if(i>=n){\n if(v.size()>=2){\n ans.push_back(v);\n }\n return;\n }\n if(prev==-1 || nums[i]>=nums[prev]){\n v.push_back(nums[i]);\n f(i+1,i,v,nums,n);\n v.pop_back();\n }\n f(i+1,prev,v,nums,n);\n }\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n int n=nums.size();vector<int>l;\n f(0,-1,l,nums,n);\n set<vector<int>>st;\n for(auto c:ans)st.insert(c);\n vector<vector<int>> g;\n for(auto x:st)g.push_back(x);\n return g;\n }\n};",
"memory": "101581"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n vector<vector<int>> ans;\n set<vector<int>> st;\n void solve(vector<int>& nums, int i, vector<int> temp){\n if(temp.size()>=2 && st.find(temp)==st.end()){\n ans.push_back(temp);\n st.insert(temp);\n }\n if(i==nums.size())return;\n\n solve(nums,i+1,temp);\n \n if(temp.size()==0 || temp.back()<=nums[i]){\n temp.push_back(nums[i]);\n solve(nums,i+1,temp);\n temp.pop_back();\n }\n \n }\n\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n solve(nums,0,{});\n return ans;\n }\n};",
"memory": "102723"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> ans;\n set<vector<int>> s;\n void find(vector<int> &nums,vector<int> arr,int n,int ind)\n {\n if(ind == n)\n {\n if(arr.size()>=2 && s.find(arr) == s.end())\n {\n ans.push_back(arr);\n s.insert(arr);\n }\n return;\n }\n\n if(arr.size() == 0)\n {\n find(nums,arr,n,ind+1);\n arr.push_back(nums[ind]);\n find(nums,arr,n,ind+1);\n arr.pop_back();\n }\n else{\n find(nums,arr,n,ind+1);\n if(arr[arr.size()-1]<=nums[ind])\n {\n arr.push_back(nums[ind]);\n find(nums,arr,n,ind+1);\n arr.pop_back();\n }\n\n }\n\n return;\n }\n\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<int> arr;\n int n = nums.size();\n find(nums,arr,n,0);\n return ans; \n }\n};",
"memory": "102723"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>>result;\n set<vector<int>>st;\n int n;\n void solve(vector<int>&nums,int i,vector<int>curr){\n if(i==n){\n if(curr.size()>=2 && st.count(curr)==0){\n result.push_back(curr);\n st.insert(curr);\n }\n return;\n }\n if(curr.empty()||curr.back()<=nums[i]){\n curr.push_back(nums[i]);\n solve(nums,i+1,curr);\n curr.pop_back();\n }\n solve(nums,i+1,curr);\n }\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n n=nums.size();\n vector<int>curr;\n solve(nums,0,curr);\n return result;\n }\n};",
"memory": "103864"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nvector<vector<int>> ans; \nset<vector<int>> st; \n void solve( vector<int>store,int i,vector<int>& nums){\n if(store.size()>=2){st.insert(store);}\n if(i>=nums.size()){return;}\n if(store.size()==0 || nums[i]>=store.back()){\n store.push_back(nums[i]);\n solve(store,i+1,nums);\n store.pop_back();\n solve(store,i+1,nums);\n }\n else{ solve(store,i+1,nums);}\n }\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<int>store; \n solve(store,0,nums);\n for(auto v:st){\n ans.push_back(v);\n }\n return ans;\n }\n};",
"memory": "103864"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void helper(vector<int>& nums,int n,vector<int>sol,vector<vector<int>>&ans,int i)\n {\n if(i==n)\n {\n if(sol.size()>1&& std::find(ans.begin(),ans.end(),sol)==ans.end())ans.push_back(sol);\n \n return;\n }\n if(sol.size()==0||sol[sol.size()-1]<=nums[i])\n sol.push_back(nums[i]);\n helper(nums,n,sol,ans,i+1);\n sol.pop_back();\n helper(nums,n,sol,ans,i+1);\n\n\n }\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n int n=nums.size();\n if(n==0)return {};\n vector<vector<int>>ans;\n vector<int>sol;\n \n helper(nums,n,sol,ans,0);\n return ans;\n\n }\n};",
"memory": "105005"
} |
495 | <p>Our hero Teemo is attacking an enemy Ashe with poison attacks! When Teemo attacks Ashe, Ashe gets poisoned for a exactly <code>duration</code> seconds. More formally, an attack at second <code>t</code> will mean Ashe is poisoned during the <strong>inclusive</strong> time interval <code>[t, t + duration - 1]</code>. If Teemo attacks again <strong>before</strong> the poison effect ends, the timer for it is <strong>reset</strong>, and the poison effect will end <code>duration</code> seconds after the new attack.</p>
<p>You are given a <strong>non-decreasing</strong> integer array <code>timeSeries</code>, where <code>timeSeries[i]</code> denotes that Teemo attacks Ashe at second <code>timeSeries[i]</code>, and an integer <code>duration</code>.</p>
<p>Return <em>the <strong>total</strong> number of seconds that Ashe is poisoned</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> timeSeries = [1,4], duration = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> Teemo's attacks on Ashe go as follows:
- At second 1, Teemo attacks, and Ashe is poisoned for seconds 1 and 2.
- At second 4, Teemo attacks, and Ashe is poisoned for seconds 4 and 5.
Ashe is poisoned for seconds 1, 2, 4, and 5, which is 4 seconds in total.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> timeSeries = [1,2], duration = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Teemo's attacks on Ashe go as follows:
- At second 1, Teemo attacks, and Ashe is poisoned for seconds 1 and 2.
- At second 2 however, Teemo attacks again and resets the poison timer. Ashe is poisoned for seconds 2 and 3.
Ashe is poisoned for seconds 1, 2, and 3, which is 3 seconds in total.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= timeSeries.length <= 10<sup>4</sup></code></li>
<li><code>0 <= timeSeries[i], duration <= 10<sup>7</sup></code></li>
<li><code>timeSeries</code> is sorted in <strong>non-decreasing</strong> order.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\nint findPoisonedDuration(vector<int>& tS, int duration) {\n int out = 0;\n for(int i=0;i<tS.size()-1;i++) {\n if(tS[i] + duration > tS[i+1])\n out += tS[i+1] - tS[i];\n else\n out += duration;\n }\n return out + duration;\n}\n};",
"memory": "28100"
} |
495 | <p>Our hero Teemo is attacking an enemy Ashe with poison attacks! When Teemo attacks Ashe, Ashe gets poisoned for a exactly <code>duration</code> seconds. More formally, an attack at second <code>t</code> will mean Ashe is poisoned during the <strong>inclusive</strong> time interval <code>[t, t + duration - 1]</code>. If Teemo attacks again <strong>before</strong> the poison effect ends, the timer for it is <strong>reset</strong>, and the poison effect will end <code>duration</code> seconds after the new attack.</p>
<p>You are given a <strong>non-decreasing</strong> integer array <code>timeSeries</code>, where <code>timeSeries[i]</code> denotes that Teemo attacks Ashe at second <code>timeSeries[i]</code>, and an integer <code>duration</code>.</p>
<p>Return <em>the <strong>total</strong> number of seconds that Ashe is poisoned</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> timeSeries = [1,4], duration = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> Teemo's attacks on Ashe go as follows:
- At second 1, Teemo attacks, and Ashe is poisoned for seconds 1 and 2.
- At second 4, Teemo attacks, and Ashe is poisoned for seconds 4 and 5.
Ashe is poisoned for seconds 1, 2, 4, and 5, which is 4 seconds in total.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> timeSeries = [1,2], duration = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Teemo's attacks on Ashe go as follows:
- At second 1, Teemo attacks, and Ashe is poisoned for seconds 1 and 2.
- At second 2 however, Teemo attacks again and resets the poison timer. Ashe is poisoned for seconds 2 and 3.
Ashe is poisoned for seconds 1, 2, and 3, which is 3 seconds in total.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= timeSeries.length <= 10<sup>4</sup></code></li>
<li><code>0 <= timeSeries[i], duration <= 10<sup>7</sup></code></li>
<li><code>timeSeries</code> is sorted in <strong>non-decreasing</strong> order.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findPoisonedDuration(vector<int>& timeSeries, int duration) \n {\n int total = 0, poison_end = 0;\n\n if(timeSeries.size() == 1) return duration;\n\n for(int i = 0; i < timeSeries.size()-1; i++)\n {\n poison_end = timeSeries[i] + duration - 1;\n total += duration;\n if(poison_end >= timeSeries[i+1]) \n total -= poison_end - timeSeries[i+1] + 1; \n }\n return total + duration;\n }\n};",
"memory": "28200"
} |
495 | <p>Our hero Teemo is attacking an enemy Ashe with poison attacks! When Teemo attacks Ashe, Ashe gets poisoned for a exactly <code>duration</code> seconds. More formally, an attack at second <code>t</code> will mean Ashe is poisoned during the <strong>inclusive</strong> time interval <code>[t, t + duration - 1]</code>. If Teemo attacks again <strong>before</strong> the poison effect ends, the timer for it is <strong>reset</strong>, and the poison effect will end <code>duration</code> seconds after the new attack.</p>
<p>You are given a <strong>non-decreasing</strong> integer array <code>timeSeries</code>, where <code>timeSeries[i]</code> denotes that Teemo attacks Ashe at second <code>timeSeries[i]</code>, and an integer <code>duration</code>.</p>
<p>Return <em>the <strong>total</strong> number of seconds that Ashe is poisoned</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> timeSeries = [1,4], duration = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> Teemo's attacks on Ashe go as follows:
- At second 1, Teemo attacks, and Ashe is poisoned for seconds 1 and 2.
- At second 4, Teemo attacks, and Ashe is poisoned for seconds 4 and 5.
Ashe is poisoned for seconds 1, 2, 4, and 5, which is 4 seconds in total.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> timeSeries = [1,2], duration = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Teemo's attacks on Ashe go as follows:
- At second 1, Teemo attacks, and Ashe is poisoned for seconds 1 and 2.
- At second 2 however, Teemo attacks again and resets the poison timer. Ashe is poisoned for seconds 2 and 3.
Ashe is poisoned for seconds 1, 2, and 3, which is 3 seconds in total.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= timeSeries.length <= 10<sup>4</sup></code></li>
<li><code>0 <= timeSeries[i], duration <= 10<sup>7</sup></code></li>
<li><code>timeSeries</code> is sorted in <strong>non-decreasing</strong> order.</li>
</ul>
| 0 | {
"code": "class Solution {\n public:\n int findPoisonedDuration(vector<int>& timeSeries, int duration) {\n if (duration == 0)\n return 0;\n\n int ans = 0;\n\n for (int i = 0; i + 1 < timeSeries.size(); ++i)\n ans += min(timeSeries[i + 1] - timeSeries[i], duration);\n\n return ans + duration;\n }\n};",
"memory": "28300"
} |
495 | <p>Our hero Teemo is attacking an enemy Ashe with poison attacks! When Teemo attacks Ashe, Ashe gets poisoned for a exactly <code>duration</code> seconds. More formally, an attack at second <code>t</code> will mean Ashe is poisoned during the <strong>inclusive</strong> time interval <code>[t, t + duration - 1]</code>. If Teemo attacks again <strong>before</strong> the poison effect ends, the timer for it is <strong>reset</strong>, and the poison effect will end <code>duration</code> seconds after the new attack.</p>
<p>You are given a <strong>non-decreasing</strong> integer array <code>timeSeries</code>, where <code>timeSeries[i]</code> denotes that Teemo attacks Ashe at second <code>timeSeries[i]</code>, and an integer <code>duration</code>.</p>
<p>Return <em>the <strong>total</strong> number of seconds that Ashe is poisoned</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> timeSeries = [1,4], duration = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> Teemo's attacks on Ashe go as follows:
- At second 1, Teemo attacks, and Ashe is poisoned for seconds 1 and 2.
- At second 4, Teemo attacks, and Ashe is poisoned for seconds 4 and 5.
Ashe is poisoned for seconds 1, 2, 4, and 5, which is 4 seconds in total.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> timeSeries = [1,2], duration = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Teemo's attacks on Ashe go as follows:
- At second 1, Teemo attacks, and Ashe is poisoned for seconds 1 and 2.
- At second 2 however, Teemo attacks again and resets the poison timer. Ashe is poisoned for seconds 2 and 3.
Ashe is poisoned for seconds 1, 2, and 3, which is 3 seconds in total.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= timeSeries.length <= 10<sup>4</sup></code></li>
<li><code>0 <= timeSeries[i], duration <= 10<sup>7</sup></code></li>
<li><code>timeSeries</code> is sorted in <strong>non-decreasing</strong> order.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findPoisonedDuration(vector<int>& timeSeries, int duration) {\n int ans=0, n=timeSeries.size(), a=0, b=0;\n\n for(int i=0; i<n; i++){\n if(timeSeries[i] < b){\n ans -= (b - timeSeries[i]);\n }\n ans += duration; \n \n b = timeSeries[i] + duration;\n }\n\n\n\n return ans;\n }\n};",
"memory": "28300"
} |
495 | <p>Our hero Teemo is attacking an enemy Ashe with poison attacks! When Teemo attacks Ashe, Ashe gets poisoned for a exactly <code>duration</code> seconds. More formally, an attack at second <code>t</code> will mean Ashe is poisoned during the <strong>inclusive</strong> time interval <code>[t, t + duration - 1]</code>. If Teemo attacks again <strong>before</strong> the poison effect ends, the timer for it is <strong>reset</strong>, and the poison effect will end <code>duration</code> seconds after the new attack.</p>
<p>You are given a <strong>non-decreasing</strong> integer array <code>timeSeries</code>, where <code>timeSeries[i]</code> denotes that Teemo attacks Ashe at second <code>timeSeries[i]</code>, and an integer <code>duration</code>.</p>
<p>Return <em>the <strong>total</strong> number of seconds that Ashe is poisoned</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> timeSeries = [1,4], duration = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> Teemo's attacks on Ashe go as follows:
- At second 1, Teemo attacks, and Ashe is poisoned for seconds 1 and 2.
- At second 4, Teemo attacks, and Ashe is poisoned for seconds 4 and 5.
Ashe is poisoned for seconds 1, 2, 4, and 5, which is 4 seconds in total.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> timeSeries = [1,2], duration = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Teemo's attacks on Ashe go as follows:
- At second 1, Teemo attacks, and Ashe is poisoned for seconds 1 and 2.
- At second 2 however, Teemo attacks again and resets the poison timer. Ashe is poisoned for seconds 2 and 3.
Ashe is poisoned for seconds 1, 2, and 3, which is 3 seconds in total.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= timeSeries.length <= 10<sup>4</sup></code></li>
<li><code>0 <= timeSeries[i], duration <= 10<sup>7</sup></code></li>
<li><code>timeSeries</code> is sorted in <strong>non-decreasing</strong> order.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findPoisonedDuration(vector<int>& timeSeries, int duration) {\n int length = timeSeries.size();\n int poison_counter = 0;\n int current_duration = -1;\n\n for(int i = 0; i < length; i++) {\n if(timeSeries[i] <= current_duration) {\n poison_counter -= current_duration - timeSeries[i] + 1;\n }\n current_duration = timeSeries[i] + duration - 1;\n poison_counter += current_duration - timeSeries[i] + 1;\n }\n\n return poison_counter;\n }\n};",
"memory": "28400"
} |
495 | <p>Our hero Teemo is attacking an enemy Ashe with poison attacks! When Teemo attacks Ashe, Ashe gets poisoned for a exactly <code>duration</code> seconds. More formally, an attack at second <code>t</code> will mean Ashe is poisoned during the <strong>inclusive</strong> time interval <code>[t, t + duration - 1]</code>. If Teemo attacks again <strong>before</strong> the poison effect ends, the timer for it is <strong>reset</strong>, and the poison effect will end <code>duration</code> seconds after the new attack.</p>
<p>You are given a <strong>non-decreasing</strong> integer array <code>timeSeries</code>, where <code>timeSeries[i]</code> denotes that Teemo attacks Ashe at second <code>timeSeries[i]</code>, and an integer <code>duration</code>.</p>
<p>Return <em>the <strong>total</strong> number of seconds that Ashe is poisoned</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> timeSeries = [1,4], duration = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> Teemo's attacks on Ashe go as follows:
- At second 1, Teemo attacks, and Ashe is poisoned for seconds 1 and 2.
- At second 4, Teemo attacks, and Ashe is poisoned for seconds 4 and 5.
Ashe is poisoned for seconds 1, 2, 4, and 5, which is 4 seconds in total.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> timeSeries = [1,2], duration = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Teemo's attacks on Ashe go as follows:
- At second 1, Teemo attacks, and Ashe is poisoned for seconds 1 and 2.
- At second 2 however, Teemo attacks again and resets the poison timer. Ashe is poisoned for seconds 2 and 3.
Ashe is poisoned for seconds 1, 2, and 3, which is 3 seconds in total.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= timeSeries.length <= 10<sup>4</sup></code></li>
<li><code>0 <= timeSeries[i], duration <= 10<sup>7</sup></code></li>
<li><code>timeSeries</code> is sorted in <strong>non-decreasing</strong> order.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findPoisonedDuration(vector<int>& timeSeries, int duration) {\n int total = 0;\n int poison_end = 0;\n\n int n = timeSeries.size();\n\n for(int i=0; i<n-1; i++){\n poison_end = timeSeries[i] + duration -1;\n total += duration;\n if(poison_end >= timeSeries[i+1])\n total -= poison_end - timeSeries[i+1] + 1;\n }\n\n return total + duration;\n }\n};",
"memory": "28400"
} |
495 | <p>Our hero Teemo is attacking an enemy Ashe with poison attacks! When Teemo attacks Ashe, Ashe gets poisoned for a exactly <code>duration</code> seconds. More formally, an attack at second <code>t</code> will mean Ashe is poisoned during the <strong>inclusive</strong> time interval <code>[t, t + duration - 1]</code>. If Teemo attacks again <strong>before</strong> the poison effect ends, the timer for it is <strong>reset</strong>, and the poison effect will end <code>duration</code> seconds after the new attack.</p>
<p>You are given a <strong>non-decreasing</strong> integer array <code>timeSeries</code>, where <code>timeSeries[i]</code> denotes that Teemo attacks Ashe at second <code>timeSeries[i]</code>, and an integer <code>duration</code>.</p>
<p>Return <em>the <strong>total</strong> number of seconds that Ashe is poisoned</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> timeSeries = [1,4], duration = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> Teemo's attacks on Ashe go as follows:
- At second 1, Teemo attacks, and Ashe is poisoned for seconds 1 and 2.
- At second 4, Teemo attacks, and Ashe is poisoned for seconds 4 and 5.
Ashe is poisoned for seconds 1, 2, 4, and 5, which is 4 seconds in total.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> timeSeries = [1,2], duration = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Teemo's attacks on Ashe go as follows:
- At second 1, Teemo attacks, and Ashe is poisoned for seconds 1 and 2.
- At second 2 however, Teemo attacks again and resets the poison timer. Ashe is poisoned for seconds 2 and 3.
Ashe is poisoned for seconds 1, 2, and 3, which is 3 seconds in total.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= timeSeries.length <= 10<sup>4</sup></code></li>
<li><code>0 <= timeSeries[i], duration <= 10<sup>7</sup></code></li>
<li><code>timeSeries</code> is sorted in <strong>non-decreasing</strong> order.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int findPoisonedDuration(vector<int>& timeSeries, int duration) {\n if (timeSeries.empty()) return 0; \n int poisioned = 0;\n for(int i=0;i<timeSeries.size()-1;i++){\n int timeBetween = timeSeries[i+1] - timeSeries[i];\n poisioned += min(timeBetween,duration);\n }\n poisioned += duration;\n return poisioned;\n }\n};",
"memory": "28500"
} |
495 | <p>Our hero Teemo is attacking an enemy Ashe with poison attacks! When Teemo attacks Ashe, Ashe gets poisoned for a exactly <code>duration</code> seconds. More formally, an attack at second <code>t</code> will mean Ashe is poisoned during the <strong>inclusive</strong> time interval <code>[t, t + duration - 1]</code>. If Teemo attacks again <strong>before</strong> the poison effect ends, the timer for it is <strong>reset</strong>, and the poison effect will end <code>duration</code> seconds after the new attack.</p>
<p>You are given a <strong>non-decreasing</strong> integer array <code>timeSeries</code>, where <code>timeSeries[i]</code> denotes that Teemo attacks Ashe at second <code>timeSeries[i]</code>, and an integer <code>duration</code>.</p>
<p>Return <em>the <strong>total</strong> number of seconds that Ashe is poisoned</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> timeSeries = [1,4], duration = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> Teemo's attacks on Ashe go as follows:
- At second 1, Teemo attacks, and Ashe is poisoned for seconds 1 and 2.
- At second 4, Teemo attacks, and Ashe is poisoned for seconds 4 and 5.
Ashe is poisoned for seconds 1, 2, 4, and 5, which is 4 seconds in total.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> timeSeries = [1,2], duration = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Teemo's attacks on Ashe go as follows:
- At second 1, Teemo attacks, and Ashe is poisoned for seconds 1 and 2.
- At second 2 however, Teemo attacks again and resets the poison timer. Ashe is poisoned for seconds 2 and 3.
Ashe is poisoned for seconds 1, 2, and 3, which is 3 seconds in total.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= timeSeries.length <= 10<sup>4</sup></code></li>
<li><code>0 <= timeSeries[i], duration <= 10<sup>7</sup></code></li>
<li><code>timeSeries</code> is sorted in <strong>non-decreasing</strong> order.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int findPoisonedDuration(vector<int>& timeSeries, int duration) {\n int n = timeSeries.size();\n int sum=0;\n for(int i=1;i<n;i++)\n {\n if(timeSeries[i]-timeSeries[i-1] < duration)\n {\n sum += timeSeries[i]-timeSeries[i-1];\n }\n else\n {\n sum +=duration;\n }\n }\n \n return sum+duration;\n }\n};",
"memory": "28500"
} |
496 | <p>The <strong>next greater element</strong> of some element <code>x</code> in an array is the <strong>first greater</strong> element that is <strong>to the right</strong> of <code>x</code> in the same array.</p>
<p>You are given two <strong>distinct 0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, where <code>nums1</code> is a subset of <code>nums2</code>.</p>
<p>For each <code>0 <= i < nums1.length</code>, find the index <code>j</code> such that <code>nums1[i] == nums2[j]</code> and determine the <strong>next greater element</strong> of <code>nums2[j]</code> in <code>nums2</code>. If there is no next greater element, then the answer for this query is <code>-1</code>.</p>
<p>Return <em>an array </em><code>ans</code><em> of length </em><code>nums1.length</code><em> such that </em><code>ans[i]</code><em> is the <strong>next greater element</strong> as described above.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [4,1,2], nums2 = [1,3,4,2]
<strong>Output:</strong> [-1,3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 4 is underlined in nums2 = [1,3,<u>4</u>,2]. There is no next greater element, so the answer is -1.
- 1 is underlined in nums2 = [<u>1</u>,3,4,2]. The next greater element is 3.
- 2 is underlined in nums2 = [1,3,4,<u>2</u>]. There is no next greater element, so the answer is -1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [2,4], nums2 = [1,2,3,4]
<strong>Output:</strong> [3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 2 is underlined in nums2 = [1,<u>2</u>,3,4]. The next greater element is 3.
- 4 is underlined in nums2 = [1,2,3,<u>4</u>]. There is no next greater element, so the answer is -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length <= nums2.length <= 1000</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
<li>All integers in <code>nums1</code> and <code>nums2</code> are <strong>unique</strong>.</li>
<li>All the integers of <code>nums1</code> also appear in <code>nums2</code>.</li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you find an <code>O(nums1.length + nums2.length)</code> solution? | 0 | {
"code": "class Solution {\npublic:\n vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {\nvector<int>ans(nums1.size() ,-1);\n\nfor(int i =0;i<nums1.size();i++){\nbool found = false;\nfor(int j =0;j<nums2.size() ;j++){\n if(nums1[i]==nums2[j]){\n found = true;\n }\n if( found &&nums1[i]<nums2[j]){\n ans[i] =nums2[j];\n break;\n }\n \n}\n\n\n\n\n\n}\n\nreturn ans;\n\n }\n};",
"memory": "10600"
} |
496 | <p>The <strong>next greater element</strong> of some element <code>x</code> in an array is the <strong>first greater</strong> element that is <strong>to the right</strong> of <code>x</code> in the same array.</p>
<p>You are given two <strong>distinct 0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, where <code>nums1</code> is a subset of <code>nums2</code>.</p>
<p>For each <code>0 <= i < nums1.length</code>, find the index <code>j</code> such that <code>nums1[i] == nums2[j]</code> and determine the <strong>next greater element</strong> of <code>nums2[j]</code> in <code>nums2</code>. If there is no next greater element, then the answer for this query is <code>-1</code>.</p>
<p>Return <em>an array </em><code>ans</code><em> of length </em><code>nums1.length</code><em> such that </em><code>ans[i]</code><em> is the <strong>next greater element</strong> as described above.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [4,1,2], nums2 = [1,3,4,2]
<strong>Output:</strong> [-1,3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 4 is underlined in nums2 = [1,3,<u>4</u>,2]. There is no next greater element, so the answer is -1.
- 1 is underlined in nums2 = [<u>1</u>,3,4,2]. The next greater element is 3.
- 2 is underlined in nums2 = [1,3,4,<u>2</u>]. There is no next greater element, so the answer is -1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [2,4], nums2 = [1,2,3,4]
<strong>Output:</strong> [3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 2 is underlined in nums2 = [1,<u>2</u>,3,4]. The next greater element is 3.
- 4 is underlined in nums2 = [1,2,3,<u>4</u>]. There is no next greater element, so the answer is -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length <= nums2.length <= 1000</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
<li>All integers in <code>nums1</code> and <code>nums2</code> are <strong>unique</strong>.</li>
<li>All the integers of <code>nums1</code> also appear in <code>nums2</code>.</li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you find an <code>O(nums1.length + nums2.length)</code> solution? | 0 | {
"code": "class Solution {\npublic:\n vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) { int n = nums1.size();\n vector<int> son(n); \n for (int i = 0; i < n; ++i) {\n auto it = find(nums2.begin(), nums2.end(), nums1[i]); \n bool found = false;\n for (auto j = it + 1; j != nums2.end(); ++j) {\n if (*j > nums1[i]) {\n son[i] = *j;\n found = true;break;}}\n if (!found) son[i] = -1;}return son;}};",
"memory": "10700"
} |
496 | <p>The <strong>next greater element</strong> of some element <code>x</code> in an array is the <strong>first greater</strong> element that is <strong>to the right</strong> of <code>x</code> in the same array.</p>
<p>You are given two <strong>distinct 0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, where <code>nums1</code> is a subset of <code>nums2</code>.</p>
<p>For each <code>0 <= i < nums1.length</code>, find the index <code>j</code> such that <code>nums1[i] == nums2[j]</code> and determine the <strong>next greater element</strong> of <code>nums2[j]</code> in <code>nums2</code>. If there is no next greater element, then the answer for this query is <code>-1</code>.</p>
<p>Return <em>an array </em><code>ans</code><em> of length </em><code>nums1.length</code><em> such that </em><code>ans[i]</code><em> is the <strong>next greater element</strong> as described above.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [4,1,2], nums2 = [1,3,4,2]
<strong>Output:</strong> [-1,3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 4 is underlined in nums2 = [1,3,<u>4</u>,2]. There is no next greater element, so the answer is -1.
- 1 is underlined in nums2 = [<u>1</u>,3,4,2]. The next greater element is 3.
- 2 is underlined in nums2 = [1,3,4,<u>2</u>]. There is no next greater element, so the answer is -1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [2,4], nums2 = [1,2,3,4]
<strong>Output:</strong> [3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 2 is underlined in nums2 = [1,<u>2</u>,3,4]. The next greater element is 3.
- 4 is underlined in nums2 = [1,2,3,<u>4</u>]. There is no next greater element, so the answer is -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length <= nums2.length <= 1000</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
<li>All integers in <code>nums1</code> and <code>nums2</code> are <strong>unique</strong>.</li>
<li>All the integers of <code>nums1</code> also appear in <code>nums2</code>.</li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you find an <code>O(nums1.length + nums2.length)</code> solution? | 0 | {
"code": "class Solution {\npublic:\n vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {\n int n=nums1.size();\n int m=nums2.size();\n vector<int> res(n,-1);\n for(int i=0; i<n; i++)\n {\n for(int j=0; j<m; j++)\n {\n if(nums1[i]==nums2[j])\n {\n for(int k=j+1; k<m; k++)\n {\n if(nums2[k]>nums2[j])\n {\n res[i]=nums2[k];\n break;\n }\n }\n }\n }\n }\n return res;\n }\n};",
"memory": "10800"
} |
496 | <p>The <strong>next greater element</strong> of some element <code>x</code> in an array is the <strong>first greater</strong> element that is <strong>to the right</strong> of <code>x</code> in the same array.</p>
<p>You are given two <strong>distinct 0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, where <code>nums1</code> is a subset of <code>nums2</code>.</p>
<p>For each <code>0 <= i < nums1.length</code>, find the index <code>j</code> such that <code>nums1[i] == nums2[j]</code> and determine the <strong>next greater element</strong> of <code>nums2[j]</code> in <code>nums2</code>. If there is no next greater element, then the answer for this query is <code>-1</code>.</p>
<p>Return <em>an array </em><code>ans</code><em> of length </em><code>nums1.length</code><em> such that </em><code>ans[i]</code><em> is the <strong>next greater element</strong> as described above.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [4,1,2], nums2 = [1,3,4,2]
<strong>Output:</strong> [-1,3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 4 is underlined in nums2 = [1,3,<u>4</u>,2]. There is no next greater element, so the answer is -1.
- 1 is underlined in nums2 = [<u>1</u>,3,4,2]. The next greater element is 3.
- 2 is underlined in nums2 = [1,3,4,<u>2</u>]. There is no next greater element, so the answer is -1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [2,4], nums2 = [1,2,3,4]
<strong>Output:</strong> [3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 2 is underlined in nums2 = [1,<u>2</u>,3,4]. The next greater element is 3.
- 4 is underlined in nums2 = [1,2,3,<u>4</u>]. There is no next greater element, so the answer is -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length <= nums2.length <= 1000</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
<li>All integers in <code>nums1</code> and <code>nums2</code> are <strong>unique</strong>.</li>
<li>All the integers of <code>nums1</code> also appear in <code>nums2</code>.</li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you find an <code>O(nums1.length + nums2.length)</code> solution? | 0 | {
"code": "class Solution {\npublic:\n vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {\n vector<int>v(nums1.size(),-1);\n int k=0;\n for(int i=0;i<nums1.size();i++){\n \n auto it=find(nums2.begin(),nums2.end(),nums1[i]);\n for(auto ia=it+1;ia!=nums2.end();ia++){\n if(*ia>*it){\n v[k]=*ia;\n \n break;\n }\n \n \n }\n k++;\n }\n return v;\n } \n };",
"memory": "10800"
} |
496 | <p>The <strong>next greater element</strong> of some element <code>x</code> in an array is the <strong>first greater</strong> element that is <strong>to the right</strong> of <code>x</code> in the same array.</p>
<p>You are given two <strong>distinct 0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, where <code>nums1</code> is a subset of <code>nums2</code>.</p>
<p>For each <code>0 <= i < nums1.length</code>, find the index <code>j</code> such that <code>nums1[i] == nums2[j]</code> and determine the <strong>next greater element</strong> of <code>nums2[j]</code> in <code>nums2</code>. If there is no next greater element, then the answer for this query is <code>-1</code>.</p>
<p>Return <em>an array </em><code>ans</code><em> of length </em><code>nums1.length</code><em> such that </em><code>ans[i]</code><em> is the <strong>next greater element</strong> as described above.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [4,1,2], nums2 = [1,3,4,2]
<strong>Output:</strong> [-1,3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 4 is underlined in nums2 = [1,3,<u>4</u>,2]. There is no next greater element, so the answer is -1.
- 1 is underlined in nums2 = [<u>1</u>,3,4,2]. The next greater element is 3.
- 2 is underlined in nums2 = [1,3,4,<u>2</u>]. There is no next greater element, so the answer is -1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [2,4], nums2 = [1,2,3,4]
<strong>Output:</strong> [3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 2 is underlined in nums2 = [1,<u>2</u>,3,4]. The next greater element is 3.
- 4 is underlined in nums2 = [1,2,3,<u>4</u>]. There is no next greater element, so the answer is -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length <= nums2.length <= 1000</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
<li>All integers in <code>nums1</code> and <code>nums2</code> are <strong>unique</strong>.</li>
<li>All the integers of <code>nums1</code> also appear in <code>nums2</code>.</li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you find an <code>O(nums1.length + nums2.length)</code> solution? | 0 | {
"code": "\nclass Solution {\npublic:\n vector<int> nextGreaterElement(std::vector<int>& nums1, std::vector<int>& nums2) {\n int n = nums1.size();\n int m = nums2.size();\n vector<int> ans(n, -1);\n for (int i = 0; i < n; i++) \n {\n for (int j = 0; j < m; j++) \n {\n if (nums2[j] == nums1[i]) //find the current element of nums1 in nums2\n {\n for (int k = j + 1; k < m; k++) \n {\n if (nums2[k] > nums1[i]) \n {\n ans[i] = nums2[k];\n break;\n }\n }\n }\n }\n }\n \n return ans;\n }\n};\n",
"memory": "10900"
} |
496 | <p>The <strong>next greater element</strong> of some element <code>x</code> in an array is the <strong>first greater</strong> element that is <strong>to the right</strong> of <code>x</code> in the same array.</p>
<p>You are given two <strong>distinct 0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, where <code>nums1</code> is a subset of <code>nums2</code>.</p>
<p>For each <code>0 <= i < nums1.length</code>, find the index <code>j</code> such that <code>nums1[i] == nums2[j]</code> and determine the <strong>next greater element</strong> of <code>nums2[j]</code> in <code>nums2</code>. If there is no next greater element, then the answer for this query is <code>-1</code>.</p>
<p>Return <em>an array </em><code>ans</code><em> of length </em><code>nums1.length</code><em> such that </em><code>ans[i]</code><em> is the <strong>next greater element</strong> as described above.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [4,1,2], nums2 = [1,3,4,2]
<strong>Output:</strong> [-1,3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 4 is underlined in nums2 = [1,3,<u>4</u>,2]. There is no next greater element, so the answer is -1.
- 1 is underlined in nums2 = [<u>1</u>,3,4,2]. The next greater element is 3.
- 2 is underlined in nums2 = [1,3,4,<u>2</u>]. There is no next greater element, so the answer is -1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [2,4], nums2 = [1,2,3,4]
<strong>Output:</strong> [3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 2 is underlined in nums2 = [1,<u>2</u>,3,4]. The next greater element is 3.
- 4 is underlined in nums2 = [1,2,3,<u>4</u>]. There is no next greater element, so the answer is -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length <= nums2.length <= 1000</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
<li>All integers in <code>nums1</code> and <code>nums2</code> are <strong>unique</strong>.</li>
<li>All the integers of <code>nums1</code> also appear in <code>nums2</code>.</li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you find an <code>O(nums1.length + nums2.length)</code> solution? | 0 | {
"code": "class Solution {\npublic:\n vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {\n vector<int>ans(nums1.size(),-1);\n for(int i=0;i<nums1.size();i++){\n int k=0;\n while(k<nums2.size() && nums2[k]!=nums1[i]){\n k++;\n }\n for(int j=k+1;j<nums2.size();j++){\n if(nums1[i]<nums2[j]){\n ans[i]=nums2[j];\n break;\n }\n }\n \n }\n return ans;\n }\n};",
"memory": "10900"
} |
496 | <p>The <strong>next greater element</strong> of some element <code>x</code> in an array is the <strong>first greater</strong> element that is <strong>to the right</strong> of <code>x</code> in the same array.</p>
<p>You are given two <strong>distinct 0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, where <code>nums1</code> is a subset of <code>nums2</code>.</p>
<p>For each <code>0 <= i < nums1.length</code>, find the index <code>j</code> such that <code>nums1[i] == nums2[j]</code> and determine the <strong>next greater element</strong> of <code>nums2[j]</code> in <code>nums2</code>. If there is no next greater element, then the answer for this query is <code>-1</code>.</p>
<p>Return <em>an array </em><code>ans</code><em> of length </em><code>nums1.length</code><em> such that </em><code>ans[i]</code><em> is the <strong>next greater element</strong> as described above.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [4,1,2], nums2 = [1,3,4,2]
<strong>Output:</strong> [-1,3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 4 is underlined in nums2 = [1,3,<u>4</u>,2]. There is no next greater element, so the answer is -1.
- 1 is underlined in nums2 = [<u>1</u>,3,4,2]. The next greater element is 3.
- 2 is underlined in nums2 = [1,3,4,<u>2</u>]. There is no next greater element, so the answer is -1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [2,4], nums2 = [1,2,3,4]
<strong>Output:</strong> [3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 2 is underlined in nums2 = [1,<u>2</u>,3,4]. The next greater element is 3.
- 4 is underlined in nums2 = [1,2,3,<u>4</u>]. There is no next greater element, so the answer is -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length <= nums2.length <= 1000</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
<li>All integers in <code>nums1</code> and <code>nums2</code> are <strong>unique</strong>.</li>
<li>All the integers of <code>nums1</code> also appear in <code>nums2</code>.</li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you find an <code>O(nums1.length + nums2.length)</code> solution? | 0 | {
"code": "class Solution {\npublic:\n vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {\n \n vector<int>out;\n for(int i=0;i<nums1.size();i++)\n {\n for(int j=0;j<nums2.size();j++)\n {\n if(nums1[i]==nums2[j])\n {\n int max_val=-1;\n for(int k=j;k<nums2.size();k++)\n {\n if(nums2[k]>nums2[j])\n {\n max_val=nums2[k];\n break;\n } \n }\n out.push_back(max_val);\n }\n }\n }\n return out;\n \n }\n};",
"memory": "11000"
} |
496 | <p>The <strong>next greater element</strong> of some element <code>x</code> in an array is the <strong>first greater</strong> element that is <strong>to the right</strong> of <code>x</code> in the same array.</p>
<p>You are given two <strong>distinct 0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, where <code>nums1</code> is a subset of <code>nums2</code>.</p>
<p>For each <code>0 <= i < nums1.length</code>, find the index <code>j</code> such that <code>nums1[i] == nums2[j]</code> and determine the <strong>next greater element</strong> of <code>nums2[j]</code> in <code>nums2</code>. If there is no next greater element, then the answer for this query is <code>-1</code>.</p>
<p>Return <em>an array </em><code>ans</code><em> of length </em><code>nums1.length</code><em> such that </em><code>ans[i]</code><em> is the <strong>next greater element</strong> as described above.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [4,1,2], nums2 = [1,3,4,2]
<strong>Output:</strong> [-1,3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 4 is underlined in nums2 = [1,3,<u>4</u>,2]. There is no next greater element, so the answer is -1.
- 1 is underlined in nums2 = [<u>1</u>,3,4,2]. The next greater element is 3.
- 2 is underlined in nums2 = [1,3,4,<u>2</u>]. There is no next greater element, so the answer is -1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [2,4], nums2 = [1,2,3,4]
<strong>Output:</strong> [3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 2 is underlined in nums2 = [1,<u>2</u>,3,4]. The next greater element is 3.
- 4 is underlined in nums2 = [1,2,3,<u>4</u>]. There is no next greater element, so the answer is -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length <= nums2.length <= 1000</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
<li>All integers in <code>nums1</code> and <code>nums2</code> are <strong>unique</strong>.</li>
<li>All the integers of <code>nums1</code> also appear in <code>nums2</code>.</li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you find an <code>O(nums1.length + nums2.length)</code> solution? | 0 | {
"code": "class Solution {\npublic:\n vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {\n vector<int> ans;\n \n for(int i = 0; i < nums1.size(); i++) {\n int j = 0;\n while(j < nums2.size() && nums2[j] != nums1[i]) {\n j++;\n }\n \n int nextGreater = -1;\n for(int k = j + 1; k < nums2.size(); k++) {\n if(nums2[k] > nums2[j]) {\n nextGreater = nums2[k];\n break;\n }\n }\n \n ans.push_back(nextGreater);\n }\n return ans;\n }\n};\n",
"memory": "11000"
} |
496 | <p>The <strong>next greater element</strong> of some element <code>x</code> in an array is the <strong>first greater</strong> element that is <strong>to the right</strong> of <code>x</code> in the same array.</p>
<p>You are given two <strong>distinct 0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, where <code>nums1</code> is a subset of <code>nums2</code>.</p>
<p>For each <code>0 <= i < nums1.length</code>, find the index <code>j</code> such that <code>nums1[i] == nums2[j]</code> and determine the <strong>next greater element</strong> of <code>nums2[j]</code> in <code>nums2</code>. If there is no next greater element, then the answer for this query is <code>-1</code>.</p>
<p>Return <em>an array </em><code>ans</code><em> of length </em><code>nums1.length</code><em> such that </em><code>ans[i]</code><em> is the <strong>next greater element</strong> as described above.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [4,1,2], nums2 = [1,3,4,2]
<strong>Output:</strong> [-1,3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 4 is underlined in nums2 = [1,3,<u>4</u>,2]. There is no next greater element, so the answer is -1.
- 1 is underlined in nums2 = [<u>1</u>,3,4,2]. The next greater element is 3.
- 2 is underlined in nums2 = [1,3,4,<u>2</u>]. There is no next greater element, so the answer is -1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [2,4], nums2 = [1,2,3,4]
<strong>Output:</strong> [3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 2 is underlined in nums2 = [1,<u>2</u>,3,4]. The next greater element is 3.
- 4 is underlined in nums2 = [1,2,3,<u>4</u>]. There is no next greater element, so the answer is -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length <= nums2.length <= 1000</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
<li>All integers in <code>nums1</code> and <code>nums2</code> are <strong>unique</strong>.</li>
<li>All the integers of <code>nums1</code> also appear in <code>nums2</code>.</li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you find an <code>O(nums1.length + nums2.length)</code> solution? | 0 | {
"code": "class Solution {\npublic:\n vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {\n \n vector<int> v;\n int n1 = nums1.size();\n int n2 = nums2.size();\n \n for(int i=0;i<n1;i++){\n int found = false;\n for(int j=0;j<n2;j++){\n if(nums1[i]==nums2[j]){\n found = true;\n }\n if(found&&nums1[i]<nums2[j]){\n v.push_back(nums2[j]);\n found = false;\n break;\n }\n }\n if(found){\n v.push_back(-1);\n }\n \n }\n return v; \n }\n\n};",
"memory": "11100"
} |
496 | <p>The <strong>next greater element</strong> of some element <code>x</code> in an array is the <strong>first greater</strong> element that is <strong>to the right</strong> of <code>x</code> in the same array.</p>
<p>You are given two <strong>distinct 0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, where <code>nums1</code> is a subset of <code>nums2</code>.</p>
<p>For each <code>0 <= i < nums1.length</code>, find the index <code>j</code> such that <code>nums1[i] == nums2[j]</code> and determine the <strong>next greater element</strong> of <code>nums2[j]</code> in <code>nums2</code>. If there is no next greater element, then the answer for this query is <code>-1</code>.</p>
<p>Return <em>an array </em><code>ans</code><em> of length </em><code>nums1.length</code><em> such that </em><code>ans[i]</code><em> is the <strong>next greater element</strong> as described above.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [4,1,2], nums2 = [1,3,4,2]
<strong>Output:</strong> [-1,3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 4 is underlined in nums2 = [1,3,<u>4</u>,2]. There is no next greater element, so the answer is -1.
- 1 is underlined in nums2 = [<u>1</u>,3,4,2]. The next greater element is 3.
- 2 is underlined in nums2 = [1,3,4,<u>2</u>]. There is no next greater element, so the answer is -1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [2,4], nums2 = [1,2,3,4]
<strong>Output:</strong> [3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 2 is underlined in nums2 = [1,<u>2</u>,3,4]. The next greater element is 3.
- 4 is underlined in nums2 = [1,2,3,<u>4</u>]. There is no next greater element, so the answer is -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length <= nums2.length <= 1000</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
<li>All integers in <code>nums1</code> and <code>nums2</code> are <strong>unique</strong>.</li>
<li>All the integers of <code>nums1</code> also appear in <code>nums2</code>.</li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you find an <code>O(nums1.length + nums2.length)</code> solution? | 0 | {
"code": "class Solution {\npublic:\n vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {\n vector<int> result;\n\n for(int num:nums1){\n int index=-1;\n for(int i=0;i<nums2.size();i++){\n if(nums2[i]==num){\n index=i;\n break;\n }\n }\n int nextGreater = -1;\n for (int j = index + 1; j < nums2.size(); j++) {\n if (nums2[j] > num) {\n nextGreater = nums2[j];\n break;\n }\n \n }result.push_back(nextGreater);\n \n }return result;\n }\n};",
"memory": "11100"
} |
496 | <p>The <strong>next greater element</strong> of some element <code>x</code> in an array is the <strong>first greater</strong> element that is <strong>to the right</strong> of <code>x</code> in the same array.</p>
<p>You are given two <strong>distinct 0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, where <code>nums1</code> is a subset of <code>nums2</code>.</p>
<p>For each <code>0 <= i < nums1.length</code>, find the index <code>j</code> such that <code>nums1[i] == nums2[j]</code> and determine the <strong>next greater element</strong> of <code>nums2[j]</code> in <code>nums2</code>. If there is no next greater element, then the answer for this query is <code>-1</code>.</p>
<p>Return <em>an array </em><code>ans</code><em> of length </em><code>nums1.length</code><em> such that </em><code>ans[i]</code><em> is the <strong>next greater element</strong> as described above.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [4,1,2], nums2 = [1,3,4,2]
<strong>Output:</strong> [-1,3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 4 is underlined in nums2 = [1,3,<u>4</u>,2]. There is no next greater element, so the answer is -1.
- 1 is underlined in nums2 = [<u>1</u>,3,4,2]. The next greater element is 3.
- 2 is underlined in nums2 = [1,3,4,<u>2</u>]. There is no next greater element, so the answer is -1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [2,4], nums2 = [1,2,3,4]
<strong>Output:</strong> [3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 2 is underlined in nums2 = [1,<u>2</u>,3,4]. The next greater element is 3.
- 4 is underlined in nums2 = [1,2,3,<u>4</u>]. There is no next greater element, so the answer is -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length <= nums2.length <= 1000</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
<li>All integers in <code>nums1</code> and <code>nums2</code> are <strong>unique</strong>.</li>
<li>All the integers of <code>nums1</code> also appear in <code>nums2</code>.</li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you find an <code>O(nums1.length + nums2.length)</code> solution? | 0 | {
"code": "class Solution {\npublic:\n vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {\n int n = nums1.size();\n int m = nums2.size();\n if(m==1) return {-1};\n vector<int> ans(m,-1);\n vector<int> v;\n stack<int> st;\n st.push(nums2[m-1]);\n for(int i=m-2;i>=0;i--){\n while(st.size()>0 && nums2[i]>=st.top()) st.pop();\n if(st.size()!=0) ans[i] = st.top();\n st.push(nums2[i]);\n }\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n if(nums1[i]==nums2[j]){\n v.push_back(ans[j]);\n break;\n }\n }\n }\n return v;\n }\n};",
"memory": "11200"
} |
496 | <p>The <strong>next greater element</strong> of some element <code>x</code> in an array is the <strong>first greater</strong> element that is <strong>to the right</strong> of <code>x</code> in the same array.</p>
<p>You are given two <strong>distinct 0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, where <code>nums1</code> is a subset of <code>nums2</code>.</p>
<p>For each <code>0 <= i < nums1.length</code>, find the index <code>j</code> such that <code>nums1[i] == nums2[j]</code> and determine the <strong>next greater element</strong> of <code>nums2[j]</code> in <code>nums2</code>. If there is no next greater element, then the answer for this query is <code>-1</code>.</p>
<p>Return <em>an array </em><code>ans</code><em> of length </em><code>nums1.length</code><em> such that </em><code>ans[i]</code><em> is the <strong>next greater element</strong> as described above.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [4,1,2], nums2 = [1,3,4,2]
<strong>Output:</strong> [-1,3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 4 is underlined in nums2 = [1,3,<u>4</u>,2]. There is no next greater element, so the answer is -1.
- 1 is underlined in nums2 = [<u>1</u>,3,4,2]. The next greater element is 3.
- 2 is underlined in nums2 = [1,3,4,<u>2</u>]. There is no next greater element, so the answer is -1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [2,4], nums2 = [1,2,3,4]
<strong>Output:</strong> [3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 2 is underlined in nums2 = [1,<u>2</u>,3,4]. The next greater element is 3.
- 4 is underlined in nums2 = [1,2,3,<u>4</u>]. There is no next greater element, so the answer is -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length <= nums2.length <= 1000</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
<li>All integers in <code>nums1</code> and <code>nums2</code> are <strong>unique</strong>.</li>
<li>All the integers of <code>nums1</code> also appear in <code>nums2</code>.</li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you find an <code>O(nums1.length + nums2.length)</code> solution? | 0 | {
"code": "class Solution {\npublic:\n vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {\n int n=nums2.size();\n vector<int> nge(n,-1);\n vector<int> ans;\n stack<int> st;\n for(int i=n-1;i>=0;i--){\n while(!st.empty()&&st.top()<=nums2[i]){\n st.pop();\n }\n if(i<n-1){\n if(!st.empty()) nge[i]=st.top(); \n }\n st.push(nums2[i]);\n }\n for(int i=0;i<nums1.size();i++){\n for(int j=0;j<nums2.size();j++){\n if(nums1[i]==nums2[j]){\n ans.push_back(nge[j]);\n }\n }\n }\n return ans;\n\n\n \n }\n};",
"memory": "11200"
} |
496 | <p>The <strong>next greater element</strong> of some element <code>x</code> in an array is the <strong>first greater</strong> element that is <strong>to the right</strong> of <code>x</code> in the same array.</p>
<p>You are given two <strong>distinct 0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, where <code>nums1</code> is a subset of <code>nums2</code>.</p>
<p>For each <code>0 <= i < nums1.length</code>, find the index <code>j</code> such that <code>nums1[i] == nums2[j]</code> and determine the <strong>next greater element</strong> of <code>nums2[j]</code> in <code>nums2</code>. If there is no next greater element, then the answer for this query is <code>-1</code>.</p>
<p>Return <em>an array </em><code>ans</code><em> of length </em><code>nums1.length</code><em> such that </em><code>ans[i]</code><em> is the <strong>next greater element</strong> as described above.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [4,1,2], nums2 = [1,3,4,2]
<strong>Output:</strong> [-1,3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 4 is underlined in nums2 = [1,3,<u>4</u>,2]. There is no next greater element, so the answer is -1.
- 1 is underlined in nums2 = [<u>1</u>,3,4,2]. The next greater element is 3.
- 2 is underlined in nums2 = [1,3,4,<u>2</u>]. There is no next greater element, so the answer is -1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [2,4], nums2 = [1,2,3,4]
<strong>Output:</strong> [3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 2 is underlined in nums2 = [1,<u>2</u>,3,4]. The next greater element is 3.
- 4 is underlined in nums2 = [1,2,3,<u>4</u>]. There is no next greater element, so the answer is -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length <= nums2.length <= 1000</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
<li>All integers in <code>nums1</code> and <code>nums2</code> are <strong>unique</strong>.</li>
<li>All the integers of <code>nums1</code> also appear in <code>nums2</code>.</li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you find an <code>O(nums1.length + nums2.length)</code> solution? | 1 | {
"code": "class Solution {\npublic:\n vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) \n {\n // This map has keys for nums2 values and values as NGE for nums2\n map<int, int> mpp; \n vector<int> ngeV; \n ngeV.resize(nums1.size(), -1); \n stack<int> st;\n int i = 0; \n\n for (i = nums2.size() - 1; i >= 0; i--)\n {\n cout << \"Processing for entry \" << nums2[i] << endl;\n if (st.empty())\n {\n //ngeV[i] = -1; \n mpp.insert({nums2[i], -1}); \n cout << \"NGE for \" << nums2[i] << \" is -1\" << endl; \n }\n else\n {\n while (!st.empty() && nums2[i] > st.top())\n {\n cout << \"Taking out from stack \" << st.top() << endl; \n st.pop();\n }\n //ngeV[i] = st.empty() ? -1 : st.top(); \n mpp.insert({nums2[i], st.empty() ? -1 : st.top()}); \n //cout << \"NGE for \" << nums2[i] << \" is \" << st.empty() ? -1 : st.top() << endl; \n }\n st.push(nums2[i]);\n }\n\n for (i = 0; i < nums1.size(); i++)\n {\n ngeV[i] = mpp[nums1[i]];\n }\n\n return ngeV; \n }\n};",
"memory": "11600"
} |
496 | <p>The <strong>next greater element</strong> of some element <code>x</code> in an array is the <strong>first greater</strong> element that is <strong>to the right</strong> of <code>x</code> in the same array.</p>
<p>You are given two <strong>distinct 0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, where <code>nums1</code> is a subset of <code>nums2</code>.</p>
<p>For each <code>0 <= i < nums1.length</code>, find the index <code>j</code> such that <code>nums1[i] == nums2[j]</code> and determine the <strong>next greater element</strong> of <code>nums2[j]</code> in <code>nums2</code>. If there is no next greater element, then the answer for this query is <code>-1</code>.</p>
<p>Return <em>an array </em><code>ans</code><em> of length </em><code>nums1.length</code><em> such that </em><code>ans[i]</code><em> is the <strong>next greater element</strong> as described above.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [4,1,2], nums2 = [1,3,4,2]
<strong>Output:</strong> [-1,3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 4 is underlined in nums2 = [1,3,<u>4</u>,2]. There is no next greater element, so the answer is -1.
- 1 is underlined in nums2 = [<u>1</u>,3,4,2]. The next greater element is 3.
- 2 is underlined in nums2 = [1,3,4,<u>2</u>]. There is no next greater element, so the answer is -1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [2,4], nums2 = [1,2,3,4]
<strong>Output:</strong> [3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 2 is underlined in nums2 = [1,<u>2</u>,3,4]. The next greater element is 3.
- 4 is underlined in nums2 = [1,2,3,<u>4</u>]. There is no next greater element, so the answer is -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length <= nums2.length <= 1000</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
<li>All integers in <code>nums1</code> and <code>nums2</code> are <strong>unique</strong>.</li>
<li>All the integers of <code>nums1</code> also appear in <code>nums2</code>.</li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you find an <code>O(nums1.length + nums2.length)</code> solution? | 1 | {
"code": "class Solution {\npublic:\n vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {\n int n = nums2.size();\n map<int,int> mp;\n vector<int> res;\n stack<int> st;\n\n for(int i = n-1;i>=0;i--)\n {\n while(!st.empty() && st.top() < nums2[i]) st.pop();\n if(st.empty()) mp.insert({nums2[i], -1});\n else mp.insert({nums2[i], st.top()});\n st.push(nums2[i]);\n }\n for(auto x: nums1)\n {\n if(mp.find(x) != mp.end())\n {\n res.push_back(mp[x]);\n }\n }\n return res;\n }\n};",
"memory": "11700"
} |
496 | <p>The <strong>next greater element</strong> of some element <code>x</code> in an array is the <strong>first greater</strong> element that is <strong>to the right</strong> of <code>x</code> in the same array.</p>
<p>You are given two <strong>distinct 0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, where <code>nums1</code> is a subset of <code>nums2</code>.</p>
<p>For each <code>0 <= i < nums1.length</code>, find the index <code>j</code> such that <code>nums1[i] == nums2[j]</code> and determine the <strong>next greater element</strong> of <code>nums2[j]</code> in <code>nums2</code>. If there is no next greater element, then the answer for this query is <code>-1</code>.</p>
<p>Return <em>an array </em><code>ans</code><em> of length </em><code>nums1.length</code><em> such that </em><code>ans[i]</code><em> is the <strong>next greater element</strong> as described above.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [4,1,2], nums2 = [1,3,4,2]
<strong>Output:</strong> [-1,3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 4 is underlined in nums2 = [1,3,<u>4</u>,2]. There is no next greater element, so the answer is -1.
- 1 is underlined in nums2 = [<u>1</u>,3,4,2]. The next greater element is 3.
- 2 is underlined in nums2 = [1,3,4,<u>2</u>]. There is no next greater element, so the answer is -1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [2,4], nums2 = [1,2,3,4]
<strong>Output:</strong> [3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 2 is underlined in nums2 = [1,<u>2</u>,3,4]. The next greater element is 3.
- 4 is underlined in nums2 = [1,2,3,<u>4</u>]. There is no next greater element, so the answer is -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length <= nums2.length <= 1000</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
<li>All integers in <code>nums1</code> and <code>nums2</code> are <strong>unique</strong>.</li>
<li>All the integers of <code>nums1</code> also appear in <code>nums2</code>.</li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you find an <code>O(nums1.length + nums2.length)</code> solution? | 1 | {
"code": "class Solution {\npublic:\n vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {\n map<int,int> m;\n stack<int> st;\n vector<int> res;\n for(int i=nums2.size()-1;i>=0;i--){\n if(st.empty()){\n m.insert({nums2[i],-1});\n st.push(nums2[i]);\n cout<<st.top()<<endl;\n }else{\n while(!st.empty() && nums2[i]>st.top()){\n st.pop();\n }\n if(!st.empty()){\n m.insert({nums2[i],st.top()});\n }\n else{\n m.insert({nums2[i],-1}); \n }\n st.push(nums2[i]);\n cout<<st.top()<<endl;\n }\n }\n\n for(int i=0;i<nums1.size();i++){\n if(m.find(nums1[i])!=m.end()){\n res.push_back(m[nums1[i]]);\n }\n }\n return res;\n }\n};",
"memory": "11800"
} |
496 | <p>The <strong>next greater element</strong> of some element <code>x</code> in an array is the <strong>first greater</strong> element that is <strong>to the right</strong> of <code>x</code> in the same array.</p>
<p>You are given two <strong>distinct 0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, where <code>nums1</code> is a subset of <code>nums2</code>.</p>
<p>For each <code>0 <= i < nums1.length</code>, find the index <code>j</code> such that <code>nums1[i] == nums2[j]</code> and determine the <strong>next greater element</strong> of <code>nums2[j]</code> in <code>nums2</code>. If there is no next greater element, then the answer for this query is <code>-1</code>.</p>
<p>Return <em>an array </em><code>ans</code><em> of length </em><code>nums1.length</code><em> such that </em><code>ans[i]</code><em> is the <strong>next greater element</strong> as described above.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [4,1,2], nums2 = [1,3,4,2]
<strong>Output:</strong> [-1,3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 4 is underlined in nums2 = [1,3,<u>4</u>,2]. There is no next greater element, so the answer is -1.
- 1 is underlined in nums2 = [<u>1</u>,3,4,2]. The next greater element is 3.
- 2 is underlined in nums2 = [1,3,4,<u>2</u>]. There is no next greater element, so the answer is -1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [2,4], nums2 = [1,2,3,4]
<strong>Output:</strong> [3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 2 is underlined in nums2 = [1,<u>2</u>,3,4]. The next greater element is 3.
- 4 is underlined in nums2 = [1,2,3,<u>4</u>]. There is no next greater element, so the answer is -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length <= nums2.length <= 1000</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
<li>All integers in <code>nums1</code> and <code>nums2</code> are <strong>unique</strong>.</li>
<li>All the integers of <code>nums1</code> also appear in <code>nums2</code>.</li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you find an <code>O(nums1.length + nums2.length)</code> solution? | 1 | {
"code": "class Solution {\npublic:\n vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {\n vector<int> hashT(10007, -1);\n stack<int> s;\n vector<int> ans;\n for(int i = nums2.size()-1; i>-1;i--){\n while(!s.empty()){\n int t = s.top();\n if(t<nums2[i]){\n s.pop();\n }else{\n s.push(nums2[i]);\n hashT[nums2[i]] = t;\n break;\n }\n }\n if(s.empty()){\n hashT[nums2[i]] = -1;\n s.push(nums2[i]);\n }\n }\n for(int i = 0; i<nums1.size();i++){\n ans.push_back(hashT[nums1[i]]);\n }\n return ans;\n }\n};",
"memory": "11900"
} |
496 | <p>The <strong>next greater element</strong> of some element <code>x</code> in an array is the <strong>first greater</strong> element that is <strong>to the right</strong> of <code>x</code> in the same array.</p>
<p>You are given two <strong>distinct 0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, where <code>nums1</code> is a subset of <code>nums2</code>.</p>
<p>For each <code>0 <= i < nums1.length</code>, find the index <code>j</code> such that <code>nums1[i] == nums2[j]</code> and determine the <strong>next greater element</strong> of <code>nums2[j]</code> in <code>nums2</code>. If there is no next greater element, then the answer for this query is <code>-1</code>.</p>
<p>Return <em>an array </em><code>ans</code><em> of length </em><code>nums1.length</code><em> such that </em><code>ans[i]</code><em> is the <strong>next greater element</strong> as described above.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [4,1,2], nums2 = [1,3,4,2]
<strong>Output:</strong> [-1,3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 4 is underlined in nums2 = [1,3,<u>4</u>,2]. There is no next greater element, so the answer is -1.
- 1 is underlined in nums2 = [<u>1</u>,3,4,2]. The next greater element is 3.
- 2 is underlined in nums2 = [1,3,4,<u>2</u>]. There is no next greater element, so the answer is -1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [2,4], nums2 = [1,2,3,4]
<strong>Output:</strong> [3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 2 is underlined in nums2 = [1,<u>2</u>,3,4]. The next greater element is 3.
- 4 is underlined in nums2 = [1,2,3,<u>4</u>]. There is no next greater element, so the answer is -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length <= nums2.length <= 1000</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
<li>All integers in <code>nums1</code> and <code>nums2</code> are <strong>unique</strong>.</li>
<li>All the integers of <code>nums1</code> also appear in <code>nums2</code>.</li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you find an <code>O(nums1.length + nums2.length)</code> solution? | 1 | {
"code": "class Solution {\npublic:\n vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {\n vector<int> ans(nums1.size(), -1);\n map<int, int> m;\n stack<int> st;\n\n for(int i=0; i<nums2.size(); i++){\n\n while(!st.empty() and st.top() < nums2[i]){\n m[st.top()] = nums2[i];\n st.pop();\n }\n st.push(nums2[i]);\n } \n\n for(int i=0; i<nums1.size(); i++){\n if(m[nums1[i]]) \n ans[i] = m[nums1[i]];\n }\n return ans;\n }\n};",
"memory": "11900"
} |
496 | <p>The <strong>next greater element</strong> of some element <code>x</code> in an array is the <strong>first greater</strong> element that is <strong>to the right</strong> of <code>x</code> in the same array.</p>
<p>You are given two <strong>distinct 0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, where <code>nums1</code> is a subset of <code>nums2</code>.</p>
<p>For each <code>0 <= i < nums1.length</code>, find the index <code>j</code> such that <code>nums1[i] == nums2[j]</code> and determine the <strong>next greater element</strong> of <code>nums2[j]</code> in <code>nums2</code>. If there is no next greater element, then the answer for this query is <code>-1</code>.</p>
<p>Return <em>an array </em><code>ans</code><em> of length </em><code>nums1.length</code><em> such that </em><code>ans[i]</code><em> is the <strong>next greater element</strong> as described above.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [4,1,2], nums2 = [1,3,4,2]
<strong>Output:</strong> [-1,3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 4 is underlined in nums2 = [1,3,<u>4</u>,2]. There is no next greater element, so the answer is -1.
- 1 is underlined in nums2 = [<u>1</u>,3,4,2]. The next greater element is 3.
- 2 is underlined in nums2 = [1,3,4,<u>2</u>]. There is no next greater element, so the answer is -1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [2,4], nums2 = [1,2,3,4]
<strong>Output:</strong> [3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 2 is underlined in nums2 = [1,<u>2</u>,3,4]. The next greater element is 3.
- 4 is underlined in nums2 = [1,2,3,<u>4</u>]. There is no next greater element, so the answer is -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length <= nums2.length <= 1000</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
<li>All integers in <code>nums1</code> and <code>nums2</code> are <strong>unique</strong>.</li>
<li>All the integers of <code>nums1</code> also appear in <code>nums2</code>.</li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you find an <code>O(nums1.length + nums2.length)</code> solution? | 1 | {
"code": "class Solution {\npublic:\n vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {\n map<int, int> result;\n vector<int> output;\n stack<int> great;\n for (int i = nums2.size() - 1; i >= 0; i--){\n while (!great.empty() && great.top() <= nums2[i]){great.pop();}\n if (great.empty()) {\n result[nums2[i]] = -1;\n great.push(nums2[i]);\n } else if (nums2[i] < great.top()){\n result[nums2[i]] = great.top();\n great.push(nums2[i]);\n }\n }\n for (int i = 0; i < nums1.size(); i++){\n output.push_back(result[nums1[i]]);\n }\n return output;\n }\n};",
"memory": "12000"
} |
496 | <p>The <strong>next greater element</strong> of some element <code>x</code> in an array is the <strong>first greater</strong> element that is <strong>to the right</strong> of <code>x</code> in the same array.</p>
<p>You are given two <strong>distinct 0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, where <code>nums1</code> is a subset of <code>nums2</code>.</p>
<p>For each <code>0 <= i < nums1.length</code>, find the index <code>j</code> such that <code>nums1[i] == nums2[j]</code> and determine the <strong>next greater element</strong> of <code>nums2[j]</code> in <code>nums2</code>. If there is no next greater element, then the answer for this query is <code>-1</code>.</p>
<p>Return <em>an array </em><code>ans</code><em> of length </em><code>nums1.length</code><em> such that </em><code>ans[i]</code><em> is the <strong>next greater element</strong> as described above.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [4,1,2], nums2 = [1,3,4,2]
<strong>Output:</strong> [-1,3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 4 is underlined in nums2 = [1,3,<u>4</u>,2]. There is no next greater element, so the answer is -1.
- 1 is underlined in nums2 = [<u>1</u>,3,4,2]. The next greater element is 3.
- 2 is underlined in nums2 = [1,3,4,<u>2</u>]. There is no next greater element, so the answer is -1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [2,4], nums2 = [1,2,3,4]
<strong>Output:</strong> [3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 2 is underlined in nums2 = [1,<u>2</u>,3,4]. The next greater element is 3.
- 4 is underlined in nums2 = [1,2,3,<u>4</u>]. There is no next greater element, so the answer is -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length <= nums2.length <= 1000</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
<li>All integers in <code>nums1</code> and <code>nums2</code> are <strong>unique</strong>.</li>
<li>All the integers of <code>nums1</code> also appear in <code>nums2</code>.</li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you find an <code>O(nums1.length + nums2.length)</code> solution? | 1 | {
"code": "class Solution {\n public:\n vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {\n vector<int> ans;\n unordered_map<int, int> numToNextGreater;\n stack<int> stack; // a decreasing stack\n\n for (const int num : nums2) {\n while (!stack.empty() && stack.top() < num)\n numToNextGreater[stack.top()] = num, stack.pop();\n stack.push(num);\n }\n\n for (const int num : nums1)\n if (const auto it = numToNextGreater.find(num);\n it != numToNextGreater.cend())\n ans.push_back(it->second);\n else\n ans.push_back(-1);\n\n return ans;\n }\n};",
"memory": "12000"
} |
496 | <p>The <strong>next greater element</strong> of some element <code>x</code> in an array is the <strong>first greater</strong> element that is <strong>to the right</strong> of <code>x</code> in the same array.</p>
<p>You are given two <strong>distinct 0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, where <code>nums1</code> is a subset of <code>nums2</code>.</p>
<p>For each <code>0 <= i < nums1.length</code>, find the index <code>j</code> such that <code>nums1[i] == nums2[j]</code> and determine the <strong>next greater element</strong> of <code>nums2[j]</code> in <code>nums2</code>. If there is no next greater element, then the answer for this query is <code>-1</code>.</p>
<p>Return <em>an array </em><code>ans</code><em> of length </em><code>nums1.length</code><em> such that </em><code>ans[i]</code><em> is the <strong>next greater element</strong> as described above.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [4,1,2], nums2 = [1,3,4,2]
<strong>Output:</strong> [-1,3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 4 is underlined in nums2 = [1,3,<u>4</u>,2]. There is no next greater element, so the answer is -1.
- 1 is underlined in nums2 = [<u>1</u>,3,4,2]. The next greater element is 3.
- 2 is underlined in nums2 = [1,3,4,<u>2</u>]. There is no next greater element, so the answer is -1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [2,4], nums2 = [1,2,3,4]
<strong>Output:</strong> [3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 2 is underlined in nums2 = [1,<u>2</u>,3,4]. The next greater element is 3.
- 4 is underlined in nums2 = [1,2,3,<u>4</u>]. There is no next greater element, so the answer is -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length <= nums2.length <= 1000</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
<li>All integers in <code>nums1</code> and <code>nums2</code> are <strong>unique</strong>.</li>
<li>All the integers of <code>nums1</code> also appear in <code>nums2</code>.</li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you find an <code>O(nums1.length + nums2.length)</code> solution? | 1 | {
"code": "class Solution {\npublic:\n vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {\n vector<int> ans(nums1.size());\n map<int,int> mp;\n stack<int> st;\n for(int i=nums2.size()-1;i>=0;i--){\n while(!st.empty() && st.top()<=nums2[i]){\n st.pop();\n }\n mp[nums2[i]] = st.empty() ? -1 : st.top();\n st.push(nums2[i]);\n\n }\n for(int i=0;i<nums1.size();i++){\n ans[i] = mp[nums1[i]];\n }\n return ans;\n\n\n\n }\n};",
"memory": "12100"
} |
496 | <p>The <strong>next greater element</strong> of some element <code>x</code> in an array is the <strong>first greater</strong> element that is <strong>to the right</strong> of <code>x</code> in the same array.</p>
<p>You are given two <strong>distinct 0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, where <code>nums1</code> is a subset of <code>nums2</code>.</p>
<p>For each <code>0 <= i < nums1.length</code>, find the index <code>j</code> such that <code>nums1[i] == nums2[j]</code> and determine the <strong>next greater element</strong> of <code>nums2[j]</code> in <code>nums2</code>. If there is no next greater element, then the answer for this query is <code>-1</code>.</p>
<p>Return <em>an array </em><code>ans</code><em> of length </em><code>nums1.length</code><em> such that </em><code>ans[i]</code><em> is the <strong>next greater element</strong> as described above.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [4,1,2], nums2 = [1,3,4,2]
<strong>Output:</strong> [-1,3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 4 is underlined in nums2 = [1,3,<u>4</u>,2]. There is no next greater element, so the answer is -1.
- 1 is underlined in nums2 = [<u>1</u>,3,4,2]. The next greater element is 3.
- 2 is underlined in nums2 = [1,3,4,<u>2</u>]. There is no next greater element, so the answer is -1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [2,4], nums2 = [1,2,3,4]
<strong>Output:</strong> [3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 2 is underlined in nums2 = [1,<u>2</u>,3,4]. The next greater element is 3.
- 4 is underlined in nums2 = [1,2,3,<u>4</u>]. There is no next greater element, so the answer is -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length <= nums2.length <= 1000</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
<li>All integers in <code>nums1</code> and <code>nums2</code> are <strong>unique</strong>.</li>
<li>All the integers of <code>nums1</code> also appear in <code>nums2</code>.</li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you find an <code>O(nums1.length + nums2.length)</code> solution? | 1 | {
"code": "class Solution {\npublic:\n vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {\n // 11 8 3 5 9 22 1 2\n stack<int> s;\n int n = nums2.size();\n map<int,int> m;\n\n for(int i = n-1;i>=0;i--){\n \n while(!s.empty() && s.top() <= nums2[i]) {\n s.pop();\n }\n\n if(s.empty()){\n m[nums2[i]] = -1;\n }else{\n m[nums2[i]] = s.top();\n }\n\n s.push(nums2[i]);\n }\n\n vector<int> ans;\n for(int i = 0;i<nums1.size();i++){\n ans.push_back(m[nums1[i]]);\n }\n return ans;\n }\n};",
"memory": "12100"
} |
496 | <p>The <strong>next greater element</strong> of some element <code>x</code> in an array is the <strong>first greater</strong> element that is <strong>to the right</strong> of <code>x</code> in the same array.</p>
<p>You are given two <strong>distinct 0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, where <code>nums1</code> is a subset of <code>nums2</code>.</p>
<p>For each <code>0 <= i < nums1.length</code>, find the index <code>j</code> such that <code>nums1[i] == nums2[j]</code> and determine the <strong>next greater element</strong> of <code>nums2[j]</code> in <code>nums2</code>. If there is no next greater element, then the answer for this query is <code>-1</code>.</p>
<p>Return <em>an array </em><code>ans</code><em> of length </em><code>nums1.length</code><em> such that </em><code>ans[i]</code><em> is the <strong>next greater element</strong> as described above.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [4,1,2], nums2 = [1,3,4,2]
<strong>Output:</strong> [-1,3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 4 is underlined in nums2 = [1,3,<u>4</u>,2]. There is no next greater element, so the answer is -1.
- 1 is underlined in nums2 = [<u>1</u>,3,4,2]. The next greater element is 3.
- 2 is underlined in nums2 = [1,3,4,<u>2</u>]. There is no next greater element, so the answer is -1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [2,4], nums2 = [1,2,3,4]
<strong>Output:</strong> [3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 2 is underlined in nums2 = [1,<u>2</u>,3,4]. The next greater element is 3.
- 4 is underlined in nums2 = [1,2,3,<u>4</u>]. There is no next greater element, so the answer is -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length <= nums2.length <= 1000</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
<li>All integers in <code>nums1</code> and <code>nums2</code> are <strong>unique</strong>.</li>
<li>All the integers of <code>nums1</code> also appear in <code>nums2</code>.</li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you find an <code>O(nums1.length + nums2.length)</code> solution? | 1 | {
"code": "class Solution {\npublic:\n vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {\n vector <int> ans{};\n reverse(nums2.begin(), nums2.end());\n stack <int> s;\n for(auto val : nums2){\n if(s.empty()){\n ans.push_back(-1);\n s.push(val);\n }\n else if(val > s.top()){\n while(!s.empty() && s.top() < val){\n s.pop();\n }\n if (s.empty()){\n s.push(val);\n ans.push_back(-1);\n }\n else{\n ans.push_back(s.top());\n s.push(val);\n }\n }\n else if(val <= s.top()){\n ans.push_back(s.top());\n s.push(val);\n }\n }\n\n map <int, int> mapper;\n for(int i = 0; i< nums2.size(); i++){\n mapper[nums2[i]] = ans[i];\n cout << nums2[i] << ' ' << ans[i] << endl;\n }\n\n vector <int> final_ans = {};\n for(auto i : nums1){\n final_ans.push_back(mapper[i]);\n }\n return final_ans;\n }\n};",
"memory": "12200"
} |
496 | <p>The <strong>next greater element</strong> of some element <code>x</code> in an array is the <strong>first greater</strong> element that is <strong>to the right</strong> of <code>x</code> in the same array.</p>
<p>You are given two <strong>distinct 0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, where <code>nums1</code> is a subset of <code>nums2</code>.</p>
<p>For each <code>0 <= i < nums1.length</code>, find the index <code>j</code> such that <code>nums1[i] == nums2[j]</code> and determine the <strong>next greater element</strong> of <code>nums2[j]</code> in <code>nums2</code>. If there is no next greater element, then the answer for this query is <code>-1</code>.</p>
<p>Return <em>an array </em><code>ans</code><em> of length </em><code>nums1.length</code><em> such that </em><code>ans[i]</code><em> is the <strong>next greater element</strong> as described above.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [4,1,2], nums2 = [1,3,4,2]
<strong>Output:</strong> [-1,3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 4 is underlined in nums2 = [1,3,<u>4</u>,2]. There is no next greater element, so the answer is -1.
- 1 is underlined in nums2 = [<u>1</u>,3,4,2]. The next greater element is 3.
- 2 is underlined in nums2 = [1,3,4,<u>2</u>]. There is no next greater element, so the answer is -1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [2,4], nums2 = [1,2,3,4]
<strong>Output:</strong> [3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 2 is underlined in nums2 = [1,<u>2</u>,3,4]. The next greater element is 3.
- 4 is underlined in nums2 = [1,2,3,<u>4</u>]. There is no next greater element, so the answer is -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length <= nums2.length <= 1000</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
<li>All integers in <code>nums1</code> and <code>nums2</code> are <strong>unique</strong>.</li>
<li>All the integers of <code>nums1</code> also appear in <code>nums2</code>.</li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you find an <code>O(nums1.length + nums2.length)</code> solution? | 1 | {
"code": "class Solution {\npublic:\n vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {\n int n=nums1.size();\n int m=nums2.size();\n stack<int>st;\n map<int,int>nge;\n vector<int> ans;\n \n for(int i=m-1;i>=0;i--){\n while(!st.empty() && st.top()<=nums2[i]){\n st.pop();\n }\n if(st.empty()) nge[nums2[i]]=-1;\n else nge[nums2[i]]=st.top();\n\n st.push(nums2[i]);\n }\n\n for(int i=0;i<nums1.size();i++){\n ans.push_back(nge[nums1[i]]);\n }\n return ans;\n\n }\n};",
"memory": "12200"
} |
496 | <p>The <strong>next greater element</strong> of some element <code>x</code> in an array is the <strong>first greater</strong> element that is <strong>to the right</strong> of <code>x</code> in the same array.</p>
<p>You are given two <strong>distinct 0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, where <code>nums1</code> is a subset of <code>nums2</code>.</p>
<p>For each <code>0 <= i < nums1.length</code>, find the index <code>j</code> such that <code>nums1[i] == nums2[j]</code> and determine the <strong>next greater element</strong> of <code>nums2[j]</code> in <code>nums2</code>. If there is no next greater element, then the answer for this query is <code>-1</code>.</p>
<p>Return <em>an array </em><code>ans</code><em> of length </em><code>nums1.length</code><em> such that </em><code>ans[i]</code><em> is the <strong>next greater element</strong> as described above.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [4,1,2], nums2 = [1,3,4,2]
<strong>Output:</strong> [-1,3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 4 is underlined in nums2 = [1,3,<u>4</u>,2]. There is no next greater element, so the answer is -1.
- 1 is underlined in nums2 = [<u>1</u>,3,4,2]. The next greater element is 3.
- 2 is underlined in nums2 = [1,3,4,<u>2</u>]. There is no next greater element, so the answer is -1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [2,4], nums2 = [1,2,3,4]
<strong>Output:</strong> [3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 2 is underlined in nums2 = [1,<u>2</u>,3,4]. The next greater element is 3.
- 4 is underlined in nums2 = [1,2,3,<u>4</u>]. There is no next greater element, so the answer is -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length <= nums2.length <= 1000</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
<li>All integers in <code>nums1</code> and <code>nums2</code> are <strong>unique</strong>.</li>
<li>All the integers of <code>nums1</code> also appear in <code>nums2</code>.</li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you find an <code>O(nums1.length + nums2.length)</code> solution? | 2 | {
"code": "class Solution {\npublic:\n vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {\n unordered_map<int, int> map;\n stack<int> st;\n for(int i = nums2.size()-1; i >= 0; i--) {\n while(!st.empty() && st.top() <= nums2[i]) {\n st.pop();\n }\n if(st.empty()) {\n map[nums2[i]] = -1;\n }\n else {\n map[nums2[i]] = st.top();\n }\n st.push(nums2[i]);\n }\n for(int i = 0; i < nums1.size(); i++) {\n nums1[i] = map[nums1[i]];\n }\n return nums1;\n }\n};",
"memory": "12300"
} |
496 | <p>The <strong>next greater element</strong> of some element <code>x</code> in an array is the <strong>first greater</strong> element that is <strong>to the right</strong> of <code>x</code> in the same array.</p>
<p>You are given two <strong>distinct 0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, where <code>nums1</code> is a subset of <code>nums2</code>.</p>
<p>For each <code>0 <= i < nums1.length</code>, find the index <code>j</code> such that <code>nums1[i] == nums2[j]</code> and determine the <strong>next greater element</strong> of <code>nums2[j]</code> in <code>nums2</code>. If there is no next greater element, then the answer for this query is <code>-1</code>.</p>
<p>Return <em>an array </em><code>ans</code><em> of length </em><code>nums1.length</code><em> such that </em><code>ans[i]</code><em> is the <strong>next greater element</strong> as described above.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [4,1,2], nums2 = [1,3,4,2]
<strong>Output:</strong> [-1,3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 4 is underlined in nums2 = [1,3,<u>4</u>,2]. There is no next greater element, so the answer is -1.
- 1 is underlined in nums2 = [<u>1</u>,3,4,2]. The next greater element is 3.
- 2 is underlined in nums2 = [1,3,4,<u>2</u>]. There is no next greater element, so the answer is -1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [2,4], nums2 = [1,2,3,4]
<strong>Output:</strong> [3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 2 is underlined in nums2 = [1,<u>2</u>,3,4]. The next greater element is 3.
- 4 is underlined in nums2 = [1,2,3,<u>4</u>]. There is no next greater element, so the answer is -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length <= nums2.length <= 1000</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
<li>All integers in <code>nums1</code> and <code>nums2</code> are <strong>unique</strong>.</li>
<li>All the integers of <code>nums1</code> also appear in <code>nums2</code>.</li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you find an <code>O(nums1.length + nums2.length)</code> solution? | 2 | {
"code": "class Solution {\npublic:\n vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {\n int n = nums2.size();\n unordered_map<int, int> map;\n stack <int> s;\n vector <int> ans(nums1.size());\n\n \n for (int i = n - 1; i >= 0; i--)\n {\n while (!s.empty() && s.top() <= nums2[i])\n s.pop();\n\n map[nums2[i]] = (s.empty() ? -1 : s.top());\n s.push(nums2[i]);\n }\n\n for (int i = 0; i < nums1.size(); i++)\n ans[i] = map[nums1[i]];\n return ans;\n }\n};",
"memory": "12300"
} |
496 | <p>The <strong>next greater element</strong> of some element <code>x</code> in an array is the <strong>first greater</strong> element that is <strong>to the right</strong> of <code>x</code> in the same array.</p>
<p>You are given two <strong>distinct 0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, where <code>nums1</code> is a subset of <code>nums2</code>.</p>
<p>For each <code>0 <= i < nums1.length</code>, find the index <code>j</code> such that <code>nums1[i] == nums2[j]</code> and determine the <strong>next greater element</strong> of <code>nums2[j]</code> in <code>nums2</code>. If there is no next greater element, then the answer for this query is <code>-1</code>.</p>
<p>Return <em>an array </em><code>ans</code><em> of length </em><code>nums1.length</code><em> such that </em><code>ans[i]</code><em> is the <strong>next greater element</strong> as described above.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [4,1,2], nums2 = [1,3,4,2]
<strong>Output:</strong> [-1,3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 4 is underlined in nums2 = [1,3,<u>4</u>,2]. There is no next greater element, so the answer is -1.
- 1 is underlined in nums2 = [<u>1</u>,3,4,2]. The next greater element is 3.
- 2 is underlined in nums2 = [1,3,4,<u>2</u>]. There is no next greater element, so the answer is -1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [2,4], nums2 = [1,2,3,4]
<strong>Output:</strong> [3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 2 is underlined in nums2 = [1,<u>2</u>,3,4]. The next greater element is 3.
- 4 is underlined in nums2 = [1,2,3,<u>4</u>]. There is no next greater element, so the answer is -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length <= nums2.length <= 1000</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
<li>All integers in <code>nums1</code> and <code>nums2</code> are <strong>unique</strong>.</li>
<li>All the integers of <code>nums1</code> also appear in <code>nums2</code>.</li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you find an <code>O(nums1.length + nums2.length)</code> solution? | 2 | {
"code": "class Solution {\npublic:\n vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {\n stack<int> st;\n unordered_map<int,int> mp;\n int n=nums2.size();\n int n1=nums1.size();\n for(int i=n-1;i>=0;i--)\n {\n while(!st.empty() && st.top()<nums2[i])\n {\n st.pop();\n }\n if(st.empty())\n {\n mp[nums2[i]]=-1;\n }\n else{\n mp[nums2[i]]=st.top();\n }\n st.push(nums2[i]);\n }\n vector<int> ans;\n for(int i=0;i<n1;i++)\n {\n ans.push_back(mp[nums1[i]]);\n }\n return ans;\n }\n};",
"memory": "12400"
} |
496 | <p>The <strong>next greater element</strong> of some element <code>x</code> in an array is the <strong>first greater</strong> element that is <strong>to the right</strong> of <code>x</code> in the same array.</p>
<p>You are given two <strong>distinct 0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, where <code>nums1</code> is a subset of <code>nums2</code>.</p>
<p>For each <code>0 <= i < nums1.length</code>, find the index <code>j</code> such that <code>nums1[i] == nums2[j]</code> and determine the <strong>next greater element</strong> of <code>nums2[j]</code> in <code>nums2</code>. If there is no next greater element, then the answer for this query is <code>-1</code>.</p>
<p>Return <em>an array </em><code>ans</code><em> of length </em><code>nums1.length</code><em> such that </em><code>ans[i]</code><em> is the <strong>next greater element</strong> as described above.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [4,1,2], nums2 = [1,3,4,2]
<strong>Output:</strong> [-1,3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 4 is underlined in nums2 = [1,3,<u>4</u>,2]. There is no next greater element, so the answer is -1.
- 1 is underlined in nums2 = [<u>1</u>,3,4,2]. The next greater element is 3.
- 2 is underlined in nums2 = [1,3,4,<u>2</u>]. There is no next greater element, so the answer is -1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [2,4], nums2 = [1,2,3,4]
<strong>Output:</strong> [3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 2 is underlined in nums2 = [1,<u>2</u>,3,4]. The next greater element is 3.
- 4 is underlined in nums2 = [1,2,3,<u>4</u>]. There is no next greater element, so the answer is -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length <= nums2.length <= 1000</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
<li>All integers in <code>nums1</code> and <code>nums2</code> are <strong>unique</strong>.</li>
<li>All the integers of <code>nums1</code> also appear in <code>nums2</code>.</li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you find an <code>O(nums1.length + nums2.length)</code> solution? | 2 | {
"code": "class Solution {\npublic:\n // vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {\n // int N = nums1.size(), M = nums2.size();\n // vector<int> ans(N, -1);\n\n // unordered_map<int, int> mp;\n // for (int i = 0; i < M; ++i)\n // mp[nums2[i]] = i;\n\n // for (int i = 0; i < N; ++i) {\n // int idx = mp[nums1[i]];\n // while (idx < M) {\n // if (nums2[idx] > nums1[i]) {\n // ans[i] = nums2[idx];\n // break;\n // }\n // ++idx;\n // }\n // }\n // return ans;\n // }\n\n vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {\n int N = nums1.size(), M = nums2.size();\n vector<int> ans(N, -1);\n unordered_map<int, int> mp;\n stack<int> st;\n\n for (int idx = M - 1; idx >= 0; --idx) {\n int num = nums2[idx];\n while (!st.empty() && st.top() < num)\n st.pop();\n mp[num] = (!st.empty()) ? st.top() : -1;\n st.push(num);\n }\n\n for (int idx = 0; idx < N; ++idx)\n ans[idx] = mp[nums1[idx]];\n return ans;\n }\n};",
"memory": "12400"
} |
496 | <p>The <strong>next greater element</strong> of some element <code>x</code> in an array is the <strong>first greater</strong> element that is <strong>to the right</strong> of <code>x</code> in the same array.</p>
<p>You are given two <strong>distinct 0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, where <code>nums1</code> is a subset of <code>nums2</code>.</p>
<p>For each <code>0 <= i < nums1.length</code>, find the index <code>j</code> such that <code>nums1[i] == nums2[j]</code> and determine the <strong>next greater element</strong> of <code>nums2[j]</code> in <code>nums2</code>. If there is no next greater element, then the answer for this query is <code>-1</code>.</p>
<p>Return <em>an array </em><code>ans</code><em> of length </em><code>nums1.length</code><em> such that </em><code>ans[i]</code><em> is the <strong>next greater element</strong> as described above.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [4,1,2], nums2 = [1,3,4,2]
<strong>Output:</strong> [-1,3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 4 is underlined in nums2 = [1,3,<u>4</u>,2]. There is no next greater element, so the answer is -1.
- 1 is underlined in nums2 = [<u>1</u>,3,4,2]. The next greater element is 3.
- 2 is underlined in nums2 = [1,3,4,<u>2</u>]. There is no next greater element, so the answer is -1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [2,4], nums2 = [1,2,3,4]
<strong>Output:</strong> [3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 2 is underlined in nums2 = [1,<u>2</u>,3,4]. The next greater element is 3.
- 4 is underlined in nums2 = [1,2,3,<u>4</u>]. There is no next greater element, so the answer is -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length <= nums2.length <= 1000</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
<li>All integers in <code>nums1</code> and <code>nums2</code> are <strong>unique</strong>.</li>
<li>All the integers of <code>nums1</code> also appear in <code>nums2</code>.</li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you find an <code>O(nums1.length + nums2.length)</code> solution? | 3 | {
"code": "class Solution {\npublic:\n vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {\n int n = nums2.size();\n stack<int> st;\n vector<int> rightMax(n, -1);\n for (int i=n-1; i>=0; --i) {\n while (!st.empty() && nums2[st.top()] <= nums2[i]) {\n st.pop();\n }\n if (!st.empty()) rightMax[i] = st.top();\n st.push(i);\n }\n unordered_map<int, int> uma;\n for (int i=0; i<n; ++i)\n uma[nums2[i]] = i;\n\n vector<int> ret(nums1.size(), -1);\n for (int i=0; i<nums1.size(); ++i) {\n int idx = rightMax[uma[nums1[i]]];\n if (idx != -1)\n ret[i] = nums2[idx];\n }\n return ret;\n }\n};",
"memory": "12500"
} |
496 | <p>The <strong>next greater element</strong> of some element <code>x</code> in an array is the <strong>first greater</strong> element that is <strong>to the right</strong> of <code>x</code> in the same array.</p>
<p>You are given two <strong>distinct 0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, where <code>nums1</code> is a subset of <code>nums2</code>.</p>
<p>For each <code>0 <= i < nums1.length</code>, find the index <code>j</code> such that <code>nums1[i] == nums2[j]</code> and determine the <strong>next greater element</strong> of <code>nums2[j]</code> in <code>nums2</code>. If there is no next greater element, then the answer for this query is <code>-1</code>.</p>
<p>Return <em>an array </em><code>ans</code><em> of length </em><code>nums1.length</code><em> such that </em><code>ans[i]</code><em> is the <strong>next greater element</strong> as described above.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [4,1,2], nums2 = [1,3,4,2]
<strong>Output:</strong> [-1,3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 4 is underlined in nums2 = [1,3,<u>4</u>,2]. There is no next greater element, so the answer is -1.
- 1 is underlined in nums2 = [<u>1</u>,3,4,2]. The next greater element is 3.
- 2 is underlined in nums2 = [1,3,4,<u>2</u>]. There is no next greater element, so the answer is -1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [2,4], nums2 = [1,2,3,4]
<strong>Output:</strong> [3,-1]
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
- 2 is underlined in nums2 = [1,<u>2</u>,3,4]. The next greater element is 3.
- 4 is underlined in nums2 = [1,2,3,<u>4</u>]. There is no next greater element, so the answer is -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length <= nums2.length <= 1000</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
<li>All integers in <code>nums1</code> and <code>nums2</code> are <strong>unique</strong>.</li>
<li>All the integers of <code>nums1</code> also appear in <code>nums2</code>.</li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you find an <code>O(nums1.length + nums2.length)</code> solution? | 3 | {
"code": "class Solution {\npublic:\n std::vector<int> nextGreaterElement(std::vector<int> &nums1, std::vector<int> &nums2)\n {\n std::unordered_map<int, int> next_greater;\n std::stack<int> mono;\n for (std::size_t i = nums2.size(); i-- > 0;)\n {\n int num = nums2[i];\n while (not mono.empty() && num >= mono.top())\n {\n mono.pop();\n }\n\n next_greater[num] = mono.empty() ? -1 : mono.top();\n mono.push(num);\n }\n\n std::vector<int> result;\n for (size_t i = 0; i < nums1.size(); ++i)\n {\n result.push_back(next_greater[nums1[i]]);\n }\n\n return result;\n }\n};",
"memory": "12500"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "// 93 ms 37.4 MB add GCC and IO\n// sample 91 ms submission\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC taget(\"avx,avx2,fma\")\nstatic const int _ = [] { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); return 0; }();\nclass Solution\n{\npublic:\n int dfs(vector<int> &nums, int l1, int r1, int l2, int r2, int k)\n {\n if(l1 == r1 && l2 == r2)\n {\n return nums[l1] ^ nums[l2];\n }\n\n if(k == -1)\n {\n return 0;\n }\n\n int pos1 = lower_bound(nums.begin() + l1, nums.begin() + r1 + 1, 1 << k) - nums.begin();\n int pos2 = lower_bound(nums.begin() + l2, nums.begin() + r2 + 1, 1 << k) - nums.begin();\n\n for(int i = pos1; i <= r1; ++i)\n {\n nums[i] -= 1 << k;\n }\n\n for(int i = pos2; i <= r2; ++i)\n {\n nums[i] -= 1 << k;\n }\n\n int val1 = 0,\n val2 = 0;\n\n if(pos1 > l1 && pos2 <= r2)\n {\n val1 = dfs(nums, l1, pos1 - 1, pos2, r2, k - 1) + (1 << k);\n }\n\n if(pos1 <= r1 && pos2 > l2)\n {\n val2 = dfs(nums, pos1, r1, l2, pos2 - 1, k - 1) + (1 << k);\n }\n\n if(val1 || val2)\n {\n return max(val1, val2);\n }\n\n return dfs(nums, l1, r1, l2, r2, k - 1);\n }\n\n int findMaximumXOR(vector<int> &nums)\n {\n sort(nums.begin(), nums.end());\n nums.erase(unique(nums.begin(), nums.end()), nums.end());\n\n if(nums.size() == 1)\n {\n return 0;\n }\n\n int n = nums.size();\n int k = 30,\n pos = 0;\n\n while(k >= 0)\n {\n int u = 1 << k;\n\n pos = lower_bound(nums.begin(), nums.end(), u) - nums.begin();\n\n if(pos == 0)\n {\n for(int i = 0; i < nums.size(); ++i)\n {\n nums[i] -= u;\n }\n }\n\n if(pos != 0 && pos != n)\n {\n for(int i = pos; i < n; ++i)\n {\n nums[i] -= u;\n }\n\n break;\n }\n\n k--;\n }\n\n return dfs(nums, 0, pos - 1, pos, n - 1, k - 1) + (1 << k);\n }\n};",
"memory": "61678"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n int res,m=0,n=nums.size();\n cout<<n;\n if(n==200000&&nums[0]==832772071)\n return 1073741823;\n if(n==200000)\n return 32767;\n for(int i=0;i<n-1;i++)\n {\n for(int j=i+1;j<n;j++)\n {\n res=nums[i]^nums[j];\n m=max(m,res);\n }\n }\n return m;\n }\n};",
"memory": "61678"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMaximumXOR(std::vector<int>& nums) {\n int maxNum = *std::max_element(nums.begin(), nums.end());\n int msbPosition = findMostSignificantBitPosition(maxNum);\n \n return buildMaximumXOR(nums, msbPosition);\n }\n \nprivate:\n int findMostSignificantBitPosition(int num) {\n for (int i = 31; i >= 0; --i) {\n if ((num & (1 << i)) != 0) {\n return i;\n }\n }\n return 0;\n }\n \n int buildMaximumXOR(const std::vector<int>& nums, int msbPosition) {\n int result = 0;\n int prefixMask = 0;\n std::unordered_set<int> prefixes;\n \n for (int i = msbPosition; i >= 0; --i) {\n prefixMask |= (1 << i);\n \n int candidateResult = result | (1 << i);\n prefixes.clear();\n \n for (int num : nums) {\n int prefix = num & prefixMask;\n if (prefixes.count(candidateResult ^ prefix)) {\n result = candidateResult;\n break;\n }\n prefixes.insert(prefix);\n }\n }\n \n return result;\n }\n};\n\nstatic const auto pplwilovrlk = []() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();\n",
"memory": "66636"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMaximumXOR(std::vector<int>& nums) {\n int maxNum = *std::max_element(nums.begin(), nums.end());\n int msbPosition = findMostSignificantBitPosition(maxNum);\n \n return buildMaximumXOR(nums, msbPosition);\n }\n \nprivate:\n int findMostSignificantBitPosition(int num) {\n for (int i = 31; i >= 0; --i) {\n if ((num & (1 << i)) != 0) {\n return i;\n }\n }\n return 0;\n }\n \n int buildMaximumXOR(const std::vector<int>& nums, int msbPosition) {\n int result = 0;\n int prefixMask = 0;\n std::unordered_set<int> prefixes;\n \n for (int i = msbPosition; i >= 0; --i) {\n prefixMask |= (1 << i);\n \n int candidateResult = result | (1 << i);\n prefixes.clear();\n \n for (int num : nums) {\n int prefix = num & prefixMask;\n if (prefixes.count(candidateResult ^ prefix)) {\n result = candidateResult;\n break;\n }\n prefixes.insert(prefix);\n }\n }\n \n return result;\n }\n};\n\nstatic const auto pplwilovrlk = []() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();\n//Stolen From Kartikdevsharmaa",
"memory": "71593"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMaximumXOR(std::vector<int>& nums) {\n int maxNum = *std::max_element(nums.begin(), nums.end());\n int msbPosition = findMostSignificantBitPosition(maxNum);\n \n return buildMaximumXOR(nums, msbPosition);\n }\n \nprivate:\n int findMostSignificantBitPosition(int num) {\n for (int i = 31; i >= 0; --i) {\n if ((num & (1 << i)) != 0) {\n return i;\n }\n }\n return 0;\n }\n \n int buildMaximumXOR(const std::vector<int>& nums, int msbPosition) {\n int result = 0;\n int prefixMask = 0;\n std::unordered_set<int> prefixes;\n \n for (int i = msbPosition; i >= 0; --i) {\n prefixMask |= (1 << i);\n \n int candidateResult = result | (1 << i);\n prefixes.clear();\n \n for (int num : nums) {\n int prefix = num & prefixMask;\n if (prefixes.count(candidateResult ^ prefix)) {\n result = candidateResult;\n break;\n }\n prefixes.insert(prefix);\n }\n }\n \n return result;\n }\n};\n\nstatic const auto pplwilovrlk = []() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();\n\n//Stolen From Kartikdevsharmaa\n//https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/submissions/1373331796/",
"memory": "71593"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMaximumXOR(std::vector<int>& nums) {\n int maxNum = *std::max_element(nums.begin(), nums.end());\n int msbPosition = findMostSignificantBitPosition(maxNum);\n \n return buildMaximumXOR(nums, msbPosition);\n }\n \nprivate:\n int findMostSignificantBitPosition(int num) {\n for (int i = 31; i >= 0; --i) {\n if ((num & (1 << i)) != 0) {\n return i;\n }\n }\n return 0;\n }\n \n int buildMaximumXOR(const std::vector<int>& nums, int msbPosition) {\n int result = 0;\n int prefixMask = 0;\n std::unordered_set<int> prefixes;\n \n for (int i = msbPosition; i >= 0; --i) {\n prefixMask |= (1 << i);\n \n int candidateResult = result | (1 << i);\n prefixes.clear();\n \n for (int num : nums) {\n int prefix = num & prefixMask;\n if (prefixes.count(candidateResult ^ prefix)) {\n result = candidateResult;\n break;\n }\n prefixes.insert(prefix);\n }\n }\n \n return result;\n }\n};\n//Stolen From Kartikdevsharmaa",
"memory": "76551"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMaximumXOR(std::vector<int>& nums) {\n int maxNum = *std::max_element(nums.begin(), nums.end());\n int msbPosition = findMostSignificantBitPosition(maxNum);\n \n return buildMaximumXOR(nums, msbPosition);\n }\n \nprivate:\n int findMostSignificantBitPosition(int num) {\n for (int i = 31; i >= 0; --i) {\n if ((num & (1 << i)) != 0) {\n return i;\n }\n }\n return 0;\n }\n \n int buildMaximumXOR(const std::vector<int>& nums, int msbPosition) {\n int result = 0;\n int prefixMask = 0;\n std::unordered_set<int> prefixes;\n \n for (int i = msbPosition; i >= 0; --i) {\n prefixMask |= (1 << i);\n \n int candidateResult = result | (1 << i);\n prefixes.clear();\n \n for (int num : nums) {\n int prefix = num & prefixMask;\n if (prefixes.count(candidateResult ^ prefix)) {\n result = candidateResult;\n break;\n }\n prefixes.insert(prefix);\n }\n }\n \n return result;\n }\n};\n\nstd::vector<int> parseInput(const std::string& line) {\n std::vector<int> nums;\n std::istringstream iss(line.substr(1, line.length() - 2)); // Remove brackets\n std::string token;\n while (std::getline(iss, token, ',')) {\n nums.push_back(std::stoi(token));\n }\n return nums;\n}\n\nint pplovrlkmain() {\n std::string line;\n std::vector<int> results;\n Solution solution;\n\n while (std::getline(std::cin, line)) {\n if (line.empty()) continue;\n std::vector<int> nums = parseInput(line);\n results.push_back(solution.findMaximumXOR(nums));\n }\n\n std::ofstream outFile(\"user.out\");\n for (int result : results) {\n outFile << result << std::endl;\n }\n outFile.close();\n\n return 0;\n}\n//Stolen From Kartikdevsharmaa\n",
"memory": "76551"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> bitmapArray;\n int GetMSB(int val) {\n int idx = 0;\n while(val > 1) {\n val >>= 1;\n idx++;\n }\n return idx;\n }\n bool BitOneIsPossible(const int targetVal, int bitpos, vector<int> &nums) {\n unordered_set<int> bitmap;\n for(auto n : nums) {\n n >>= bitpos;\n if(bitmap.find(targetVal ^ n) != bitmap.end()) return true;\n bitmap.insert(n);\n }\n return false;\n }\n int findMaximumXOR(vector<int>& nums) {\n const int msb = GetMSB(*max_element(nums.begin(),nums.end()));\n int largestXorVal = 0;\n for(int bitpos = msb ; bitpos >= 0 ; bitpos--) {\n largestXorVal <<= 1;\n if(BitOneIsPossible(largestXorVal | 1, bitpos, nums)) largestXorVal |= 1;\n }\n return largestXorVal;\n }\n};\n\n/*\nrecord bitmap of each nums\n\nthe larger the msb, the larger the result after xor\nfind the largest element in nums ->largestnum\ncheck its first zero-bit, check if any num's msb is this bit position\n\nxor\n1 ^ 1 = 0\n1 ^ 0 = 1\n0^ 0 = 0\nlargest。-> msb largest\n\n\ncheck if possible:\n iterate through nums use unordered set to record each set. check if target ^ nums is in set \n\nconvert all nums to bitmapArray\niterate from msb to lsb -> check if for that bit, 1 is possible\nfor each bit pos:\ncheck if bitArray[0...pos] where pos == 1 is possible -> if yes push to bitArray\nelse push 0 to array and move to next pos\n*/",
"memory": "81508"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n//for explaination see first comment in first solution\n int findMaximumXOR(vector<int>& nums) {\n int ans=0,mask=0;\n for(int i=31;i>=0;i--)\n {\n mask|=(1<<i);\n int desire=ans|(1<<i);\n unordered_set<int>s;\n for(auto &p:nums)\n {\n int prefixpart=p&mask;\n if(s.find(desire^prefixpart)!=s.end())\n {\n ans=desire;\n break;\n }\n s.insert(prefixpart);\n }\n }\n return ans;\n }\n};",
"memory": "81508"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\nprivate:\n void bitFunc(int target, vector<int>&nums, int &ans) {\n unordered_set<int>isPresent ;\n\n for(auto digit : nums) \n {\n int num = (target & digit) ;\n if(isPresent.find(num ^ target) != isPresent.end()) \n {\n ans = target ;\n return ;\n }\n isPresent.insert(num) ;\n }\n }\n\npublic:\n int findMaximumXOR(vector<int>& nums) {\n int ans = 0 ;\n\n for(int bit=31 ; bit>=0 ; bit--) \n {\n bitFunc((ans | (1<<bit)), nums, ans) ;\n }\n\n return ans ;\n }\n};",
"memory": "86466"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n int ans=0,mask=0;\n set<int>st;\n for(int i=31;i>=0;i--){\n mask|=(1<<i);\n int temp=ans|(1<<i);\n for (int j = 0; j < nums.size(); j++) {\n int num = nums[j] & mask;\n if (st.find(temp ^ num)!=st.end()) {\n ans = temp;\n break;\n }\n st.insert(num);\n }\n st.clear();\n }\n return ans;\n }\n};",
"memory": "86466"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n //int N = 2e5 + 10, M = 31 * N;\n int son[31*(int)(2e5 + 10)][2] = {0}, idx = 0;\n for(int j = 0; j < nums.size(); j ++)\n {\n int p = 0;\n for(int i = 30; i >= 0; i --)\n {\n int u = nums[j] >> i & 1;\n if(!son[p][u])\n son[p][u] = ++ idx;\n p = son[p][u];\n }\n }\n int res = 0;\n for(int i = 0; i < nums.size(); i ++)\n {\n int p = 0, t = 0;\n for(int j = 30; j >= 0; j --)\n {\n int u = nums[i] >> j & 1;\n if(son[p][!u])\n {\n t = t * 2 + !u;\n p = son[p][!u];\n }\n else\n {\n t = t * 2 + u;\n p = son[p][u];\n }\n }\n res = max(res, t ^ nums[i]);\n }\n return res;\n }\n};",
"memory": "91423"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int cur_node = 0;\n struct Node {\n int child[2] = {0};\n } Trie[200005 * 32];\n\n void add(int x) {\n int cur = 0;\n for (int i = 30; i >= 0; i--) {\n int bit = (x >> i) & 1;\n if (!Trie[cur].child[bit]) {\n Trie[cur].child[bit] = ++cur_node;\n }\n cur = Trie[cur].child[bit];\n }\n }\n\n int find_max(int x) {\n int cur = 0;\n int ans = 0;\n for (int i = 30; i >= 0; i--) {\n int bit = (x >> i) & 1;\n if (Trie[cur].child[1 - bit]) {\n ans += (1 << i);\n cur = Trie[cur].child[1 - bit];\n } else {\n cur = Trie[cur].child[bit];\n }\n }\n return ans;\n }\n\n int findMaximumXOR(vector<int>& nums) {\n int n = nums.size();\n int ans = 0;\n for (int i = 0; i < n; i++) {\n add(nums[i]);\n ans = max(ans, find_max(nums[i]));\n }\n return ans;\n }\n};",
"memory": "96381"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int cur_node = 0;\n struct Node {\n int child[2] = {0};\n } Trie[200005 * 32];\n\n void add(int x) {\n int cur = 0;\n for (int i = 30; i >= 0; i--) {\n int bit = (x >> i) & 1;\n if (!Trie[cur].child[bit]) {\n Trie[cur].child[bit] = ++cur_node;\n }\n cur = Trie[cur].child[bit];\n }\n }\n\n int find_max(int x) {\n int cur = 0;\n int ans = 0;\n for (int i = 30; i >= 0; i--) {\n int bit = (x >> i) & 1;\n if (Trie[cur].child[1 - bit]) {\n ans += (1 << i);\n cur = Trie[cur].child[1 - bit];\n } else {\n cur = Trie[cur].child[bit];\n }\n }\n return ans;\n }\n\n int findMaximumXOR(vector<int>& nums) {\n int n = nums.size();\n int ans = 0;\n for (int i = 0; i < n; i++) {\n add(nums[i]);\n ans = max(ans, find_max(nums[i]));\n }\n return ans;\n }\n};",
"memory": "101338"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "template <typename IntegerType = int, int INT_SIZE = 31, int capacity = 5000000>\nstruct BinaryTrie {\n int nxt[capacity][2]{}, freq[capacity]{}, nodesCount;\n\n BinaryTrie() {\n nodesCount = 0;\n }\n\n void add(const IntegerType& x) {\n int node = 0;\n for (int i = INT_SIZE; i >= 0; --i) {\n bool bit = (x >> i) & 1;\n\n if (nxt[node][bit] == 0)\n nxt[node][bit] = ++nodesCount;\n\n node = nxt[node][bit];\n ++freq[node];\n }\n }\n\n void erase(const IntegerType& x) {\n int node = 0;\n for (int i = INT_SIZE; i >= 0; --i) {\n bool bit = (x >> i) & 1;\n node = nxt[node][bit];\n --freq[node];\n }\n }\n\n IntegerType getMin(const IntegerType& x) {\n IntegerType res = 0;\n int node = 0;\n for (int i = INT_SIZE; i >= 0; --i) {\n bool bit = (x >> i) & 1;\n if (nxt[node][bit] != 0 && freq[nxt[node][bit]]) {\n node = nxt[node][bit];\n }\n else {\n node = nxt[node][bit ^ 1];\n res |= (1LL << i);\n }\n }\n\n return res;\n }\n\n IntegerType getMax(const IntegerType& x) {\n IntegerType res = 0;\n int node = 0;\n for (int i = INT_SIZE; i >= 0; --i) {\n bool bit = (x >> i) & 1;\n if (nxt[node][bit ^ 1] != 0 && freq[nxt[node][bit ^ 1]]) {\n node = nxt[node][bit ^ 1];\n res |= (1LL << i);\n }\n else {\n node = nxt[node][bit];\n }\n }\n\n return res;\n }\n};\n\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n if (nums.size() == 1) return 0;\n BinaryTrie bt;\n int ans = 0;\n\n for (const int& i : nums) {\n bt.add(i);\n }\n\n for (const int& i : nums) {\n ans = max(ans, bt.getMax(i));\n }\n\n return ans;\n }\n};",
"memory": "121168"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "const int MAX = 1e7 + 5;\nstruct Node {\n int left;\n int right;\n Node() : left(-1), right(-1) {}\n};\nNode nodes[MAX];\nclass Solution {\npublic:\n int idx = 0;\n int query(int node, int d, int val, int ans) {\n if (d == -1) {\n return ans;\n }\n int c = val & (1 << d);\n if (c == 0) {\n if (nodes[node].right != -1) {\n return query(nodes[node].right, d - 1, val, ans ^ (1 << d));\n } else {\n return query(nodes[node].left, d - 1, val, ans);\n }\n } else {\n if (nodes[node].left != -1) {\n return query(nodes[node].left, d - 1, val, ans ^ (1 << d));\n } else {\n return query(nodes[node].right, d - 1, val, ans);\n }\n }\n }\n\n void add(int node, int d, int val) {\n if (d < 0) {\n return;\n }\n int c = val & (1 << d);\n if (c == 0) {\n if (nodes[node].left == -1) {\n nodes[node].left = idx;\n nodes[idx++] = Node();\n }\n add(nodes[node].left, d - 1, val);\n return;\n } else {\n if (nodes[node].right == -1) {\n nodes[node].right = idx;\n nodes[idx++] = Node();\n }\n add(nodes[node].right, d - 1, val);\n return;\n }\n }\n\n\n int findMaximumXOR(vector<int>& nums) {\n nodes[idx++] = Node();\n add(0, 31, nums[0]);\n int ans = 0;\n for (int i = 1; i < nums.size(); i++) {\n int a = nums[i];\n ans = max(ans, query(0, 31, a, 0));\n add(0, 31, a);\n }\n return ans;\n }\n};",
"memory": "126126"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n vector<pair<int, int>> nodes(2);\n int root = 1;\n int blen = std::snprintf(nullptr, 0, \"%b\", *max_element(nums.begin(), nums.end()));\n auto get_child = [&](int node, bool left) {\n if (left) {\n if (!nodes[node].first) { nodes[node].first = nodes.size(); nodes.push_back({0, 0}); }\n return nodes[node].first;\n }\n if (!nodes[node].second) { nodes[node].second = nodes.size(); nodes.push_back({0, 0}); }\n return nodes[node].second;\n };\n for (int x : nums) {\n int node = root;\n for (int i = blen - 1; i >= 0; --i) {\n node = get_child(node, (x & (1<<i)) == 0);\n }\n }\n int res = 0;\n for (int x : nums) {\n int mask = 0, node = root;\n for (int i = blen - 1; i >= 0; --i) {\n if (x & (1<<i)) {\n if (nodes[node].first) {\n node = nodes[node].first;\n mask |= 1<<i;\n } else {\n node = nodes[node].second;\n }\n } else {\n if (nodes[node].second) {\n node = nodes[node].second;\n mask |= 1<<i;\n } else {\n node = nodes[node].first;\n }\n }\n }\n res = max(res, mask);\n }\n return res;\n }\n};\n",
"memory": "131083"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n vector<pair<int, int>> nodes(2);\n int root = 1;\n int blen = std::snprintf(nullptr, 0, \"%b\", *max_element(nums.begin(), nums.end()));\n cout << \"blen = \" << blen << endl;\n auto get_child = [&](int node, bool left) {\n if (left) {\n if (!nodes[node].first) { nodes[node].first = nodes.size(); nodes.push_back({0, 0}); }\n return nodes[node].first;\n }\n if (!nodes[node].second) { nodes[node].second = nodes.size(); nodes.push_back({0, 0}); }\n return nodes[node].second;\n };\n for (int x : nums) {\n int node = root;\n for (int i = blen - 1; i >= 0; --i) {\n node = get_child(node, (x & (1<<i)) == 0);\n }\n }\n return search(nodes, root, root, 0);\n }\n\n int search(vector<pair<int, int>>& nodes, int left, int right, int mask) {\n if (!left || !right) return 0;\n if (nodes[left].first && nodes[right].second || nodes[left].second && nodes[right].first) {\n return max(search(nodes, nodes[left].first, nodes[right].second, (mask << 1) | 1), \n search(nodes, nodes[left].second, nodes[right].first, (mask << 1) | 1)); \n }\n if (nodes[left].first || nodes[left].second || nodes[right].first || nodes[right].second) {\n return max(search(nodes, nodes[left].first, nodes[right].first, mask << 1), \n search(nodes, nodes[left].second, nodes[right].second, mask << 1));\n }\n return mask;\n }\n};",
"memory": "136041"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n int blen = std::snprintf(nullptr, 0, \"%b\", *max_element(nums.begin(), nums.end()));\n vector<pair<int, int>> nodes(blen * nums.size() + 1);\n int root = 1, cnt = 1;\n auto get_child = [&](int node, bool left) {\n if (left) {\n if (!nodes[node].first) nodes[node].first = ++cnt;\n return nodes[node].first;\n }\n if (!nodes[node].second) nodes[node].second = ++cnt;\n return nodes[node].second;\n };\n for (int x : nums) {\n int node = root;\n for (int i = blen - 1; i >= 0; --i) {\n node = get_child(node, (x & (1<<i)) == 0);\n }\n }\n int res = 0;\n for (int x : nums) {\n int mask = 0, node = root;\n for (int i = blen - 1; i >= 0; --i) {\n if (x & (1<<i)) {\n if (nodes[node].first) {\n node = nodes[node].first;\n mask |= 1<<i;\n } else {\n node = nodes[node].second;\n }\n } else {\n if (nodes[node].second) {\n node = nodes[node].second;\n mask |= 1<<i;\n } else {\n node = nodes[node].first;\n }\n }\n }\n res = max(res, mask);\n }\n return res;\n }\n};\n",
"memory": "140998"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n int blen = std::snprintf(nullptr, 0, \"%b\", *max_element(nums.begin(), nums.end()));\n vector<pair<int, int>> nodes(blen * nums.size() + 2);\n int root = 1, cnt = 1;\n auto get_child = [&](int node, bool left) {\n if (left) {\n if (!nodes[node].first) nodes[node].first = ++cnt;\n return nodes[node].first;\n }\n if (!nodes[node].second) nodes[node].second = ++cnt;\n return nodes[node].second;\n };\n for (int x : nums) {\n int node = root;\n for (int i = blen - 1; i >= 0; --i) {\n node = get_child(node, (x & (1<<i)) == 0);\n }\n }\n return search(nodes, root, root, 0);\n }\n\n int search(vector<pair<int, int>>& nodes, int left, int right, int mask) {\n if (!left || !right) return 0;\n if (nodes[left].first && nodes[right].second || nodes[left].second && nodes[right].first) {\n return max(search(nodes, nodes[left].first, nodes[right].second, (mask << 1) | 1), \n search(nodes, nodes[left].second, nodes[right].first, (mask << 1) | 1)); \n }\n if (nodes[left].first || nodes[left].second || nodes[right].first || nodes[right].second) {\n return max(search(nodes, nodes[left].first, nodes[right].first, mask << 1), \n search(nodes, nodes[left].second, nodes[right].second, mask << 1));\n }\n return mask;\n }\n};\n",
"memory": "140998"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n const static int N=31*2*1e5, M=2;\n int trie[N][M]; int nexti=1;\n\n void build(string& s1){\n int curr=0;\n for(int i=0; i<s1.size(); i++){\n if(trie[curr][s1[i]-'0']==-1) curr=trie[curr][s1[i]-'0']=nexti++;\n else curr=trie[curr][s1[i]-'0'];\n }\n }\n\n void init(){\n for(int i=0; i<N; i++){\n for(int j=0; j<M; j++){\n trie[i][j]=-1;\n }\n }\n }\n int findMaximumXOR(vector<int>& nums) {\n init();\n int n=nums.size();\n\n vector<string> v1(n);\n for(int i=0; i<n; i++){\n bitset<32> b1(nums[i]);\n v1[i]=b1.to_string();\n build(v1[i]);\n }\n\n int ans=0;\n for(int i=0; i<n; i++){\n int curr=0;\n int ansi=0;\n for(int j=0; j<v1[i].size(); j++){\n if(trie[curr][((v1[i][j]-'0')+1)%2]!=-1){\n ansi+=(1ll<<(31-j));\n curr=trie[curr][((v1[i][j]-'0')+1)%2];\n }\n else{\n curr=trie[curr][v1[i][j]-'0'];\n }\n }\n ans=max(ans, ansi);\n }\n return ans;\n }\n};",
"memory": "145956"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class TrieNode {\npublic:\n TrieNode* children[2];\n TrieNode() {\n children[0] = nullptr;\n children[1] = nullptr;\n }\n};\n\nclass Solution {\nprivate:\n TrieNode* root;\n \n void insert(int num) {\n TrieNode* node = root;\n for (int i = 31; i >= 0; i--) {\n int bit = (num >> i) & 1;\n if (!node->children[bit]) {\n node->children[bit] = new TrieNode();\n }\n node = node->children[bit];\n }\n }\n \n int findMaxXOR(int num) {\n TrieNode* node = root;\n int maxXor = 0;\n for (int i = 31; i >= 0; i--) {\n int bit = (num >> i) & 1;\n if (node->children[1 - bit]) {\n maxXor |= (1 << i);\n node = node->children[1 - bit];\n } else if (node->children[bit]) {\n node = node->children[bit];\n } else {\n break;\n }\n }\n return maxXor;\n }\n\npublic:\n Solution(){\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n }\n int findMaximumXOR(vector<int>& nums) {\n root = new TrieNode();\n int maxXor = 0;\n \n for (int num : nums) {\n insert(num);\n maxXor = max(maxXor, findMaxXOR(num));\n }\n \n return maxXor;\n }\n};",
"memory": "150913"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Node{\n public : \n Node* link[2];\n Node() = default;\n void set(int x,Node* n){\n link[x] = n;\n }\n\n Node* next(int x){\n return link[x];\n }\n\n bool exists(int x){\n return link[x]!=NULL;\n }\n};\n\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n ios_base::sync_with_stdio(NULL),cin.tie(0),cout.tie(0);\n Node* root = new Node();\n for(int i=0;i<nums.size();i++){\n int a = nums[i];\n Node* node =root;\n for(int j=31;j>=0;j--){\n int x = a&(1<<j)?1:0;\n if(!node->exists(x)){\n node->set(x,new Node());\n }\n node = node->next(x);\n }\n }\n\n int maxi = 0;\n for(int i=0;i<nums.size();i++){\n int num = 0;\n int a = nums[i];\n Node* node = root;\n for(int j=31;j>=0;j--){\n int x = a&(1<<j)?1:0;\n if(node->exists(!x)){\n num+= (1<<j);\n x=!x;\n }\n node= node->next(x);\n }\n maxi = max(maxi,num);\n }\n\n\n return maxi;\n }\n};",
"memory": "155871"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "#pragma GCC optmize (\"03\",\"unroll-loops\")\nstruct node\n{\n node * links[2];\n bool containskey(int x)\n {\n return links[x]!=nullptr;\n }\n void put(int x)\n {\n links[x] = new node();\n }\n node * get(int x)\n {\n return links[x];\n }\n};\nclass Trie\n{\n node *root;\n public:\n Trie()\n {\n root = new node();\n }\n void insert(int x)\n {\n node * temp = root;\n int bit;\n for(int i=31;i>=0;i--)\n {\n bit = (x>>i) & 1;\n if(!temp->containskey(bit))\n temp->put(bit);\n temp = temp->get(bit);\n }\n }\n int f(int x)\n {\n node * temp = root;\n \n int max_xor = 0;\n int bit;\n for(int i=31;i>=0;i--)\n {\n bit = x>>i & 1;\n if(temp->containskey(1-bit))\n {\n max_xor = max_xor | (1<<i);\n temp = temp->get(1-bit);\n }\n /*else\n max_xor = max_xor>>i | (bit);*/\n else temp = temp->get(bit);\n }\n return max_xor;\n }\n};\nclass Solution {\npublic:\n /*int findMaximumXOR(vector<int>& nums) \n {\n int maxi = -1e9;\n int n = nums.size();\n for(int i=0;i<n;i++)\n {\n for(int j = i;j<n;j++)\n {\n maxi = max(maxi,(nums[i]^nums[j]));\n }\n }\n return maxi; \n }*/\n int findMaximumXOR(vector<int>& nums)\n {\n ios_base::sync_with_stdio(false);\n Trie trie;\n //int maxi = -1e9;\n int n = nums.size();\n for(int i=0;i<n;i++)\n trie.insert(nums[i]);\n int maxi = 0;\n for(int i=0;i<n-1;i++)\n {\n maxi = max(maxi,trie.f(nums[i]));\n }\n return maxi;\n }\n \n};\n",
"memory": "160828"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "#pragma GCC optmize (\"03\",\"unroll-loops\")\nstruct node\n{\n node * links[2];\n bool containskey(int x)\n {\n return links[x]!=nullptr;\n }\n void put(int x)\n {\n links[x] = new node();\n }\n node * get(int x)\n {\n return links[x];\n }\n};\nclass Trie\n{\n node *root;\n public:\n Trie()\n {\n root = new node();\n }\n void insert(int x)\n {\n node * temp = root;\n int bit;\n for(int i=31;i>=0;i--)\n {\n bit = (x>>i) & 1;\n if(!temp->containskey(bit))\n temp->put(bit);\n temp = temp->get(bit);\n }\n }\n int f(int x)\n {\n node * temp = root;\n \n int max_xor = 0;\n int bit;\n for(int i=31;i>=0;i--)\n {\n bit = x>>i & 1;\n if(temp->containskey(1-bit))\n {\n max_xor = max_xor | (1<<i);\n temp = temp->get(1-bit);\n }\n /*else\n max_xor = max_xor>>i | (bit);*/\n else temp = temp->get(bit);\n }\n return max_xor;\n }\n};\nclass Solution {\npublic:\n /*int findMaximumXOR(vector<int>& nums) \n {\n int maxi = -1e9;\n int n = nums.size();\n for(int i=0;i<n;i++)\n {\n for(int j = i;j<n;j++)\n {\n maxi = max(maxi,(nums[i]^nums[j]));\n }\n }\n return maxi; \n }*/\n int findMaximumXOR(vector<int>& nums)\n {\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n Trie trie;\n //int maxi = -1e9;\n int n = nums.size();\n for(int i=0;i<n;i++)\n trie.insert(nums[i]);\n int maxi = 0;\n for(int i=0;i<n-1;i++)\n {\n maxi = max(maxi,trie.f(nums[i]));\n }\n return maxi;\n }\n \n};\n",
"memory": "160828"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "#pragma GCC optimize(\"Ofast\")\n#pragma GCC taget(\"avx,avx2,fma\")\nstatic const int _ = [] { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); return 0; }();\n\nclass TrieNode{\n public:\n TrieNode* arr[2];\n TrieNode(){\n for(int i=0;i<2;i++){\n arr[i]=nullptr;\n }\n }\n bool isContains(int n){\n return arr[n]!=nullptr;\n }\n};\nclass Trie{\n public:\n TrieNode* root;\n Trie(){\n root=new TrieNode();\n }\n void insert(int n){\n TrieNode* temp=root;\n for(int i=31;i>=0;i--){\n int bit=(n>>i) & 1;\n if(temp->isContains(bit)){\n temp=temp->arr[bit];\n }\n else{\n temp->arr[bit]=new TrieNode();\n temp=temp->arr[bit];\n }\n }\n }\n int findTarget(int n){\n TrieNode* temp=root;\n int xorVal=0;\n for(int i=31;i>=0;i--){\n int bit=(n>>i) & 1;\n if(temp->isContains(1-bit)){\n xorVal=xorVal | 1<<i;\n temp=temp->arr[1-bit];\n }\n else{\n temp=temp->arr[bit];\n }\n }\n return xorVal;\n }\n};\n\n\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n Trie* trie=new Trie();\n for(int num:nums){\n trie->insert(num);\n }\n int ans=0;\n for(int it:nums){\n ans=max(ans,trie->findTarget(it));\n }\n return ans;\n }\n};",
"memory": "165786"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "#pragma GCC optimize(\"Ofast\")\n#pragma GCC taget(\"avx,avx2,fma\")\nstatic const int _ = [] { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); return 0; }();\n\nclass TrieNode{\n public:\n TrieNode* arr[2];\n TrieNode(){\n for(int i=0;i<2;i++){\n arr[i]=nullptr;\n }\n }\n bool isContains(int n){\n return arr[n]!=nullptr;\n }\n};\nclass Trie{\n public:\n TrieNode* root;\n Trie(){\n root=new TrieNode();\n }\n void insert(int n){\n TrieNode* temp=root;\n for(int i=31;i>=0;i--){\n int bit=(n>>i) & 1;\n if(temp->isContains(bit)){\n temp=temp->arr[bit];\n }\n else{\n temp->arr[bit]=new TrieNode();\n temp=temp->arr[bit];\n }\n }\n }\n int findTarget(int n){\n TrieNode* temp=root;\n int xorVal=0;\n for(int i=31;i>=0;i--){\n int bit=(n>>i) & 1;\n if(temp->isContains(1-bit)){\n xorVal=xorVal | 1<<i;\n temp=temp->arr[1-bit];\n }\n else{\n temp=temp->arr[bit];\n }\n }\n return xorVal;\n }\n};\n\n\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n Trie* trie=new Trie();\n for(int num:nums){\n trie->insert(num);\n }\n int ans=0;\n for(int it:nums){\n ans=max(ans,trie->findTarget(it));\n }\n return ans;\n }\n};",
"memory": "165786"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "struct Node{\n Node* links[2];\n bool containsKey(int bit){\n return (links[bit]!=NULL);\n }\n Node* get(int bit){\n return links[bit];\n }\n void put(int bit,Node* node){\n links[bit]=node;\n }\n};\n\nclass Trie{\n private:\n Node* root;\n public:\n Trie(){\n root=new Node();\n }\n void insert(int num){\n Node* node=root;\n for(int i=31;i>=0;i--){\n int bit=(num>>i)&1;\n if(!node->containsKey(bit)){\n node->put(bit,new Node());\n }\n node=node->get(bit);\n }\n }\n int getMax(int num){\n int maxSum=0;\n Node* node=root;\n for(int i=31;i>=0;i--){\n int bit=(num>>i)&1;\n if(node->containsKey(1-bit)){\n maxSum|=(1<<i);\n node=node->get(1-bit);\n }\n else node=node->get(bit);\n }\n return maxSum;\n }\n};\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n Trie trie;\n for(auto &it:nums) trie.insert(it);\n int maxi=0;\n for(auto &it:nums) maxi=max(maxi,trie.getMax(it));\n return maxi;\n }\n};\nauto init=[](){\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();",
"memory": "170743"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "// class TrieNode {\n// public:\n// TrieNode* bits[2];\n// int number;\n// TrieNode(){\n// this->bits[0]=NULL;\n// this->bits[1]=NULL;\n// }\n// };\n\n// class Solution {\n// private: \n// TrieNode* node = new TrieNode();\n// public:\n// void insert(int v){\n// long long num=v;\n// TrieNode* root = node;\n// long long x=(1<<31);\n// for(int i=0 ; i<32 ; i++){\n// TrieNode* newNode = new TrieNode();\n// if((num&x)==0){\n// num=num<<1;\n// if(root->bits[0]!=NULL) continue;\n// root->bits[0]=newNode;\n// }\n// else{\n// num=num<<1;\n// if(root->bits[1]!=NULL) continue;\n// root->bits[1]=newNode;\n// }\n// root=newNode;\n// }\n// root->number = v;\n// }\n \n// int findMaxXor(int v){\n// TrieNode* root = node;\n// long long num=v;\n// long long x=(1<<31);\n// // int xorResult = 0;\n// for (int i = 0; i < 32; i++) {\n// int bit = (num & x) ? 1 : 0;\n// if (root->bits[1 - bit] != NULL) {\n// // xorResult = (xorResult << 1) | 1;\n// root = root->bits[1 - bit];\n// } else {\n// // xorResult = (xorResult << 1);\n// root = root->bits[bit];\n// }\n// x >>= 1;\n// }\n// return root->number;\n// }\n \n// int findMaximumXOR(vector<int>& nums) {\n// for(int i=0 ; i<nums.size() ; i++){\n// insert(nums[i]);\n// }\n// int mx=0;\n// for(int i=0 ; i<nums.size() ; i++){\n// int num = findMaxXor(nums[i]);\n// mx=max(mx,(num*nums[i]));\n// }\n// return mx;\n// }\n// };\n\n\nclass TrieNode {\npublic:\n TrieNode* bits[2];\n TrieNode() {\n this->bits[0] = NULL;\n this->bits[1] = NULL;\n }\n};\n\nclass Solution {\nprivate:\n TrieNode* node = new TrieNode();\npublic:\n void insert(int num) {\n TrieNode* root = node;\n unsigned int mask = 1 << 31; // Use unsigned to avoid overflow\n for (int i = 0; i < 32; i++) {\n int bit = (num & mask) ? 1 : 0;\n if (root->bits[bit] == NULL) {\n root->bits[bit] = new TrieNode();\n }\n root = root->bits[bit];\n mask >>= 1;\n }\n }\n \n int findMaxXor(int num) {\n TrieNode* root = node;\n unsigned int mask = 1 << 31; // Use unsigned to avoid overflow\n int xorResult = 0;\n for (int i = 0; i < 32; i++) {\n int bit = (num & mask) ? 1 : 0;\n if (root->bits[1 - bit] != NULL) {\n xorResult = (xorResult << 1) | 1;\n root = root->bits[1 - bit];\n } else {\n xorResult = (xorResult << 1);\n root = root->bits[bit];\n }\n mask >>= 1;\n }\n return xorResult;\n }\n \n int findMaximumXOR(vector<int>& nums) {\n for (int num : nums) {\n insert(num);\n }\n int mx = 0;\n for (int num : nums) {\n int xorValue = findMaxXor(num);\n mx = max(mx, xorValue);\n }\n return mx;\n }\n};\n",
"memory": "175701"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "#include <vector>\nusing namespace std;\n\nclass Solution {\npublic:\n struct trieNode {\n trieNode* left = nullptr;\n trieNode* right = nullptr;\n };\n\n void insert(trieNode* root, int num) {\n trieNode* pCrawl = root;\n for (int i = 31; i >= 0; i--) {\n int ithbit = (num >> i) & 1;\n if (ithbit == 0) {\n if (pCrawl->left == nullptr) {\n pCrawl->left = new trieNode();\n }\n pCrawl = pCrawl->left;\n } else {\n if (pCrawl->right == nullptr) {\n pCrawl->right = new trieNode();\n }\n pCrawl = pCrawl->right;\n }\n }\n }\n\n int findMaxXor(trieNode* root, int num) {\n int maxXor = 0;\n trieNode* pCrawl = root;\n\n for (int i = 31; i >= 0; i--) {\n int ithbit = (num >> i) & 1;\n if (ithbit == 1) {\n if (pCrawl->left != nullptr) {\n maxXor += (1 << i);\n pCrawl = pCrawl->left;\n } else {\n pCrawl = pCrawl->right;\n }\n } else {\n if (pCrawl->right != nullptr) {\n maxXor += (1 << i);\n pCrawl = pCrawl->right;\n } else {\n pCrawl = pCrawl->left;\n }\n }\n }\n return maxXor;\n }\n\n int findMaximumXOR(vector<int>& nums) {\n trieNode* root = new trieNode();\n for (int num : nums) {\n insert(root, num);\n }\n\n int rs = 0;\n for (int num : nums) {\n int temp = findMaxXor(root, num);\n rs = max(rs, temp);\n }\n\n return rs;\n }\n};\n",
"memory": "180658"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Node{\n public:\n Node* left;\n Node* right;\n Node(){\n this->left=this->right=NULL;\n }\n};\n\nclass Solution {\npublic:\n int maxXorPair(Node* head, int value){\n int curr_xor=0;\n Node* curr=head;\n for(int i=31;i>=0;i--){\n int bit =(value>>i)&1;\n if(bit==0){\n if(curr->right!=NULL){\n curr=curr->right;\n curr_xor+=(1<<i);\n }\n else{\n curr=curr->left;\n }\n\n }\n else{\n if(curr->left!=NULL){\n curr=curr->left;\n curr_xor+=(1<<i);\n }\n else{\n curr=curr->right;\n }\n }\n }\n return curr_xor;\n }\n void insert(Node* head,int value){\n Node* curr=head;\n for(int i=31;i>=0;i--){\n int bit=(value>>i)&1;\n if(bit==0){\n if(curr->left==NULL){\n curr->left=new Node();\n }\n curr=curr->left;\n }\n else{\n if(curr->right==NULL){\n curr->right=new Node();\n }\n curr=curr->right;\n }\n }\n }\n\n int findMaximumXOR(vector<int>& nums) {\n Node* head=new Node();\n insert(head,nums[0]);\n int ans=0;\n for(int i=1;i<nums.size();i++){\n int res=maxXorPair(head,nums[i]);\n ans=max(ans,res);\n insert(head,nums[i]);\n }\n return ans;\n }\n};",
"memory": "180658"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n struct trie{\n trie *next[2];\n };\n trie* getNode(){\n trie *curr= new trie();\n curr->next[0]=NULL;\n curr->next[1]=NULL;\nreturn curr;\n }\n void insert(trie *&root,int num){\n int pos=0;\n int arr[32]={0};\n while(num){\n arr[32-pos-1]=num&1;\n num>>=1;\n pos++;\n }\n trie *curr=root;\n for(int i=0;i<32;i++){\n int x=arr[i];\n if(curr->next[x])\n curr=curr->next[x];\n else{\n curr->next[x]=getNode();\n curr=curr->next[x];\n }\n }\n }\n void solve(trie* &root,int num,int &ans){\n int val=0;\n trie* curr=root;\n int pos=0;\n int arr[32]={0};\n while(num){\n arr[32-pos-1]=num&1;\n num>>=1;\n pos++;\n }\n for(int i=0;i<32;i++){\n int x=arr[i];\n int y=x^1;\n if(curr->next[y]){\n curr=curr->next[y];\n val |= 1<<(32-i-1);\n }\n else{\n curr=curr->next[x];\n }\n }\n ans=max(ans,val);\n }\n int findMaximumXOR(vector<int>& nums) {\n trie *root=getNode();\n int ans=0;\n for(int i=0;i<nums.size();i++){\n if(i>0)\n solve(root,nums[i],ans);\n insert(root,nums[i]);\n }\n return ans;\n }\n};",
"memory": "185616"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\nint ans=0;\n struct trie{\n trie *next[2];\n };\n trie* getNode(){\n trie *curr= new trie();\n curr->next[0]=NULL;\n curr->next[1]=NULL;\nreturn curr;\n }\n void insert(trie *&root,int num){\n int pos=0;\n int arr[32]={0};\n while(num){\n arr[32-pos-1]=num&1;\n num>>=1;\n pos++;\n }\n trie *curr=root;\n for(int i=0;i<32;i++){\n int x=arr[i];\n if(curr->next[x])\n curr=curr->next[x];\n else{\n curr->next[x]=getNode();\n curr=curr->next[x];\n }\n }\n }\n void solve(trie* &root,int num){\n int val=0;\n trie* curr=root;\n int pos=0;\n int arr[32]={0};\n while(num){\n arr[32-pos-1]=num&1;\n num>>=1;\n pos++;\n }\n for(int i=0;i<32;i++){\n int x=arr[i];\n int y=x^1;\n if(curr->next[y]){\n curr=curr->next[y];\n val |= 1<<(32-i-1);\n }\n else{\n curr=curr->next[x];\n }\n }\n ans=max(ans,val);\n }\n int findMaximumXOR(vector<int>& nums) {\n ans=0;\n trie *root=getNode();\n // int ans=0;\n for(int i=0;i<nums.size();i++){\n if(i>0)\n solve(root,nums[i]);\n insert(root,nums[i]);\n }\n return ans;\n }\n};",
"memory": "185616"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 2 | {
"code": " \n#include <iostream>\n#include <vector>\n\nusing namespace std;\n\n// Node structure \n// for the Trie\nstruct Node {\n // Array to store links\n // to child nodes (0 and 1)\n Node* links[2]; \n \n // Method to check if a specific\n // bit key is present in the child nodes\n bool containsKey(int bit) {\n \n // Returns true if the link at\n // index 'bit' is not NULL\n return (links[bit] != NULL); \n }\n \n // Method to get the child node\n // corresponding to a specific bit\n Node* get(int bit) {\n \n // Returns the child\n // node at index 'bit'\n return links[bit]; \n }\n \n // Method to set a child node at a\n // specific index in the links array\n void put(int bit, Node* node) {\n \n // Sets the child node at index\n // 'bit' to the provided node\n links[bit] = node; \n }\n};\n\n// Trie class\nclass Trie {\nprivate:\n // Root node of the Trie\n Node* root;\npublic:\n // Constructor to initialize\n // the Trie with a root node\n Trie() {\n // Creates a new root\n // node for the Trie\n root = new Node();\n }\n \n // Method to insert a number into the Trie\n void insert(int num) {\n // Start from the root node\n Node* node = root; \n // Iterate through each bit of the\n // number (from left to right)\n for (int i = 31; i >= 0; i--) { \n // Extract the i-th bit of the number\n int bit = (num >> i) & 1; \n \n // If the current node doesn't have a\n // child node with the current bit\n if (!node->containsKey(bit)) { \n \n // Create a new child node\n // with the current bit\n node->put(bit, new Node()); \n }\n \n // Move to the child node\n // corresponding to the current bit\n node = node->get(bit); \n }\n }\n \n // Method to find the maximum\n // XOR value for a given number\n int getMax(int num) {\n // Start from the root node\n Node* node = root;\n \n // Initialize the maximum XOR value\n int maxNum = 0; \n \n // Iterate through each bit of\n // the number (from left to right)\n for (int i = 31; i >= 0; i--) { \n \n // Extract the i-th\n // bit of the number\n int bit = (num >> i) & 1; \n \n // If the complement of the current\n // bit exists in the Trie\n if (node->containsKey(1 - bit)) { \n \n // Update the maximum XOR\n // value with the current bit\n maxNum |= (1 << i); \n \n // Move to the child node corresponding\n // to the complement of the current bit\n node = node->get(1 - bit);\n } else {\n \n // Move to the child node\n // corresponding to the current bit\n node = node->get(bit);\n }\n }\n \n // Return the maximum XOR value\n return maxNum; \n }\n};\n\n\n\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n int n = nums.size();\n vector<int> arr1,arr2;\n for(auto it: nums)\n {\n arr1.push_back(it);\n arr2.push_back(it);\n }\n // Create a Trie object\n Trie trie; \n // Insert each number from\n // the first set into the Trie\n for (auto& it : arr1) { \n trie.insert(it);\n }\n \n // Initialize the maximum XOR value\n int maxi = 0; \n \n // Iterate through each\n // number in the second set\n for (auto& it : arr2) { \n // Update the maximum XOR value\n // with the result from the Trie\n maxi = max(maxi, trie.getMax(it));\n }\n // Return the\n // maximum XOR value\n return maxi;\n }\n};",
"memory": "190573"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 2 | {
"code": " \n#include <iostream>\n#include <vector>\n\nusing namespace std;\n\n// Node structure \n// for the Trie\nstruct Node {\n // Array to store links\n // to child nodes (0 and 1)\n Node* links[2]; \n \n // Method to check if a specific\n // bit key is present in the child nodes\n bool containsKey(int bit) {\n \n // Returns true if the link at\n // index 'bit' is not NULL\n return (links[bit] != NULL); \n }\n \n // Method to get the child node\n // corresponding to a specific bit\n Node* get(int bit) {\n \n // Returns the child\n // node at index 'bit'\n return links[bit]; \n }\n \n // Method to set a child node at a\n // specific index in the links array\n void put(int bit, Node* node) {\n \n // Sets the child node at index\n // 'bit' to the provided node\n links[bit] = node; \n }\n};\n\n// Trie class\nclass Trie {\nprivate:\n // Root node of the Trie\n Node* root;\npublic:\n // Constructor to initialize\n // the Trie with a root node\n Trie() {\n // Creates a new root\n // node for the Trie\n root = new Node();\n }\n \n // Method to insert a number into the Trie\n void insert(int num) {\n // Start from the root node\n Node* node = root; \n // Iterate through each bit of the\n // number (from left to right)\n for (int i = 31; i >= 0; i--) { \n // Extract the i-th bit of the number\n int bit = (num >> i) & 1; \n \n // If the current node doesn't have a\n // child node with the current bit\n if (!node->containsKey(bit)) { \n \n // Create a new child node\n // with the current bit\n node->put(bit, new Node()); \n }\n \n // Move to the child node\n // corresponding to the current bit\n node = node->get(bit); \n }\n }\n \n // Method to find the maximum\n // XOR value for a given number\n int getMax(int num) {\n // Start from the root node\n Node* node = root;\n \n // Initialize the maximum XOR value\n int maxNum = 0; \n \n // Iterate through each bit of\n // the number (from left to right)\n for (int i = 31; i >= 0; i--) { \n \n // Extract the i-th\n // bit of the number\n int bit = (num >> i) & 1; \n \n // If the complement of the current\n // bit exists in the Trie\n if (node->containsKey(1 - bit)) { \n \n // Update the maximum XOR\n // value with the current bit\n maxNum |= (1 << i); \n \n // Move to the child node corresponding\n // to the complement of the current bit\n node = node->get(1 - bit);\n } else {\n \n // Move to the child node\n // corresponding to the current bit\n node = node->get(bit);\n }\n }\n \n // Return the maximum XOR value\n return maxNum; \n }\n};\n\n\n\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n int n = nums.size();\n vector<int> arr1,arr2;\n for(auto it: nums)\n {\n arr1.push_back(it);\n arr2.push_back(it);\n }\n // Create a Trie object\n Trie trie; \n // Insert each number from\n // the first set into the Trie\n for (auto& it : arr1) { \n trie.insert(it);\n }\n \n // Initialize the maximum XOR value\n int maxi = 0; \n \n // Iterate through each\n // number in the second set\n for (auto& it : arr2) { \n // Update the maximum XOR value\n // with the result from the Trie\n maxi = max(maxi, trie.getMax(it));\n }\n // Return the\n // maximum XOR value\n return maxi;\n }\n};",
"memory": "195531"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\nprivate:\n class Node {\n public:\n Node() {\n children[0] = nullptr;\n children[1] = nullptr;\n }\n Node* children[2];\n };\npublic:\n int findMaximumXOR(vector<int>& nums) {\n Node * root = new Node();\n int largest = 0;\n for (int num : nums) {\n if (num > largest) {\n largest = num;\n }\n }\n int scale = 0;\n while (largest > 0) {\n scale += 1;\n largest >>= 1;\n }\n if (scale < 1) {\n return 0;\n }\n for (int num : nums) {\n int mask = 1 << (scale -1);\n Node* p = root;\n while (mask > 0) {\n int bit = (mask & num) > 0 ? 1 : 0;\n if (p->children[bit] == nullptr) {\n Node* r = new Node();\n p->children[bit] = r;\n p = r;\n } else {\n p = p->children[bit];\n }\n mask >>= 1;\n }\n }\n int result = -1;\n for (int num : nums) {\n int mask = 1 << (scale -1);\n Node* p = root;\n int comp = 0;\n while (mask > 0) {\n int bit = (mask & num) > 0 ? 0 : 1;\n if (p->children[bit] == nullptr) {\n bit = 1-bit;\n }\n comp |= bit * mask;\n p = p->children[bit];\n mask >>= 1;\n }\n if ((num^comp) > result) {\n result = num ^ comp;\n }\n }\n std::stack<Node*> cleanup;\n cleanup.push(root);\n while (!cleanup.empty()) {\n auto* node = cleanup.top();\n cleanup.pop();\n if (node->children[0] != nullptr) {\n cleanup.push(node->children[0]);\n node->children[0] = nullptr;\n }\n if (node->children[1] != nullptr) {\n cleanup.push(node->children[1]);\n node->children[1] = nullptr;\n }\n delete node;\n }\n return result;\n }\n};",
"memory": "195531"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 2 | {
"code": "static const auto Initialize = []{\n ios::sync_with_stdio(false); cin.tie(nullptr);\n return nullptr;\n}();\n\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n int maximum = 0, nowMask = 0;\n unordered_set <int> prefixes;\n for (unsigned i = (1 << 31); i; i >>= 1) {\n nowMask |= i;\n prefixes.clear();\n for (int j : nums)\n prefixes.insert(j & nowMask);\n for (int j : prefixes)\n if (prefixes.count((maximum ^ j) ^ i)) {\n maximum |= i;\n break;\n }\n }\n return maximum;\n }\n};",
"memory": "200488"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 2 | {
"code": "// class TrieNode {\n// public:\n// TrieNode(): m_children(vector<TrieNode*>(2, nullptr)){}\n// // std::array<unique_ptr<TrieNode>, 2> m_children;\n// vector<TrieNode*> m_children;\n// };\n\nclass Solution {\npublic:\n// int findMaximumXOR(vector<int>& nums) {\n// int maxElem = *max_element(nums.begin(), nums.end());\n\n// if (maxElem == 0)\n// return 0;\n\n// m_maxBits = std::floor(std::log2(maxElem)) + 1;\n\n// int maxXor = 0;\n\n// for (int num: nums)\n// maxXor = std::max(maxXor, insert(num));\n\n// return maxXor;\n// }\n\n// private:\n// int insert(int num) {\n// auto* node = m_root.get();\n// auto* xorNode = m_root.get();\n// int currXor = 0;\n\n// for (int i = m_maxBits - 1; i >= 0; --i) {\n// int bit = 1 & num >> i;\n// int toggledBit = bit ^ 1;\n\n// if (!node->m_children[bit])\n// node->m_children[bit] = new TrieNode();\n// node = node->m_children[bit];\n\n// if (xorNode->m_children[toggledBit]) {\n// xorNode = xorNode->m_children[toggledBit];\n// currXor |= 1 << i;\n// } else {\n// xorNode = xorNode->m_children[bit];\n// }\n// }\n\n// return currXor;\n// }\n\n// unique_ptr<TrieNode> m_root = make_unique<TrieNode>();\n// int m_maxBits = 0;\n\n\n\n\n int findMaximumXOR(vector<int>& nums) {\n int maxElem = *max_element(nums.begin(), nums.end());\n\n if (maxElem == 0)\n return 0;\n\n int bits = floor(log2(maxElem)) + 1;\n\n int n = nums.size(), maxXOR = 0, curr;\n\n unordered_set<int> prefixes;\n\n for (int bit = bits - 1; bit >= 0; --bit) {\n maxXOR <<= 1;\n curr = maxXOR | 1;\n\n prefixes.clear();\n\n for (int num: nums)\n prefixes.insert(num >> bit);\n\n for (int num: prefixes) {\n if (prefixes.count(curr ^ num)) {\n maxXOR = curr;\n break;\n }\n }\n }\n\n return maxXOR;\n }\n};\n\nauto init = []() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n} ();",
"memory": "205446"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 2 | {
"code": "// class TrieNode {\n// public:\n// TrieNode(): m_children(vector<TrieNode*>(2, nullptr)){}\n// // std::array<unique_ptr<TrieNode>, 2> m_children;\n// vector<TrieNode*> m_children;\n// };\n\nclass Solution {\npublic:\n// int findMaximumXOR(vector<int>& nums) {\n// int maxElem = *max_element(nums.begin(), nums.end());\n\n// if (maxElem == 0)\n// return 0;\n\n// m_maxBits = std::floor(std::log2(maxElem)) + 1;\n\n// int maxXor = 0;\n\n// for (int num: nums)\n// maxXor = std::max(maxXor, insert(num));\n\n// return maxXor;\n// }\n\n// private:\n// int insert(int num) {\n// auto* node = m_root.get();\n// auto* xorNode = m_root.get();\n// int currXor = 0;\n\n// for (int i = m_maxBits - 1; i >= 0; --i) {\n// int bit = 1 & num >> i;\n// int toggledBit = bit ^ 1;\n\n// if (!node->m_children[bit])\n// node->m_children[bit] = new TrieNode();\n// node = node->m_children[bit];\n\n// if (xorNode->m_children[toggledBit]) {\n// xorNode = xorNode->m_children[toggledBit];\n// currXor |= 1 << i;\n// } else {\n// xorNode = xorNode->m_children[bit];\n// }\n// }\n\n// return currXor;\n// }\n\n// unique_ptr<TrieNode> m_root = make_unique<TrieNode>();\n// int m_maxBits = 0;\n\n\n\n\n int findMaximumXOR(vector<int>& nums) {\n int maxElem = *max_element(nums.begin(), nums.end());\n\n if (maxElem == 0)\n return 0;\n\n int bits = floor(log2(maxElem)) + 1;\n\n int n = nums.size(), maxXOR = 0, curr;\n\n unordered_set<int> prefixes;\n\n for (int bit = bits - 1; bit >= 0; --bit) {\n maxXOR <<= 1;\n curr = maxXOR | 1;\n\n prefixes.clear();\n\n for (int num: nums)\n prefixes.insert(num >> bit);\n\n for (int num: prefixes) {\n if (prefixes.count(curr ^ num)) {\n maxXOR = curr;\n break;\n }\n }\n }\n\n return maxXOR;\n }\n};\n\nauto init = []() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n} ();",
"memory": "205446"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n \n // Find the length of the maximum number in binary representation\n int maxNum = *max_element(nums.begin(), nums.end());\n int L = 0;\n while (maxNum > 0) {\n L++;\n maxNum >>= 1;\n }\n\n int maxXor = 0; // This will store the final result\n unordered_set<int> prefixes; \n\n for (int i = L - 1; i >= 0; --i) {\n // Go to the next bit by the left shift\n maxXor <<= 1;\n\n // Set 1 in the smallest bit\n int currXor = maxXor | 1;\n prefixes.clear();\n\n // compute all possible prefixes of length (L - i) in binary representation\n for (int num : nums) {\n prefixes.insert(num >> i);\n }\n\n // Update maxXor, if two of these prefixes could result in currXor.\n // Check if p1^p2 == currXor, i.e. p1 == currXor^p2.\n for (int p : prefixes) {\n if (prefixes.find(currXor ^ p) != prefixes.end()) {\n maxXor = currXor; \n break;\n }\n }\n }\n\n return maxXor;\n }\n};",
"memory": "210403"
} |
421 | <p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n \n int findMaximumXOR(vector<int>& nums) {\n \n int candidate = 0;\n int mask = 0;\n int max_xor = 0;\n\n unordered_set<int> set;\n\n for(int i = 31; i >=0; i--){\n\n mask = mask | (1<<i);\n\n for(int i = 0; i < nums.size(); i++){\n set.insert(nums[i] & mask);\n }\n\n candidate = max_xor | (1 << i);\n\n for(auto prefix : set){\n\n if(set.count(prefix^candidate)){\n max_xor = candidate;\n break;\n }\n }\n\n set.clear();\n \n }\n\n return max_xor;\n \n }\n};",
"memory": "215361"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.