id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
167
<p>Given a <strong>1-indexed</strong> array of integers <code>numbers</code> that is already <strong><em>sorted in non-decreasing order</em></strong>, find two numbers such that they add up to a specific <code>target</code> number. Let these two numbers be <code>numbers[index<sub>1</sub>]</code> and <code>numbers[index...
0
{ "code": "class Solution {\npublic:\n vector<int> twoSum(vector<int>& numbers, int target) {\n int n = numbers.size();\n int low = 0;\n int high = n-1;\n vector<int> ans;\n for (int i=0 ; i<n ; i++){\n //int mid = low + (high - low)/2;\n if (numbers[low] + ...
167
<p>Given a <strong>1-indexed</strong> array of integers <code>numbers</code> that is already <strong><em>sorted in non-decreasing order</em></strong>, find two numbers such that they add up to a specific <code>target</code> number. Let these two numbers be <code>numbers[index<sub>1</sub>]</code> and <code>numbers[index...
0
{ "code": "class Solution {\npublic:\n vector<int> twoSum(vector<int>& numbers, int target) {\n int i=0,j=numbers.size()-1;\n int sum;\n while(i<=j) {\n sum=numbers[i]+numbers[j];\n if(sum==target){\n return {i+1,j+1};\n }\n else if(su...
167
<p>Given a <strong>1-indexed</strong> array of integers <code>numbers</code> that is already <strong><em>sorted in non-decreasing order</em></strong>, find two numbers such that they add up to a specific <code>target</code> number. Let these two numbers be <code>numbers[index<sub>1</sub>]</code> and <code>numbers[index...
0
{ "code": "class Solution {\npublic:\n vector<int> twoSum(vector<int>& numbers, int target) {\n int i=0, j=numbers.size()-1;\n vector<int> index;\n while(i < j) {\n if(numbers[i] + numbers[j] == target) {\n index.push_back(i+1);\n index.push_back(j+1);\...
167
<p>Given a <strong>1-indexed</strong> array of integers <code>numbers</code> that is already <strong><em>sorted in non-decreasing order</em></strong>, find two numbers such that they add up to a specific <code>target</code> number. Let these two numbers be <code>numbers[index<sub>1</sub>]</code> and <code>numbers[index...
0
{ "code": "class Solution {\npublic:\n vector<int> twoSum(vector<int>& numbers, int target) {\n int left = 0;\n int right = numbers.size() - 1;\n \n while (left < right) {\n int sum = numbers[left] + numbers[right];\n \n if (sum == target) {\n ...
167
<p>Given a <strong>1-indexed</strong> array of integers <code>numbers</code> that is already <strong><em>sorted in non-decreasing order</em></strong>, find two numbers such that they add up to a specific <code>target</code> number. Let these two numbers be <code>numbers[index<sub>1</sub>]</code> and <code>numbers[index...
0
{ "code": "class Solution {\npublic:\n vector<int> twoSum(vector<int>& numbers, int target) {\n int l = 0;\n int r = numbers.size()-1;\n for (int i = 0; i < numbers.size(); i++)\n {\n if (numbers[l] + numbers[r] == target)\n {\n vector<int> result = ...
167
<p>Given a <strong>1-indexed</strong> array of integers <code>numbers</code> that is already <strong><em>sorted in non-decreasing order</em></strong>, find two numbers such that they add up to a specific <code>target</code> number. Let these two numbers be <code>numbers[index<sub>1</sub>]</code> and <code>numbers[index...
0
{ "code": "class Solution {\npublic:\n vector<int> twoSum(vector<int>& numbers, int target) {\n int i=0;\n int j=numbers.size()-1;\n\n while(i<j){\n int temp = numbers[i]+numbers[j];\n if(temp == target) return {i+1, j+1};\n if(target > temp) i++;\n ...
167
<p>Given a <strong>1-indexed</strong> array of integers <code>numbers</code> that is already <strong><em>sorted in non-decreasing order</em></strong>, find two numbers such that they add up to a specific <code>target</code> number. Let these two numbers be <code>numbers[index<sub>1</sub>]</code> and <code>numbers[index...
0
{ "code": "class Solution {\npublic:\n vector<int> twoSum(vector<int>& numbers, int target) {\n int i=0;\n int j=numbers.size()-1;\n vector<int>temp;\n while(i<j){\n \n if(numbers[i]+numbers[j]==target){\n temp.push_back(i+1);\n temp.p...
167
<p>Given a <strong>1-indexed</strong> array of integers <code>numbers</code> that is already <strong><em>sorted in non-decreasing order</em></strong>, find two numbers such that they add up to a specific <code>target</code> number. Let these two numbers be <code>numbers[index<sub>1</sub>]</code> and <code>numbers[index...
0
{ "code": "class Solution {\npublic:\n vector<int> twoSum(vector<int>& num, int k) {\n vector<int> ans;\n int l=0;\n int r=num.size()-1;\n while(l<r){\n if(num[l] + num[r] > k)\n r--;\n else if(num[l] + num[r] < k)\n l++;\n ...
167
<p>Given a <strong>1-indexed</strong> array of integers <code>numbers</code> that is already <strong><em>sorted in non-decreasing order</em></strong>, find two numbers such that they add up to a specific <code>target</code> number. Let these two numbers be <code>numbers[index<sub>1</sub>]</code> and <code>numbers[index...
0
{ "code": "class Solution {\npublic:\n vector<int> twoSum(vector<int>& numbers, int target) {\n int left = 0;\n int right = numbers.size() - 1;\n\n while (left < right) {\n int total = numbers[left] + numbers[right];\n\n if (total == target) {\n return {l...
167
<p>Given a <strong>1-indexed</strong> array of integers <code>numbers</code> that is already <strong><em>sorted in non-decreasing order</em></strong>, find two numbers such that they add up to a specific <code>target</code> number. Let these two numbers be <code>numbers[index<sub>1</sub>]</code> and <code>numbers[index...
1
{ "code": "class Solution {\npublic:\n vector<int> twoSum(vector<int>& numbers, int target) {\n int n = numbers.size();\n int i =0;\n int k =n-1;\n while(i<k){\n if(numbers[i]+numbers[k]==target){\n return{i+1,k+1};\n }else if(numbers[i]+numbers[k]<t...
167
<p>Given a <strong>1-indexed</strong> array of integers <code>numbers</code> that is already <strong><em>sorted in non-decreasing order</em></strong>, find two numbers such that they add up to a specific <code>target</code> number. Let these two numbers be <code>numbers[index<sub>1</sub>]</code> and <code>numbers[index...
3
{ "code": "\nclass Solution {\npublic:\n vector<int> twoSum(vector<int>& nums, int t) {\n vector<int>v1;\n\n map<int,int>m1;\n\n for(int i=0;i<size(nums);i++){\n\n // if(nums[i]>t) break;\n\n\n if(m1.find(t-nums[i])!=m1.end()){\n \n v1.push_ba...
167
<p>Given a <strong>1-indexed</strong> array of integers <code>numbers</code> that is already <strong><em>sorted in non-decreasing order</em></strong>, find two numbers such that they add up to a specific <code>target</code> number. Let these two numbers be <code>numbers[index<sub>1</sub>]</code> and <code>numbers[index...
3
{ "code": "class Solution {\npublic:\n vector<int> twoSum(vector<int>& numbers, int target) {\n map<int, int>m;\n int n = numbers.size();\n vector<int>v;\n for(int i = 0;i< n;i++)\n {\n if(m[numbers[i]] != 0)\n {\n v = {m[numbers[i...
167
<p>Given a <strong>1-indexed</strong> array of integers <code>numbers</code> that is already <strong><em>sorted in non-decreasing order</em></strong>, find two numbers such that they add up to a specific <code>target</code> number. Let these two numbers be <code>numbers[index<sub>1</sub>]</code> and <code>numbers[index...
3
{ "code": "class Solution {\npublic:\n vector<int> twoSum(vector<int>& numbers, int target) {\n unordered_map<int,int> hp;\n vector<int> ans;\n\n for(int i=0; i<numbers.size(); i++){\n if(hp.count(target - numbers[i])){\n ans.push_back(hp[target - numbers[i]] + 1);\n ...
167
<p>Given a <strong>1-indexed</strong> array of integers <code>numbers</code> that is already <strong><em>sorted in non-decreasing order</em></strong>, find two numbers such that they add up to a specific <code>target</code> number. Let these two numbers be <code>numbers[index<sub>1</sub>]</code> and <code>numbers[index...
3
{ "code": "class Solution {\npublic:\n vector<int> twoSum(vector<int>& numbers, int target) {\n int n = numbers.size();\n unordered_map<int, int> indexing;\n for(int i=0;i<n;i++){\n int rem = target - numbers[i];\n if(indexing.find(rem) != indexing.end()){\n ...
168
<p>Given an integer <code>columnNumber</code>, return <em>its corresponding column title as it appears in an Excel sheet</em>.</p> <p>For example:</p> <pre> A -&gt; 1 B -&gt; 2 C -&gt; 3 ... Z -&gt; 26 AA -&gt; 27 AB -&gt; 28 ... </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong...
0
{ "code": "class Solution {\npublic:\n string convertToTitle(int columnNumber) {\n string ans = \"\";\n while (columnNumber > 0) {\n columnNumber--; \n ans = char('A' + columnNumber % 26) + ans;\n columnNumber /= 26;\n }\n return ans;\n }\n};\n", "m...
168
<p>Given an integer <code>columnNumber</code>, return <em>its corresponding column title as it appears in an Excel sheet</em>.</p> <p>For example:</p> <pre> A -&gt; 1 B -&gt; 2 C -&gt; 3 ... Z -&gt; 26 AA -&gt; 27 AB -&gt; 28 ... </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong...
0
{ "code": "class Solution {\npublic:\n string convertToTitle(int columnNumber) {\n string rev = \"\";\n while (columnNumber) {\n rev += ('A' + ((columnNumber - 1) % 26));\n columnNumber = (columnNumber - 1) / 26;\n }\n reverse(rev.begin(), rev.end());\n retu...
168
<p>Given an integer <code>columnNumber</code>, return <em>its corresponding column title as it appears in an Excel sheet</em>.</p> <p>For example:</p> <pre> A -&gt; 1 B -&gt; 2 C -&gt; 3 ... Z -&gt; 26 AA -&gt; 27 AB -&gt; 28 ... </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong...
0
{ "code": "class Solution {\npublic:\n string convertToTitle(int c) {\n string s = \"\";\n while(c>0){\n c = c - 1;\n char ch = 'A' + c % 26;\n s += ch;\n c /= 26;\n }\n reverse(s.begin(),s.end());\n return s;\n }\n};", "memory": "...
168
<p>Given an integer <code>columnNumber</code>, return <em>its corresponding column title as it appears in an Excel sheet</em>.</p> <p>For example:</p> <pre> A -&gt; 1 B -&gt; 2 C -&gt; 3 ... Z -&gt; 26 AA -&gt; 27 AB -&gt; 28 ... </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong...
0
{ "code": "class Solution {\npublic:\n string convertToTitle(int columnNumber) {\n string col = \"\";\n while (columnNumber-->0) {\n int digit = columnNumber%26;\n col = char(digit+'A')+col;\n columnNumber /= 26;\n }\n return col;\n }\n};", "memory"...
168
<p>Given an integer <code>columnNumber</code>, return <em>its corresponding column title as it appears in an Excel sheet</em>.</p> <p>For example:</p> <pre> A -&gt; 1 B -&gt; 2 C -&gt; 3 ... Z -&gt; 26 AA -&gt; 27 AB -&gt; 28 ... </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong...
0
{ "code": "class Solution {\npublic:\n string convertToTitle(int columnNumber) {\n string ans;\n \n while (columnNumber > 0) {\n columnNumber--; // Decrease column number by 1 to handle 1-based indexing\n ans = char('A' + (columnNumber % 26)) + ans;\n colum...
168
<p>Given an integer <code>columnNumber</code>, return <em>its corresponding column title as it appears in an Excel sheet</em>.</p> <p>For example:</p> <pre> A -&gt; 1 B -&gt; 2 C -&gt; 3 ... Z -&gt; 26 AA -&gt; 27 AB -&gt; 28 ... </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong...
0
{ "code": "class Solution {\npublic:\n string convertToTitle(int cn) {\n string out = \"\";\n while(cn) {\n int temp = cn % 26;\n if (temp != 0) {\n cn/=26;\n char str = 'A' + temp - 1;\n out = str + out;\n } else {\n ...
168
<p>Given an integer <code>columnNumber</code>, return <em>its corresponding column title as it appears in an Excel sheet</em>.</p> <p>For example:</p> <pre> A -&gt; 1 B -&gt; 2 C -&gt; 3 ... Z -&gt; 26 AA -&gt; 27 AB -&gt; 28 ... </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong...
1
{ "code": "class Solution {\npublic:\n string convertToTitle(int columnNumber) {\n string res=\"\";\n while(columnNumber>0)\n {\n columnNumber--;\n res = char(columnNumber % 26 + 'A') + res;\n columnNumber /= 26;\n }\n return res;\n }\n};", "...
168
<p>Given an integer <code>columnNumber</code>, return <em>its corresponding column title as it appears in an Excel sheet</em>.</p> <p>For example:</p> <pre> A -&gt; 1 B -&gt; 2 C -&gt; 3 ... Z -&gt; 26 AA -&gt; 27 AB -&gt; 28 ... </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong...
1
{ "code": "class Solution {\npublic:\n string convertToTitle(int columnNumber) {\n string s;\n while(columnNumber>0){\n if(columnNumber % 26 ==0){\n int digit = 'Z';\n s.push_back(digit);\n columnNumber = columnNumber/26 -1;\n }else{\...
169
<p>Given an array <code>nums</code> of size <code>n</code>, return <em>the majority element</em>.</p> <p>The majority element is the element that appears more than <code>&lfloor;n / 2&rfloor;</code> times. You may assume that the majority element always exists in the array.</p> <p>&nbsp;</p> <p><strong class="example...
0
{ "code": "int init = [] {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);cout.tie(nullptr);\n ofstream out(\"user.out\");\n\n for (string s; getline(cin, s); out << '\\n') {\n int ans = 0;\n int c = 0;\n\n for (int _i = 1, _n = s.length(); _i < _n; ++_i) {\n bool _...
169
<p>Given an array <code>nums</code> of size <code>n</code>, return <em>the majority element</em>.</p> <p>The majority element is the element that appears more than <code>&lfloor;n / 2&rfloor;</code> times. You may assume that the majority element always exists in the array.</p> <p>&nbsp;</p> <p><strong class="example...
0
{ "code": "#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(2)\n#pragma GCC optimize(3)\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"inline\")\n#pragma GCC optimize(\"-fgcse\")\n#pragma GCC optimize(\"-fgcse-lm\")\n#pragma GCC optimize(\"-fipa-sra\")\n#pragma GCC optimize(\"-ftree-pre\")\n#pragma GCC op...
169
<p>Given an array <code>nums</code> of size <code>n</code>, return <em>the majority element</em>.</p> <p>The majority element is the element that appears more than <code>&lfloor;n / 2&rfloor;</code> times. You may assume that the majority element always exists in the array.</p> <p>&nbsp;</p> <p><strong class="example...
0
{ "code": "int init = [] {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);cout.tie(nullptr);\n ofstream out(\"user.out\");\n\n for (string s; getline(cin, s); out << '\\n') {\n int ans = 0;\n int c = 0;\n\n for (int _i = 1, _n = s.length(); _i < _n; ++_i) {\n bool _...
169
<p>Given an array <code>nums</code> of size <code>n</code>, return <em>the majority element</em>.</p> <p>The majority element is the element that appears more than <code>&lfloor;n / 2&rfloor;</code> times. You may assume that the majority element always exists in the array.</p> <p>&nbsp;</p> <p><strong class="example...
0
{ "code": "#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(2)\n#pragma GCC optimize(3)\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"inline\")\n#pragma GCC optimize(\"-fgcse\")\n#pragma GCC optimize(\"-fgcse-lm\")\n#pragma GCC optimize(\"-fipa-sra\")\n#pragma GCC optimize(\"-ftree-pre\")\n#pragma GCC op...
169
<p>Given an array <code>nums</code> of size <code>n</code>, return <em>the majority element</em>.</p> <p>The majority element is the element that appears more than <code>&lfloor;n / 2&rfloor;</code> times. You may assume that the majority element always exists in the array.</p> <p>&nbsp;</p> <p><strong class="example...
0
{ "code": "int init = [] {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);cout.tie(nullptr);\n ofstream out(\"user.out\");\n\n for (string s; getline(cin, s); out << '\\n') {\n int ans = 0;\n int c = 0;\n\n for (int _i = 1, _n = s.length(); _i < _n; ++_i) {\n bool _...
169
<p>Given an array <code>nums</code> of size <code>n</code>, return <em>the majority element</em>.</p> <p>The majority element is the element that appears more than <code>&lfloor;n / 2&rfloor;</code> times. You may assume that the majority element always exists in the array.</p> <p>&nbsp;</p> <p><strong class="example...
0
{ "code": "class Solution {\npublic:\n int majorityElement(vector<int>& nums) {\n int count = 0;\n int candidate = 0;\n \n for (int num : nums) {\n if (count == 0) {\n candidate = num;\n }\n \n if (num == candidate) {\n ...
169
<p>Given an array <code>nums</code> of size <code>n</code>, return <em>the majority element</em>.</p> <p>The majority element is the element that appears more than <code>&lfloor;n / 2&rfloor;</code> times. You may assume that the majority element always exists in the array.</p> <p>&nbsp;</p> <p><strong class="example...
0
{ "code": "class Solution {\npublic:\n int majorityElement(vector<int>& nums) {\n for (int i=0;i<nums.size();i++) {\n int temp = 1; \n for (int j =i+1;j<nums.size();j++) {\n if (nums[i] == nums[j]) {\n temp++;\n }\n }\n ...
169
<p>Given an array <code>nums</code> of size <code>n</code>, return <em>the majority element</em>.</p> <p>The majority element is the element that appears more than <code>&lfloor;n / 2&rfloor;</code> times. You may assume that the majority element always exists in the array.</p> <p>&nbsp;</p> <p><strong class="example...
0
{ "code": "class Solution {\npublic:\n int majorityElement(vector<int>& nums) {\n int n=nums.size();\n int cnt=0;\n int ans;\n for(int i=0;i<n;i++){\n if(cnt==0){\n cnt=1;\n ans=nums[i];\n }\n else if(nums[i]==ans){\n ...
169
<p>Given an array <code>nums</code> of size <code>n</code>, return <em>the majority element</em>.</p> <p>The majority element is the element that appears more than <code>&lfloor;n / 2&rfloor;</code> times. You may assume that the majority element always exists in the array.</p> <p>&nbsp;</p> <p><strong class="example...
0
{ "code": "class Solution {\npublic:\n int majorityElement(vector<int>& nums) {\n int ans;\n int count=0;\n for(int i=0;i<nums.size();i++){\n if(count==0){\n ans= nums[i];\n count++;\n } else if(ans==nums[i]){\n count++;\n ...
169
<p>Given an array <code>nums</code> of size <code>n</code>, return <em>the majority element</em>.</p> <p>The majority element is the element that appears more than <code>&lfloor;n / 2&rfloor;</code> times. You may assume that the majority element always exists in the array.</p> <p>&nbsp;</p> <p><strong class="example...
0
{ "code": "class Solution {\npublic:\n int majorityElement(vector<int>& nums) {\n int count=1;\n int candidate=nums[0];\n int n=1;\n while(n<nums.size()){\n if(nums[n]==candidate){\n count++;\n } else {\n count--;\n if(c...
169
<p>Given an array <code>nums</code> of size <code>n</code>, return <em>the majority element</em>.</p> <p>The majority element is the element that appears more than <code>&lfloor;n / 2&rfloor;</code> times. You may assume that the majority element always exists in the array.</p> <p>&nbsp;</p> <p><strong class="example...
0
{ "code": "class Solution {\npublic:\n int majorityElement(vector<int>& nums) {\n int i = 0;\n for(i;i<nums.size();i++){\n if(count(nums.begin(),nums.end(),nums[i])>=(nums.size()+1)/2){\n break;\n }\n }\n return nums[i];\n }\n};", "memory": "269...
169
<p>Given an array <code>nums</code> of size <code>n</code>, return <em>the majority element</em>.</p> <p>The majority element is the element that appears more than <code>&lfloor;n / 2&rfloor;</code> times. You may assume that the majority element always exists in the array.</p> <p>&nbsp;</p> <p><strong class="example...
0
{ "code": "class Solution {\npublic:\n int majorityElement(vector<int>& nums) {\n int l = nums.size();\n int ele = nums[0];\n int count = 1;\n \n for (int i = 1; i < l; i++) {\n if (nums[i] == ele) {\n count += 1;\n } else {\n c...
169
<p>Given an array <code>nums</code> of size <code>n</code>, return <em>the majority element</em>.</p> <p>The majority element is the element that appears more than <code>&lfloor;n / 2&rfloor;</code> times. You may assume that the majority element always exists in the array.</p> <p>&nbsp;</p> <p><strong class="example...
0
{ "code": "class Solution {\npublic:\n int majorityElement(vector<int>& nums) {\n int count = 0;\n int curr_el = nums[0];\n for (int i = 0; i < nums.size(); i++) {\n if (count == 0) {\n curr_el = nums[i];\n }\n if (nums[i] == curr_el) {\n ...
169
<p>Given an array <code>nums</code> of size <code>n</code>, return <em>the majority element</em>.</p> <p>The majority element is the element that appears more than <code>&lfloor;n / 2&rfloor;</code> times. You may assume that the majority element always exists in the array.</p> <p>&nbsp;</p> <p><strong class="example...
0
{ "code": "// Solve by sorting\n/*\nclass Solution \n{\npublic:\n int majorityElement(vector<int>& nums) \n {\n sort(nums.begin(), nums.end());\n return nums[nums.size()/2];\n }\n};\n*/\n\n// Solve using bit manipulation\nclass Solution \n{\npublic:\n int majorityElement(vector<int>& nums) \...
169
<p>Given an array <code>nums</code> of size <code>n</code>, return <em>the majority element</em>.</p> <p>The majority element is the element that appears more than <code>&lfloor;n / 2&rfloor;</code> times. You may assume that the majority element always exists in the array.</p> <p>&nbsp;</p> <p><strong class="example...
2
{ "code": "class Solution {\npublic:\n int majorityElement(vector<int>& nums) {\n int cnt = 0;\n for(int i=0; i<nums.size(); i++){\n cnt = 0;\n for(int j=0; j<nums.size(); j++){\n if(nums[i] == nums[j]){\n cnt++;\n }\n ...
169
<p>Given an array <code>nums</code> of size <code>n</code>, return <em>the majority element</em>.</p> <p>The majority element is the element that appears more than <code>&lfloor;n / 2&rfloor;</code> times. You may assume that the majority element always exists in the array.</p> <p>&nbsp;</p> <p><strong class="example...
2
{ "code": "class Solution {\npublic:\n int majorityElement(vector<int>& nums) {\n int n=nums.size();\n int k=n/2;\n int p;\n int majority=-1;\n int index=-1;\n int i,j;\n for( i=0;i<n;i++)\n{\n int count=1;\n for( j=i+1;j<n;j++)\n ...
169
<p>Given an array <code>nums</code> of size <code>n</code>, return <em>the majority element</em>.</p> <p>The majority element is the element that appears more than <code>&lfloor;n / 2&rfloor;</code> times. You may assume that the majority element always exists in the array.</p> <p>&nbsp;</p> <p><strong class="example...
2
{ "code": "class Solution {\npublic:\n int majorityElement(vector<int>& nums) {\n map<int,int> c;\n\t for(auto it=nums.begin();it!=nums.end();it++){\n\t\t c[*it]++;\n\t }\n\t int m=0;\n\t int l=0;\n\t for(const auto& p: c){\n\t\t if(m<p.second){\n\t\t\t m=p.second;\n\t\t\t l=p.fir...
169
<p>Given an array <code>nums</code> of size <code>n</code>, return <em>the majority element</em>.</p> <p>The majority element is the element that appears more than <code>&lfloor;n / 2&rfloor;</code> times. You may assume that the majority element always exists in the array.</p> <p>&nbsp;</p> <p><strong class="example...
2
{ "code": "class Solution {\npublic:\n int majorityElement(vector<int>& nums) {\n map<int,int> mp;\n int n=nums.size();\n n=floor(n/2);\n for(int i=0;i<nums.size();i++)\n {\n mp[nums[i]]++;\n }\n for(auto it:mp)\n {\n if(it.second>n)\n ...
169
<p>Given an array <code>nums</code> of size <code>n</code>, return <em>the majority element</em>.</p> <p>The majority element is the element that appears more than <code>&lfloor;n / 2&rfloor;</code> times. You may assume that the majority element always exists in the array.</p> <p>&nbsp;</p> <p><strong class="example...
2
{ "code": "class Solution {\npublic:\n int majorityElement(vector<int>& nums) {\n ios::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n int candidate;\n int vote = 0;\n for(int &x: nums){\n vote==0 ? candidate=x, vote=1 : candidate==x ? ++vote : --v...
169
<p>Given an array <code>nums</code> of size <code>n</code>, return <em>the majority element</em>.</p> <p>The majority element is the element that appears more than <code>&lfloor;n / 2&rfloor;</code> times. You may assume that the majority element always exists in the array.</p> <p>&nbsp;</p> <p><strong class="example...
2
{ "code": "class Solution {\npublic:\nSolution(){\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n}\n int majorityElement(vector<int>& nums) {\n map<int,int> m;\n for(int i=0;i<nums.size();i++){\n m[nums[i]]++;\n }\n for(auto it:m){\n if(it.second>nums.size()/...
169
<p>Given an array <code>nums</code> of size <code>n</code>, return <em>the majority element</em>.</p> <p>The majority element is the element that appears more than <code>&lfloor;n / 2&rfloor;</code> times. You may assume that the majority element always exists in the array.</p> <p>&nbsp;</p> <p><strong class="example...
2
{ "code": "class Solution {\npublic:\n int majorityElement(vector<int>& nums) {\n int j = nums.size();\n stable_sort(nums.begin(),nums.end());\n return nums[j/2];\n }\n};", "memory": "27400" }
169
<p>Given an array <code>nums</code> of size <code>n</code>, return <em>the majority element</em>.</p> <p>The majority element is the element that appears more than <code>&lfloor;n / 2&rfloor;</code> times. You may assume that the majority element always exists in the array.</p> <p>&nbsp;</p> <p><strong class="example...
2
{ "code": "class Solution {\npublic:\n int majorityElement(vector<int>& nums) {\n stable_sort(nums.begin(),nums.end());\n return nums[(nums.size())/2];\n }\n};", "memory": "27500" }
169
<p>Given an array <code>nums</code> of size <code>n</code>, return <em>the majority element</em>.</p> <p>The majority element is the element that appears more than <code>&lfloor;n / 2&rfloor;</code> times. You may assume that the majority element always exists in the array.</p> <p>&nbsp;</p> <p><strong class="example...
2
{ "code": "class Solution {\npublic:\n int majorityElement(vector<int>& nums) {\n stable_sort(nums.begin(),nums.end());\n return nums[(nums.size())/2];\n }\n};", "memory": "27600" }
169
<p>Given an array <code>nums</code> of size <code>n</code>, return <em>the majority element</em>.</p> <p>The majority element is the element that appears more than <code>&lfloor;n / 2&rfloor;</code> times. You may assume that the majority element always exists in the array.</p> <p>&nbsp;</p> <p><strong class="example...
2
{ "code": "class Solution {\npublic:\n int majorityElement(vector<int>& nums) {\n int n=nums.size();\n sort(nums.begin(),nums.end());\n return nums[n/2];\n }\n};", "memory": "27700" }
1,528
<p>There are <code>n</code> kids with candies. You are given an integer array <code>candies</code>, where each <code>candies[i]</code> represents the number of candies the <code>i<sup>th</sup></code> kid has, and an integer <code>extraCandies</code>, denoting the number of extra candies that you have.</p> <p>Return <e...
0
{ "code": "class Solution {\npublic:\n vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) {\n vector<bool> result;\n int maxi = -1;\n for(auto i:candies) maxi = max(maxi,i);\n for(auto i:candies) {\n if(i + extraCandies >= maxi) result.push_back(true);\n ...
1,528
<p>There are <code>n</code> kids with candies. You are given an integer array <code>candies</code>, where each <code>candies[i]</code> represents the number of candies the <code>i<sup>th</sup></code> kid has, and an integer <code>extraCandies</code>, denoting the number of extra candies that you have.</p> <p>Return <e...
0
{ "code": "class Solution {\npublic:\n vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) {\n int maxi = *max_element(candies.begin(),candies.end());\n int n = candies.size();\n vector<bool> ans(n,false);\n for(int i=0;i<n;i++){\n if(extraCandies+candies[i]>...
1,528
<p>There are <code>n</code> kids with candies. You are given an integer array <code>candies</code>, where each <code>candies[i]</code> represents the number of candies the <code>i<sup>th</sup></code> kid has, and an integer <code>extraCandies</code>, denoting the number of extra candies that you have.</p> <p>Return <e...
0
{ "code": "class Solution {\npublic:\n vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) {\n vector<bool> ans;\n int max = candies[0];\n for(int i = 0; i<candies.size(); i++){\n if(candies[i] > max){\n max = candies[i];\n }\n }\n ...
1,528
<p>There are <code>n</code> kids with candies. You are given an integer array <code>candies</code>, where each <code>candies[i]</code> represents the number of candies the <code>i<sup>th</sup></code> kid has, and an integer <code>extraCandies</code>, denoting the number of extra candies that you have.</p> <p>Return <e...
0
{ "code": "#include <vector>\n#include <algorithm>\n\nclass Solution {\npublic:\n std::vector<bool> kidsWithCandies(const std::vector<int>& candies, int extraCandies) {\n int iMax = *std::max_element(candies.begin(), candies.end());\n\n // Use std::transform to apply a function to each element and st...
1,528
<p>There are <code>n</code> kids with candies. You are given an integer array <code>candies</code>, where each <code>candies[i]</code> represents the number of candies the <code>i<sup>th</sup></code> kid has, and an integer <code>extraCandies</code>, denoting the number of extra candies that you have.</p> <p>Return <e...
0
{ "code": "class Solution {\npublic:\n vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) {\n \n int maxCandies = -1;\n int n = candies.size();\n for(int i=0;i<n;i++) maxCandies = max(maxCandies, candies[i]);\n vector<bool> res(n,false);\n //int find = maxCa...
1,528
<p>There are <code>n</code> kids with candies. You are given an integer array <code>candies</code>, where each <code>candies[i]</code> represents the number of candies the <code>i<sup>th</sup></code> kid has, and an integer <code>extraCandies</code>, denoting the number of extra candies that you have.</p> <p>Return <e...
0
{ "code": "class Solution {\npublic:\n vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) {\n int maxx = *max_element(candies.begin(), candies.end());\n vector<bool> ans((int)candies.size(), false);\n for (int i = 0; i < candies.size(); i++) {\n if (candies[i] + ex...
1,528
<p>There are <code>n</code> kids with candies. You are given an integer array <code>candies</code>, where each <code>candies[i]</code> represents the number of candies the <code>i<sup>th</sup></code> kid has, and an integer <code>extraCandies</code>, denoting the number of extra candies that you have.</p> <p>Return <e...
1
{ "code": "class Solution {\npublic:\n vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) {\n vector<bool>ans;\n int maxi = 0;\n for (int i=0;i<candies.size();i++){\n if (candies[i] > maxi){\n maxi = candies[i];\n }\n }\n for...
1,528
<p>There are <code>n</code> kids with candies. You are given an integer array <code>candies</code>, where each <code>candies[i]</code> represents the number of candies the <code>i<sup>th</sup></code> kid has, and an integer <code>extraCandies</code>, denoting the number of extra candies that you have.</p> <p>Return <e...
1
{ "code": "class Solution {\npublic:\n vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) {\n int n =candies.size();\n int maxCandies = *max_element(begin(candies),end(candies));\n vector<bool> result(n,false);\n for(int i =0;i<n;i++){\n if(candies[i] + extraCandies >= maxC...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
0
{ "code": "#include <vector>\n#include <iostream>\n#define ll long long\nusing namespace std;\nll f[1 << 10][42];\nll MOD = 1e9 + 7;\nclass Solution\n{\npublic:\n int numberHats = 40;\n int n;\n int prefer[12][42];\n int numberWays(vector<vector<int>> &hats)\n {\n // F[bit][i] people in bit wear...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
0
{ "code": "\n// #define ll long long int \n// int mod = 1e9 + 7;\n\n\n// class Solution {\n// long long countWays(int person, vector<vector<int>>& hats, vector<bool>& used) {\n// int n = hats.size();\n// if (person == n) { \n// return 1;\n// }\n \n \n// long l...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
0
{ "code": "class Solution {\npublic:\n int dp[42][2000];\n int mod=1e9+7;\n int f(int hat,int mask,vector<vector<int>>&hats,int m){\n int n=hats.size();\n if(hat>40){\n if(mask==m) return 1;\n return 0;\n }\n if(dp[hat][mask]!=-1) return dp[hat][mask];\n ...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
0
{ "code": "class Solution {\npublic:\n int n;\n int dp[41][(1<<10)];\n int MOD = 1e9 + 7;\n int col[41];\n\n int solve(int ind, int mask, vector<vector<int>>& hats) {\n if (mask == (1 << n) - 1) \n return 1;\n if (ind == 41) \n return 0;\n\n if (dp[ind][mask...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
0
{ "code": "class Solution {\npublic:\n vector<int>arr[41];\n int dp[41][(1<<10)];int mod=1e9+7;int n;\n int solve(int ind,int mask){\n if(mask==(1<<n)-1) return 1;\n if(ind>=41) return 0;\n if(dp[ind][mask]!=-1) return dp[ind][mask];\n int res=0;\n int notake=solve(ind+1,ma...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
0
{ "code": "class Solution {\n int mod = 1e9 + 7; // To avoid overflow\n int dp[41][1 << 10]; // DP table: dp[hat][mask], where mask is a bitmask representing the hats already assigned\n vector<vector<int>> personHats; // To store the hats each person can wear\n\n // Recursive function to count the number ...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
0
{ "code": "class Solution {\npublic:\n //we can solve by assigning 40 hats to those people\n int mod=1e9+7;\n int dp[41][1<<10];\n long long f(int ind, int mask, vector<int>caps[], int n){\n if( mask == ((1<<n)-1) ) return 1;\n if(ind>40) return 0;\n\n if(dp[ind][mask]!=-1) return dp[...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
0
{ "code": "class Solution {\npublic:\n int n,m;\n vector<int>Hats[41]; // Hats[i] = list of the people who prefer i-th hat\n\n // Reverseing the dp style.\n int dp[41][(1<<11)]; // dp[i][mask] = count of additional ways to to use i-th hat to m-th hat to fill unused persons.\n\n\n int mod = 1000000007;\...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
0
{ "code": "class Solution {\npublic:\n\n int dp[41][1024];\n\n int numberWays(vector<vector<int>> &people,int etapa,int bitmask,int pessoas)\n{\n if (dp[etapa][bitmask] != -1) return dp[etapa][bitmask];\n\n if (bitmask == (1<<pessoas)-1) return 1;\n\n if (etapa >= 40) return 0;\n\n int size = people...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
1
{ "code": "class Solution {\npublic:\n int dp[41][1<<11];\n int M=1e9+7;\n int getWays(int i,int peopleMask,int count,int n,vector<vector<int>> &people)\n {\n if(count==n)\n return 1;\n if(i>40)\n return 0;\n if(dp[i][peopleMask]!=-1)\n return dp[i][pe...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
1
{ "code": "class Solution {\npublic:\n long t[41][1 << 10]; //memo table\n int mod = 1e9 + 7;\n int numberWays(vector<vector<int>>& hats) {\n \n vector <vector<int>> interim;\n int maxId = 0;\n \n for(int i = 0; i < hats.size(); i++)\n {\n vector <int> tem...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
1
{ "code": "class Solution {\npublic:\n int mod=1e9+7;\n int dp[42][3000];\n int helper(int hat,vector<int> caps[],int n,int mask){\n if(mask==((1<<n)-1)) return 1;\n if(hat>40) return 0;\n if(dp[hat][mask]!=-1) return dp[hat][mask];\n int take=0;\n for(auto j:caps[hat]){\n ...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>>v;\n long long mod = 1e9+7;\n long long dp[40][1<<10];\n int n ;\n long long helper(int i, int mask) {\n if(i==v.size()) {\n return __builtin_popcount(mask) == n;\n }\n if(dp[i][mask]!=-1) return dp[i][mask];\n ...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
1
{ "code": "class Solution {\nprivate:\n int dp[41][1025], mod = 1e9+7; \n int n;\n unordered_map <int, vector<int>> mp;\n vector<vector<int>> a; \n int func(int hat, int mask) {\n if(mask == 0)\n return 1;\n if(hat > 40)\n return 0;\n if(dp[hat][mask] != -1)...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
1
{ "code": "class Solution \n{\npublic:\n int numberWays(vector<vector<int>>& hats) \n {\n int n = hats.size();\n std::unordered_map<int, std::vector<int>> map;\n for (int i = 0; i < n; ++i)\n {\n for (auto hat : hats[i])\n map[hat].push_back(i);\n }\n...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
1
{ "code": "#include <bits/stdc++.h>\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\n\nusing namespace std;\nusing namespace __gnu_pbds;\n\ntemplate <typename T>\nusing ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;\n\n#define ll long long in...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
1
{ "code": "class Solution {\npublic:\n #define ll long long \n const int MOD = 1e9 + 7 ;\n ll dp[41][1 << 10] ;\n int n ;\n unordered_map<int,vector<int>> mpp ;\n \n ll solve(int hat , int mask){\n int all = (1 << n) - 1; //mask for all persons visited\n \n if(mask == all) re...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
1
{ "code": "class Solution {\npublic:\n #define ll long long \n const int MOD = 1e9 + 7 ;\n ll dp[41][1 << 10] ;\n int n ;\n unordered_map<int,vector<int>> mpp ;\n \n ll solve(int hat , int mask){\n int all = (1 << n) - 1; //mask for all persons visited\n \n if(mask == all) re...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
1
{ "code": "class Solution {\npublic:\nint n;\nint dp[41][(1 << 10) + 9];\nmap<int, vector<int>> arr;\nint mod = 1e9+7;\n\nint solve(int idx, int mask){\n\t//BaseCase : All people assigned hat\n\t//ie mask is 1111111111 return 1 \n\t//ie 1 possible arrangement found\n\tif(mask == (1 << n) - 1)\n\t\treturn 1;\n\n\t//If...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
1
{ "code": "class Solution {\npublic:\n int numberWays(vector<vector<int>>& hats) {\n const int MOD = 1e9 + 7;\n int n = hats.size();\n vector<vector<int>> dp(41, vector<int>(1 << n, 0));\n dp[0][0] = 1;\n for (int i = 1; i <= 40; ++i) {\n dp[i] = dp[i - 1];\n ...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
1
{ "code": "class Solution {\npublic:\n \n int numberWays(vector<vector<int>>& hats) {\n int n = hats.size(); // Number of people\n int mod = 1e9 + 7;\n vector<vector<int>> peopleWithHat(41); // Hats are 1-indexed up to 40\n \n // Mapping hats to people\n for (int i = 0; i < n; ++i) {\n ...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
1
{ "code": "class Solution {\nconst int M = 1e9 + 7;\npublic:\n int numberWays(vector<vector<int>>& hats) {\n int n = hats.size();\n vector<vector<int>>caps(41);\n for(int i=0;i<n;i++){\n for(int j = 0;j < hats[i].size() ; j++){\n caps[hats[i][j]].push_back(i);\n ...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
2
{ "code": "#include <vector>\n#include <unordered_map>\n\nusing namespace std;\n\nclass Solution {\npublic:\n int numberWays(vector<vector<int>>& hats) {\n const int MOD = 1e9 + 7;\n int n = hats.size();\n \n // Create a mapping of hats to people\n vector<vector<int>> hat_to_peop...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
2
{ "code": "class Solution {\npublic:\n const int MOD = 1e9 + 7;\n \n // Memoization table to store intermediate results\n vector<vector<int>> dp;\n \n // Dynamic programming function to calculate the number of ways\n int dfs(int hat, int mask, vector<vector<int>>& hats_to_people, int done) {\n ...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
2
{ "code": "class Solution {\npublic:\n int count_ways(int cap,int left_persons,vector<int> per_hav_hat[],vector<int> &avl_caps,vector<vector<int>> &dp)\n {\n int N = 1000000007;\n if(left_persons==0)\n {\n return 1;\n }\n if(cap>40)\n {\n return 0;...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
2
{ "code": "class Solution {\npublic:\n\nunordered_map<int, vector<int>> mp;\n\nint dp[41][1<<10];\n\nint mod= 1e9+7;\n\nint help( int i, int n, int mask ){\n\n if(mask==((1<<n)-1 )) return 1;\n\n if(i>40) return 0;\n\n if(dp[i][mask]!=-1) return dp[i][mask];\n\n int a=0,b=0;\n\n for(auto t: mp[i]){...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
2
{ "code": "class Solution {\npublic:\nint mod = 1e9+7;\n int numberWays(vector<vector<int>>& hats) {\n int n = hats.size();\n vector<vector<int>> dp((1<<n),vector<int>(41,0));\n for(int i = 0 ; i <= 40 ; i++)\n {\n dp[0][i] = 1;\n }\n for(int i = 1 ; i < (1<<n)...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
2
{ "code": "class Solution {\npublic:\nint MOD = 1e9+7;\n int numberWays(vector<vector<int>>& hats) {\n int n = hats.size();\n int mask = (1 << n)- 1; // 1024\n\n vector<vector<int>> hat_people(40);\n for (int i = 0; i < hats.size(); ++i) {\n for (int hat : hats[i]) {\n ...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
2
{ "code": "class Solution {\npublic:\n int n, M = 1e9 + 7;\n \n int dfs(vector<vector<int>>& hatsToPeople, vector<vector<int>>& dp, int hat, int mask) {\n // if the mask is able to reache the end state, \n // i.e. all people can wear some hats, then return 1\n if (mask == (1 << n) - 1) r...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
2
{ "code": "class Solution {\npublic:\n int mod = 1000000007;\n vector<vector<int>> dp; // Memoization table for DP\n\n // DP function to calculate the number of ways\n int solve(int peopleMask, int idHat, const vector<vector<int>>& hatToPeople, int numPeople) {\n if (peopleMask == (1 << numPeople)...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
2
{ "code": "const int mod = 1e9 + 7;\nclass Solution {\nprivate:\n\tint fun(int &n, vector<int>*adj, vector<vector<int>>&dp, int s, int mask) {\n\t\tif (s == 41) {\n\t\t\treturn (mask + 1 == (1 << n));\n\t\t}\n\t\tif (dp[mask][s] != -1)return dp[mask][s];\n\t\tint ans = fun(n, adj, dp, s + 1, mask);\n\t\tfor (int i = ...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
2
{ "code": "class Solution {\npublic:\n int mod = 1e9 + 7;\n int numberWays(vector<vector<int>>& hats) {\n int n = hats.size();\n unordered_map<int, vector<int>> hats_to_people_map;\n for(int ppl_idx = 0; ppl_idx < n; ++ppl_idx) {\n for(int& hat : hats[ppl_idx]) {\n ...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
2
{ "code": "class Solution {\npublic:\n int numberWays(vector<vector<int>>& preferences) {\n const int MOD = (int)1e9 + 7;\n int n(preferences.size()), k((1ll<<n)-1);\n map<int, vector<int>> mp;\n for (int i(0); i < n; ++i) \n for (int &hat : preferences[i]) \n ...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
2
{ "code": "class Solution \n{\npublic:\n int numberWays(vector<vector<int>>& hats) \n {\n int n = hats.size();\n int totalState = (1 << n) - 1;\n std::vector<int> dp(totalState + 1, 0);\n\n std::unordered_map<int, std::vector<int>> hatMap;\n for (int i = 0; i < n; ++i)\n ...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
2
{ "code": "class Solution {\npublic:\n int dp[41][1L << 10];\n map<int, set<int>> mp;\n int count(int i, long mask, vector<vector<int>>& hats) {\n long long mod = (long long)1e9 + 7;\n if (mask + 1 == (1 << hats.size()))\n return 1;\n if (i == 41)\n return 0;\n\n ...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
2
{ "code": "class Solution {\npublic:\nconst int mod = 1e9+7;\n int solve(vector<set<int>> &hats, int hat,int mask,vector<vector<int>> &memo){\n int n = hats.size();\n if (hat>40){\n if (mask==((1<<n)-1)) return 1;\n return 0;\n }\n if (memo[hat][mask]!=-1){\n ...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
2
{ "code": "#define setbits(x) __builtin_popcount(x)\nconst int M = 1000000007;\nclass Solution {\n int solve(int mask, int num, vector<vector<bool>> &hat, vector<vector<int>> &dp, int n){\n if(setbits(mask)==n){\n return 1;\n }\n if(num==40) return 0;\n if(dp[mask][num]!=-1) ...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
2
{ "code": "class Solution {\n int n,done;\n int mod = 1e9+7;\n vector<vector<int>> memo;\n unordered_map<int,vector<int>> mp;\n int dp(int hat,int mask) {\n if (mask==done) {\n return 1;\n }\n if (hat>40) return 0;\n if (memo[hat][mask]!=-1) return memo[hat][mask]...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
2
{ "code": "class Solution {\npublic:\n int MOD = 1e9+7;\n int goFind(int hat, int mask, int &n, unordered_map<int, vector<int>> &mp, vector<vector<int>> &dp) {\n if(hat == 0) {\n return __builtin_popcount(mask) == n;\n }\n if(dp[hat][mask] != -1) {\n return dp[hat][mas...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
3
{ "code": "using ll = long long;\n\nclass Solution {\npublic:\n int numberWays(vector<vector<int>>& hats) {\n vector<vector<int>> d(41);\n int n = hats.size();\n int mx = 0;\n for (int i = 0; i < n; ++i) {\n for (int& h : hats[i]) {\n d[h].push_back(i);\n ...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
3
{ "code": "class Solution {\npublic:\n int numberWays(vector<vector<int>>& hats) {\n // there are 40 hats\n // and at most 10 persons.\n // to state compression, we should take 10 persons as state instead of 40 hats.\n // memo[human_state][hat_idx] = \n // given the human sta...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
3
{ "code": "class Solution {\npublic: \n int mod = 1e9+7;\n long long solve(long long hat , long long mask , vector<vector<int>>&hats,\n vector<vector<long long>>&dp)\n {\n long long n = hats.size();\n if(mask==0)\n {\n return 1; //means sabko de diya hats 1 way found\n ...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
3
{ "code": "class Solution {\npublic:\n int n;\n int mod=1e9+7;\n int func(int i, int marks, vector<int>& v, unordered_map<int, vector<int>>& mp, vector<vector<int>>& dp){\n if(marks==((1<<(n))-1)) return 1;\n if(i==v.size() || v[i]>40) return 0;\n\n if(dp[marks][i]!=-1) return dp[marks][...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> dp;\n vector<vector<int>> htp;\n int mod = 1000000007;\n int full;\n int solve(int a, int b) {\n if (b >= full) return 1;\n if (a >= 40) return 0;\n if (dp[a][b] >= 0) return dp[a][b];\n\n long long res = solve(a + 1...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
3
{ "code": "#define ll long long int\n#define vi vector<int>\n#define vvi vector<vi>\n#define vll vector<ll>\n#define vvll vector<vll>\n#define mod 1000000007\n#define total 40\n\n/*\n dp[i][mask] represents the number of ways to wear different hats\n when upto ith hat has been processed for persons marked i...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
3
{ "code": "class Solution {\npublic:\n long long int solve(vector<vector<int>>& hats, vector<vector<long long int>>& dp, int ind, long long int mask, int people){\n //_______________________________________________________\n\n //T.C. : N*K*(2^K)\n\n //__________________________________________...