id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
3,206
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> of sizes <code>n</code> and <code>m</code>, respectively. Calculate the following values:</p> <ul> <li><code>answer1</code> : the number of indices <code>i</code> such that <code>nums1[i]</code> exists in <code>nums2</code>.</li> <li><code...
3
{ "code": "class Solution {\npublic:\n vector<int> findIntersectionValues(vector<int>& nums1, vector<int>& nums2) {\n map<int,bool>ans1;\n map<int,bool>ans2;\n for(int i=0;i<nums1.size();i++){\n ans1[nums1[i]]=true;\n }\n for(int i=0;i<nums2.size();i++){\n a...
3,206
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> of sizes <code>n</code> and <code>m</code>, respectively. Calculate the following values:</p> <ul> <li><code>answer1</code> : the number of indices <code>i</code> such that <code>nums1[i]</code> exists in <code>nums2</code>.</li> <li><code...
3
{ "code": "class Solution {\npublic:\n vector<int> findIntersectionValues(vector<int>& nums1, vector<int>& nums2) {\n int ans1 = 0, ans2 = 0;\n unordered_map<int, int> nums1Count, nums2Count;\n for (auto num: nums1) nums1Count[num]++;\n for (auto num: nums2) {\n nums2Count[nu...
3,206
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> of sizes <code>n</code> and <code>m</code>, respectively. Calculate the following values:</p> <ul> <li><code>answer1</code> : the number of indices <code>i</code> such that <code>nums1[i]</code> exists in <code>nums2</code>.</li> <li><code...
3
{ "code": "class Solution {\npublic:\n vector<int> findIntersectionValues(vector<int>& nums1, vector<int>& nums2) {\n unordered_map<int,bool> m1;\n unordered_map<int,bool> m2;\n for(auto i:nums1){\n m1[i]=true;\n }\n for(auto i:nums2){\n m2[i]=true;\n ...
3,230
<p>You are given a <strong>0-indexed</strong> string <code>word</code>.</p> <p>In one operation, you can pick any index <code>i</code> of <code>word</code> and change <code>word[i]</code> to any lowercase English letter.</p> <p>Return <em>the <strong>minimum</strong> number of operations needed to remove all adjacent...
0
{ "code": "class Solution {\npublic:\n int f(int i,int last,string &s,vector<vector<int>>&dp)\n {\n if(i>=s.size())return 0;\n if(dp[i][last+2]!=-1)return dp[i][last+2];\n int c=s[i]-'a';\n int ans=1e9;\n for(int j=0;j<26;++j)\n {\n int cost=0;\n ...
3,230
<p>You are given a <strong>0-indexed</strong> string <code>word</code>.</p> <p>In one operation, you can pick any index <code>i</code> of <code>word</code> and change <code>word[i]</code> to any lowercase English letter.</p> <p>Return <em>the <strong>minimum</strong> number of operations needed to remove all adjacent...
0
{ "code": "class Solution {\n bool almostEq(char a, char b){\n return abs(a-b)<=1;\n }\npublic:\n int removeAlmostEqualCharacters(string word) {\n int n=word.size(),i,prev=0, c=0;\n for(i=1; i<n; i++){\n if(almostEq(word[i], word[i-1])){\n c++;\n ...
3,230
<p>You are given a <strong>0-indexed</strong> string <code>word</code>.</p> <p>In one operation, you can pick any index <code>i</code> of <code>word</code> and change <code>word[i]</code> to any lowercase English letter.</p> <p>Return <em>the <strong>minimum</strong> number of operations needed to remove all adjacent...
0
{ "code": "class Solution {\npublic:\n int removeAlmostEqualCharacters(string word) {\n \n int ans = 0;\n for(int i = 1; i < word.size(); ) {\n if(abs(word[i] - word[i-1]) <= 1) {\n ans++;\n i += 2;\n } else {\n i++;\n ...
3,230
<p>You are given a <strong>0-indexed</strong> string <code>word</code>.</p> <p>In one operation, you can pick any index <code>i</code> of <code>word</code> and change <code>word[i]</code> to any lowercase English letter.</p> <p>Return <em>the <strong>minimum</strong> number of operations needed to remove all adjacent...
0
{ "code": "class Solution {\npublic:\n int removeAlmostEqualCharacters(string word) {\n int n = word.size();\n int ret = 0;\n for(int i=1; i<n; ++i) {\n if(std::abs(word[i]-word[i-1]) <= 1) {\n ++ret;\n ++i;\n }\n }\n return ret...
3,230
<p>You are given a <strong>0-indexed</strong> string <code>word</code>.</p> <p>In one operation, you can pick any index <code>i</code> of <code>word</code> and change <code>word[i]</code> to any lowercase English letter.</p> <p>Return <em>the <strong>minimum</strong> number of operations needed to remove all adjacent...
0
{ "code": "/*\n\n*/\n\nclass Solution {\npublic:\n int removeAlmostEqualCharacters(string word) \n {\n int n = word.size(), res = 0;\n for (int i = 0; i < n-1; i++)\n {\n if (abs(word[i] - word[i+1]) <= 1)\n {\n res ++;\n i = i+1;\n ...
3,230
<p>You are given a <strong>0-indexed</strong> string <code>word</code>.</p> <p>In one operation, you can pick any index <code>i</code> of <code>word</code> and change <code>word[i]</code> to any lowercase English letter.</p> <p>Return <em>the <strong>minimum</strong> number of operations needed to remove all adjacent...
2
{ "code": "class Solution {\npublic:\n int removeAlmostEqualCharacters(string word) {\n int n = word.size();\n int ret = 0;\n for(int i=1; i<n; ++i) {\n if(abs(word[i]-word[i-1]) <= 1) {\n ++ret;\n ++i;\n }\n }\n return ret;\n ...
3,230
<p>You are given a <strong>0-indexed</strong> string <code>word</code>.</p> <p>In one operation, you can pick any index <code>i</code> of <code>word</code> and change <code>word[i]</code> to any lowercase English letter.</p> <p>Return <em>the <strong>minimum</strong> number of operations needed to remove all adjacent...
3
{ "code": "#include <cmath>\n\nclass Solution {\npublic:\n int removeAlmostEqualCharacters(string word) {\n \n vector<int> v (word.length(), 0);\n\n if(word.length() <= 1) return 0;\n if(word.length() == 2) return abs(word[0] - word[1]) <= 1;\n \n if(abs(word[1] - word[0])...
3,230
<p>You are given a <strong>0-indexed</strong> string <code>word</code>.</p> <p>In one operation, you can pick any index <code>i</code> of <code>word</code> and change <code>word[i]</code> to any lowercase English letter.</p> <p>Return <em>the <strong>minimum</strong> number of operations needed to remove all adjacent...
3
{ "code": "class Solution {\npublic:\n int removeAlmostEqualCharacters(string word) {\n int n= word.length();\n vector<int>vis(n,0);\n int c=0;\n for(int i=0;i<word.length()-1;i++){\n // cout<<word[i]-word[i+1] <<word[i]<<\" \"<<word[i+1]<<endl;\n if(!vis[i] && ((w...
3,230
<p>You are given a <strong>0-indexed</strong> string <code>word</code>.</p> <p>In one operation, you can pick any index <code>i</code> of <code>word</code> and change <code>word[i]</code> to any lowercase English letter.</p> <p>Return <em>the <strong>minimum</strong> number of operations needed to remove all adjacent...
3
{ "code": "class Solution {\npublic:\n int removeAlmostEqualCharacters(string word) {\n int count = 0;\n \n int n = word.size();\n if(n==1) return 0;\n vector< int> dp(n,0);\n dp[0] = 0;\n dp[1] = (abs(word[1]- word[0]) <= 1) ? 1 : 0;\n for(int i = 2; i < n ;...
3,230
<p>You are given a <strong>0-indexed</strong> string <code>word</code>.</p> <p>In one operation, you can pick any index <code>i</code> of <code>word</code> and change <code>word[i]</code> to any lowercase English letter.</p> <p>Return <em>the <strong>minimum</strong> number of operations needed to remove all adjacent...
3
{ "code": "class Solution {\npublic:\n int removeAlmostEqualCharacters(string word) {\n int n = word.size();\n vector<int> dp(n);\n dp[0] = 0;\n for (int i = 1; i < n; i++) {\n dp[i] = dp[i-1];\n if (abs(word[i] - word[i-1]) <= 1) {\n dp[i]++;\n ...
3,230
<p>You are given a <strong>0-indexed</strong> string <code>word</code>.</p> <p>In one operation, you can pick any index <code>i</code> of <code>word</code> and change <code>word[i]</code> to any lowercase English letter.</p> <p>Return <em>the <strong>minimum</strong> number of operations needed to remove all adjacent...
3
{ "code": "class Solution {\npublic:\n\n struct element {\n int val[2];\n };\n\n element* g_cache;\n\n bool isAlmostEqual(const char a, const char b)\n {\n return abs(a - b) <= 1;\n }\n\n int sol(string& word, int startIndex, bool firstCharAE) {\n\n if (startIndex > word.size...
3,230
<p>You are given a <strong>0-indexed</strong> string <code>word</code>.</p> <p>In one operation, you can pick any index <code>i</code> of <code>word</code> and change <code>word[i]</code> to any lowercase English letter.</p> <p>Return <em>the <strong>minimum</strong> number of operations needed to remove all adjacent...
3
{ "code": "class Solution {\npublic:\n\n struct element {\n int val[2];\n };\n\n element* g_cache;\n\n bool isAlmostEqual(const char a, const char b)\n {\n return abs(a - b) <= 1;\n }\n\n int sol(string& word, int startIndex, bool firstCharAE) {\n\n if (startIndex > word.size...
3,230
<p>You are given a <strong>0-indexed</strong> string <code>word</code>.</p> <p>In one operation, you can pick any index <code>i</code> of <code>word</code> and change <code>word[i]</code> to any lowercase English letter.</p> <p>Return <em>the <strong>minimum</strong> number of operations needed to remove all adjacent...
3
{ "code": "class Solution {\npublic:\n int removeAlmostEqualCharacters(string word) {\n int n=word.length();\n vector<int>temp;\n for(int i=0;i<n-1;i++)\n {\n if(abs(word[i]-word[i+1])<=1)\n {\n temp.push_back(1);\n }\n else tem...
3,230
<p>You are given a <strong>0-indexed</strong> string <code>word</code>.</p> <p>In one operation, you can pick any index <code>i</code> of <code>word</code> and change <code>word[i]</code> to any lowercase English letter.</p> <p>Return <em>the <strong>minimum</strong> number of operations needed to remove all adjacent...
3
{ "code": "class Solution {\npublic:\n int removeAlmostEqualCharacters(string word) {\n int len = word.length();\n vector<int> nc(len, 0);\n vector<int> c(len, 0);\n nc[0] = 0;\n c[0] = 1;\n for (int i = 1; i < len; i++) {\n c[i] = min(c[i - 1], nc[i - 1]) + 1;\...
3,230
<p>You are given a <strong>0-indexed</strong> string <code>word</code>.</p> <p>In one operation, you can pick any index <code>i</code> of <code>word</code> and change <code>word[i]</code> to any lowercase English letter.</p> <p>Return <em>the <strong>minimum</strong> number of operations needed to remove all adjacent...
3
{ "code": "class Solution {\npublic:\n int removeAlmostEqualCharacters(string word) {\n word = '@' + word + '@';\n vector<int> temp(word.size(), 0);\n int res = 0;\n for (int i = 1; i < word.size() - 1; i++) {\n if (abs(word[i+1] - word[i]) <= 1 && temp[i-1] != 1) {\n ...
3,230
<p>You are given a <strong>0-indexed</strong> string <code>word</code>.</p> <p>In one operation, you can pick any index <code>i</code> of <code>word</code> and change <code>word[i]</code> to any lowercase English letter.</p> <p>Return <em>the <strong>minimum</strong> number of operations needed to remove all adjacent...
3
{ "code": "class Solution {\npublic:\n int removeAlmostEqualCharacters(string word) {\n int len = word.length();\n stack<char>st;\n int cnt=0;\n for(int i=0;i<len;i++){\n char ch=word[i];\n if(!st.empty()){\n char top = st.top();\n if(...
3,230
<p>You are given a <strong>0-indexed</strong> string <code>word</code>.</p> <p>In one operation, you can pick any index <code>i</code> of <code>word</code> and change <code>word[i]</code> to any lowercase English letter.</p> <p>Return <em>the <strong>minimum</strong> number of operations needed to remove all adjacent...
3
{ "code": "class Solution {\npublic:\n\n int n;\n\n int dp[101][26];\n\n int rec(int ind,int prev,string &word){\n if(ind==n)return 0;\n if(dp[ind][prev]!=-1)return dp[ind][prev];\n int ans=109;\n for(int i=0;i<26;++i){\n if(abs(i-prev)<=1)continue;\n if(i==w...
3,225
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p> <p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equ...
0
{ "code": "auto init = [](){\n ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);\n return 'c';\n}();\n\nconst size_t BUFFER_SIZE = 0x6fafffff;\nalignas(std::max_align_t) char buffer[BUFFER_SIZE];\nsize_t buffer_pos = 0;\n\nvoid* operator new(size_t size) {\n constexpr std::size_t alignment = alignof(std::max...
3,225
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p> <p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equ...
0
{ "code": "class Solution {\npublic:\n void comp(vector<int>& a) {\n vector<int> t = a;\n sort(t.begin(), t.end());\n t.erase(unique(t.begin(), t.end()), t.end());\n for (int& i : a) {\n i = lower_bound(t.begin(), t.end(), i) - t.begin();\n }\n }\n\n int maxSubar...
3,225
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p> <p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equ...
0
{ "code": "class Solution {\npublic:\n int maxSubarrayLength(vector<int>& nums, int k) {\n int l = 0;\n int size = 0;\n int x = 0;\n unordered_map<int, int>mp;\n\n for (x; x < nums.size(); x++)\n if (mp[nums[x]] < k)\n mp[nums[x]]++;\n else\n {\n si...
3,225
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p> <p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equ...
0
{ "code": "class Solution {\npublic:\n Solution(){ // constructor to be run before the solution --> lower time complexity\n // Fast input\n ios::sync_with_stdio(false); std::cin.tie(nullptr);\n }\n unordered_map<int , int> freq_mp; // prefered rather than ordered map as we do not need sorting\n...
3,225
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p> <p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equ...
0
{ "code": "class Solution {\npublic:\n Solution(){ // constructor to be run before the solution --> lower time complexity\n // Fast input\n ios::sync_with_stdio(false); std::cin.tie(nullptr);\n }\n unordered_map<int , int> freq_mp; // prefered rather than ordered map as we do not need sorting\n...
3,225
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p> <p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equ...
0
{ "code": "class Solution {\npublic:\n int maxSubarrayLength(vector<int>& nums, int k) {\n int n = nums.size();\n unordered_map<int,int> mp;\n int maxLen = 0;\n int i=0,j=0;\n while(i<n)\n {\n mp[nums[i]]++;\n while(j<=i && mp[nums[i]] > k)\n ...
3,225
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p> <p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equ...
0
{ "code": "class Solution {\npublic:\n int maxSubarrayLength(vector<int>& nums, int k) {\n int l=0, r = 0, n = nums.size(), len =0;\n unordered_map<int,int> mp;\n\n while(r < n){\n mp[nums[r]]++;\n\n while(mp[nums[r]] > k){\n mp[nums[l]]--;\n ...
3,225
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p> <p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equ...
0
{ "code": "class Solution {\npublic:\n int maxSubarrayLength(vector<int>& nums, int k) {\n int l = 0;\n int r = 0;\n int length = INT_MIN;\n\n unordered_map<int,int> mp;\n\n while(r < nums.size()){\n mp[nums[r]]++;\n\n while(mp[nums[r]] > k){\n ...
3,225
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p> <p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equ...
0
{ "code": "class Solution {\npublic:\n int maxSubarrayLength(vector<int>& nums, int k) {\n int maxi=INT_MIN;\n int i=0;int j=0;\n unordered_map<int,int>mp;\n\n while(j<nums.size()){\n mp[nums[j]]++;\n\n while(mp[nums[j]]>k){\n mp[nums[i]]--;\n ...
3,225
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p> <p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equ...
2
{ "code": "class Solution {\npublic:\n int maxSubarrayLength(vector<int>& nums, int k) {\n int n=nums.size();\n int res=0,i=0,j=0;\n unordered_map<int,int>m;\n while(j<n){\n m[nums[j]]++;\n while( m[nums[j]]>k){\n m[nums[i]]--;\n i++;\...
3,225
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p> <p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equ...
2
{ "code": "class Solution {\npublic:\n int maxSubarrayLength(vector<int>& nums, int k) {\n int longest=0;\n int s=0, e=0;\n unordered_map<int,int> freq;\n while(e<nums.size()) {\n freq[nums[e]]++;\n while(freq[nums[e]]>k) {\n freq[nums[s]]--;\n ...
3,225
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p> <p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equ...
2
{ "code": "class Solution {\npublic:\n int maxSubarrayLength(vector<int>& nums, int k) {\n // sliding window\n auto m = unordered_map<int, int>();\n auto maxi = 0;\n for (auto i = -1, j = 0; j < nums.size(); j++) {\n auto val = nums.at(j);\n if (!m.contains(val)) m...
3,225
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p> <p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equ...
2
{ "code": "class Solution {\npublic:\n int maxSubarrayLength(vector<int>& nums, int k) {\n unordered_map<int,int>mpp;\n int l=0,r=0,cnt=0;\n while(r<nums.size()){\n int curr=nums[r++];\n mpp[curr]++;\n while(l<r && mpp[curr]>k){\n mpp[nums[l++]]-...
3,225
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p> <p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equ...
2
{ "code": "class Solution {\npublic:\n int maxSubarrayLength(vector<int>& nums, int k) {\n unordered_map<int, int> freq;\n int left = 0, right = 0;\n auto n = nums.size();\n int ans = 0;\n while (right < n)\n {\n int cur = nums[right];\n while (right ...
3,225
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p> <p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equ...
2
{ "code": "class Solution {\npublic:\n int maxSubarrayLength(vector<int>& nums, int k) {\n if (nums.size() == 1)\n return 1;\n\n unordered_map<int, int> frequencyCount;\n int left{}, right{1};\n auto leftNumber = nums.at(left);\n auto rightNumber = nums.at(right);\n ...
3,225
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p> <p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equ...
2
{ "code": "auto init = [](){\n ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);\n return 'c';\n}();\nclass Solution {\npublic:\n int maxSubarrayLength(vector<int>& nums, int k) {\n const int n = nums.size();\n \n int ans = 0;\n int head = 0;\n int tail = 0;\n map<in...
3,225
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p> <p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equ...
2
{ "code": "class Solution {\npublic:\n int maxSubarrayLength(vector<int>& nums, int k) {\n int i=0,j=0,maxi=0;\n map<int,int>m;\n while(j<nums.size()){\n m[nums[j]]++;\n if(m[nums[j]]<=k){\n maxi=max(maxi,j-i+1);\n }else{\n while(m...
3,225
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p> <p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equ...
2
{ "code": "class Solution {\npublic:\n int maxSubarrayLength(vector<int>& nums, int k) {\n int l = 0;\n int ans = 0;\n int n = nums.size();\n map<int, int> freq;\n\n for (int r = 0; r < n; r++) {\n freq[nums[r]]++;\n while (freq[nums[r]] > k) {\n ...
3,225
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p> <p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equ...
2
{ "code": "class Solution {\npublic:\n int maxSubarrayLength(vector<int>& nums, int k) {\n map<int, int> maps;\n for (int i = 0; i < nums.size(); i++) {\n maps[nums[i]]++;\n }\n for (auto it = maps.begin(); it != maps.end(); it++) {\n if (it->second >= k) it->secon...
3,225
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p> <p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equ...
3
{ "code": "class Solution {\npublic:\n int maxSubarrayLength(vector<int>& nums, int k) {\n map<int, int> frequency;\n int left = 0, res = 0;\n\n for (int right = 0; right < nums.size(); ++right) {\n frequency[nums[right]] += 1;\n while (frequency[nums[right]] > k) {\n ...
3,225
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p> <p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equ...
3
{ "code": "class Solution {\npublic:\n int maxSubarrayLength(vector<int>& nums, int k) {\n int n = nums.size();\n int l = 0;\n int r = 0;\n map<int,int> mp;\n int maxl = 0;\n while(r<n){\n mp[nums[r]]++;\n while(mp[nums[r]] > k){\n mp[n...
3,225
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p> <p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equ...
3
{ "code": "class Solution {\npublic:\n int maxSubarrayLength(vector<int>& nums, int k) {\n map<int, int> freqs;\n map<int, int> cnt;\n\n int i=0, j=0, n=nums.size();\n int ans = 0;\n for(i=0; i<n; i++) {\n ++cnt[nums[i]];\n ans = max(ans, i-j);\n ...
3,225
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p> <p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equ...
3
{ "code": "class Solution {\npublic:\n int maxSubarrayLength(vector<int>& nums, int k) {\n int i=0;\n int j=0;\n int ans = 0;\n map<int,int>m;\n while(j<nums.size()){\n int ch = nums[j];\n m[ch]++;\n if(m[ch]<=k){\n ans = max(ans , ...
3,225
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p> <p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equ...
3
{ "code": "class Solution {\npublic:\n int maxSubarrayLength(vector<int>& nums, int k) {\n int n = nums.size();\n int i = 0;\n int maxLength = 0;\n unordered_map<int, int> mp; \n\n for (int j = 0; j < n; ++j) {\n // Expand the window by including nums[j]\n m...
3,225
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p> <p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equ...
3
{ "code": "class Solution {\npublic:\n int maxSubarrayLength(vector<int>& nums, int k) {\n unordered_map<int,int>mp;\n int n = nums.size();\n int len = 1;\n int j = 0;\n\n for(int i = 0;i<n;i++){\n mp[nums[i]]++;\n\n while(mp[nums[i]] > k && j<=i){\n ...
3,225
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p> <p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equ...
3
{ "code": "class Solution {\npublic:\n int maxSubarrayLength(vector<int>& nums, int k) {\n int n = nums.size(), i, left = 0, maxlen = INT_MIN;\n unordered_map<int, int> mp;\n for(i = 0; i < n; i++) {\n mp[nums[i]]++;\n while(mp[nums[i]] > k) {\n mp[nums[lef...
3,225
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p> <p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equ...
3
{ "code": "class Solution {\npublic:\n int maxSubarrayLength(vector<int>& nums, int k) {\n unordered_map<int, int> freq;\n int left = 0, right = 0, maxLength = 0;\n while (right < nums.size()) {\n freq[nums[right]]++;\n while(freq[nums[right]] > k) {\n freq...
3,225
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p> <p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equ...
3
{ "code": "class Solution {\npublic:\n int maxSubarrayLength(vector<int>& nums, int k) {\n int n = nums.size();\n\n unordered_map<int,int> mp;\n\n int ans = 0;\n\n int l=0,r=0;\n\n while(r<n){\n mp[nums[r]]++;\n\n while(mp[nums[r]] > k){\n mp[...
3,225
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p> <p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equ...
3
{ "code": "class Solution {\npublic:\n int maxSubarrayLength(vector<int>& nums, int k) {\n int l = 0;\n int r= 0 ;\n int ans = 0;\n unordered_map<int,int>hash;\n int mostRepeated = -1;\n while(r<nums.size()){\n hash[nums[r]]++;\n mostRepeated = (mostR...
3,225
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p> <p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equ...
3
{ "code": "class Solution {\npublic:\n int maxSubarrayLength(vector<int>& nums, int k) {\n unordered_map<int, int> dict;\n int left = 0, answer = 0;\n\n for (int right = 0; right < nums.size(); right++)\n {\n int num = nums[right];\n dict[num]++;\n\n whi...
3,225
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p> <p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equ...
3
{ "code": "class Solution {\npublic:\n // bool help(unordered_map<long long,int> mp,int k){\n // for(auto it: mp){\n // if(it.second>k){\n // return true;\n // }\n // }\n // return false;\n // }\n int maxSubarrayLength(vector<int>& nums, int k) {\n ...
3,225
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p> <p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equ...
3
{ "code": "class Solution {\npublic:\n // bool help(unordered_map<long long,int> mp,int k){\n // for(auto it: mp){\n // if(it.second>k){\n // return true;\n // }\n // }\n // return false;\n // }\n int maxSubarrayLength(vector<int>& nums, int k) {\n ...
3,225
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p> <p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equ...
3
{ "code": "class Solution {\npublic:\n int maxSubarrayLength(vector<int>& nums, int k) {\n int n = nums.size();\n unordered_map<long long, long long> mp;\n int l = 0, r = 0;\n int ans = INT_MIN;\n while(r < n) {\n if(mp[nums[r]] < k) {\n mp[nums[r++]]++;...
3,225
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p> <p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equ...
3
{ "code": "class Solution {\nprivate:\n unordered_map<int, int> cnt_num{};\n map<int, int> cnt_freq{};\n\npublic:\n int maxSubarrayLength(vector<int>& nums, int k) {\n int i = 0, j = 0, result = 0;\n\n while (j < nums.size()) {\n int old_freq = cnt_num[nums[j]];\n if (cnt_...
3,225
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p> <p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equ...
3
{ "code": "class Solution {\npublic:\n int maxSubarrayLength(vector<int>& arr, int k) {\n ios_base::sync_with_stdio(false);\ncin.tie(NULL);\n int n = arr.size();\n if (n == 0) return 0;\n\n int maxi = 0;\n int left = 0;\n int right = 0;\n int maxiiele = -1;\n ...
3,225
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p> <p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equ...
3
{ "code": "class Solution {\npublic:\n int maxSubarrayLength(vector<int>&v, int k) {\n int n=v.size();\n map<int,int>mp;\n int i=0,j=0,len=0;\n while(j<n){\n mp[v[j]]++;\n while(mp[v[j]]>k){\n mp[v[i]]--;\n if(mp[v[i]]==0){\n ...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
0
{ "code": "class Solution {\npublic:\n int numberOfSets(int n, int maxDistance, vector<vector<int>>& roads) {\n int ans = 0;\n for (int mask = 0; mask < 1 << n; ++mask) {\n int g[n][n];\n memset(g, 0x3f, sizeof(g));\n for (auto& e : roads) {\n int u = e...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
0
{ "code": "class Solution {\npublic:\n int numberOfSets(int n, int maxDistance, vector<vector<int>>& roads) {\n int ans = 0;\n for (int mask = 0; mask < 1 << n; ++mask) {\n int g[n][n];\n memset(g, 0x3f, sizeof(g));\n for (auto& e : roads) {\n int u = e...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
0
{ "code": "class Solution {\npublic:\n\n int Max_Distance(std::vector<std::vector<std::pair<int, int>>>& graph, int node, int removed_nodes){\n \n if(removed_nodes & (1 << node)) return 0;\n\n std::vector<int> distance_cache(graph.size(), INT_MAX);\n std::priority_queue<std::pair<int, i...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
0
{ "code": "class Solution {\npublic:\n int numberOfSets(int n, int maxDistance, vector<vector<int>>& roads) {\n vector<vector<pair<int,int>>> adjList(n);\n for(auto &vec:roads){\n int f = vec[0];\n int s = vec[1];\n int w = vec[2];\n adjList[f].push_back({s...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
0
{ "code": "class Solution {\npublic:\n int numberOfSets(int n, int maxDistance, vector<vector<int>>& roads) {\n \n vector<vector<pair<int,int>>> adjList(n);\n for(auto &vec:roads){\n int f = vec[0];\n int s = vec[1];\n int w = vec[2];\n adjList[f].pu...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
0
{ "code": "class Solution {\npublic:\n\n bool check(vector<int> &a1, vector<vector<pair<int, int>>>&adj, int &maxDistance){\n for(int i=0;i<a1.size();i++){\n if(a1[i]){\n vector<int> vis(a1.size(), INT_MAX);\n priority_queue<pair<int,int>, vector<pair<int,int>>, grea...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
0
{ "code": "class Solution {\npublic:\n\n const int INF = 1e9;\n int bitNodeMap[10];\n\n bool floydWarshall(vector<vector<int>>& edges, int n, int mask, int maxDistance) {\n int nodes = 0;\n for (int b = 0; b < n; b++) {\n bitNodeMap[b] = (mask & (1 << b)) ? nodes++ : -1;\n }\n...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
0
{ "code": "class Solution {\npublic:\n\n const int INF = 1e9;\n int bitNodeMap[10];\n\n bool floydWarshall(vector<pair<int, int>> adj[], int n, int mask, int maxDistance) {\n int nodes = 0;\n for (int b = 0; b < n; b++) {\n bitNodeMap[b] = (mask & (1 << b)) ? nodes++ : -1;\n }...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
0
{ "code": "class Solution {\npublic:\n bool shortestPath(vector<vector<pair<int, int>>>& adj, int maxDistance, int mask, int src){\n int n=adj.size();\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;\n vector<int> dist(n, 1e9);\n\n dist[src] = 0;\n ...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
0
{ "code": "class Solution {\npublic:\n int numberOfSets(int n, int mxd, vector<vector<int>>& edges) {\n const int oo = 1e7;\n vector<vector<int>> w(n,vector<int>(n,oo));\n vector<vector<int>> adj(n);\n for(auto &ed:edges)\n {\n int u = ed[0],v = ed[1],wt = ed[2];\n ...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
0
{ "code": "class Solution {\npublic:\n int bfs(int nod,vector<int>&vis,vector<vector<pair<int,int>>>&graph,int n,int bit){\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;\n pq.push({0,nod});\n int ans=0;\n while(pq.size()){\n auto p =pq.top();\...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
0
{ "code": "class Solution {\npublic:\n bool helperCheck(int n, int maxDistance, int mask,vector<pair<int, int>> graph[]) {\n for (int i = 0; i < n; i++) {\n if (mask & (1 << i))\n continue;\n for (int j = i + 1; j < n; j++) {\n if (mask & (1 << j))\n ...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
0
{ "code": "#include <vector>\n#include <iostream>\n#include <queue>\nusing namespace std;\n\nclass Solution {\npublic:\n int numberOfSets(int n, int maxDistance, vector<vector<int>>& roads) {\n // Step 1: Graph creation\n // Create a graph represented as an adjacency list\n vector<vector<pair<...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
0
{ "code": "int coc;\nbool dijk(int a,int &bitmask,vector<vector<pair<int,int>>>&v,vector<int>&dist)\n{\n set<pair<int,int>>s;\n s.insert({0,a});\n dist[a]=0;\n while(!s.empty())\n {\n auto now=*s.begin();\n int path=now.first;\n int node=now.second;\n s.erase(s.begin());\n ...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
0
{ "code": "class Solution {\npublic:\n int numberOfSets(int n, int maxDistance, vector<vector<int>>& roads) {\n bool flag=1;\n vector<vector<int>> adj(n,vector<int>(n,INT_MAX));\n\n for(auto &i:roads){\n adj[i[0]][i[1]]=adj[i[1]][i[0]]=min(adj[i[1]][i[0]],i[2]);\n }\n\n ...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
0
{ "code": "class Solution {\npublic:\n int numberOfSets(int n, int maxDistance, vector<vector<int>>& roads) {\n const int INF = 1e9;\n int ret = 0;\n vector<vector<int>> init(n, vector<int>(n, INF));\n for (auto &x: roads)\n {\n int u = x[0], v = x[1];\n ini...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
0
{ "code": "class Solution {\npublic:\nint pre[10];\nint ans=0;\nvector<vector<int>> road;\nint maxDistanco;\nint no;\nvoid dfs(int a,int b)\n{\n \n if(a==no)\n {\n // cout<<b<<endl;\n vector<pair<int,int>> graph[no];\n for(int i=0;i<road.size();i++)\n {\n if((b&(1<<road...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
0
{ "code": "class Solution {\npublic:\n int numberOfSets(int n, int maxDistance, vector<vector<int>>& roads) {\n int fin = 0;\n\n vector<vector<pair<int,int>>> E(n);\n\n for(auto u: roads){\n E[u[0]].push_back({u[1], u[2]});\n E[u[1]].push_back({u[0], u[2]});\n }\n\...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
0
{ "code": "// Idea: use bitset/bitmask to iterate through the power-set 2^n:\n// just increment m from 0 to 2^n.\n// for each mask, use Floyd-Warshall O(n^3) to compute the all-pairs\n// shortest paths in the subgraph imposed by the mask.\n// then gather the max distance in the sub-graph\nclass Solution {\n static...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
0
{ "code": "class Solution {\npublic:\n int getMaxDistance(int mask, int n, vector<vector<int>> d /* copy */) {\n int res = 0;\n for (int k = 0; k < n; ++k) // Floyd-Warshall\n if (mask & (1 << k))\n for (int i = 0; i < n; ++i)\n if (i != k && mask & (1 << ...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
0
{ "code": "// Idea: use bitset/bitmask to iterate through the power-set 2^n:\n// just increment m from 0 to 2^n.\n// for each mask, use Floyd-Warshall O(n^3) to compute the all-pairs\n// shortest paths in the subgraph imposed by the mask.\n// then gather the max distance in the sub-graph\nclass Solution {\n static...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
0
{ "code": "// Idea: use bitset/bitmask to iterate through the power-set 2^n:\n// just increment m from 0 to 2^n.\n// for each mask, use Floyd-Warshall O(n^3) to compute the all-pairs\n// shortest paths in the subgraph imposed by the mask.\n// then gather the max distance in the sub-graph\nclass Solution {\n static...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
0
{ "code": "class Solution {\npublic:\n#define vvi vector<vector<int>>\n\nint check(int mask, vvi des)\n{\n int n=des.size();\n for(int k=0;k<n;k++)\n {\n if(mask&(1<<k))\n {\n for(int i=0;i<n;i++)\n {\n if(i!=k && mask&(1<<i))\n {\n ...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
0
{ "code": "class Solution {\npublic:\n const int INF=INT_MAX/4;\n int N,maxd;\n void ShortestPath(vector<vector<int>> &m)\n {\n for(int i=0;i<N;++i)\n {\n m[i][i]=0;\n }\n for(int k=0;k<N;++k)\n {\n for(int i=0;i<N;++i)\n {\n ...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
0
{ "code": "#include <climits> // For LLONG_MAX\n\nclass Solution {\npublic:\n int numberOfSets(int n, int maxDistance, vector<vector<int>>& roads) {\n int maxi = (1 << n);\n int ans = 0;\n\n // Initialize distance matrix\n vector<vector<int>> dis(n, vector<int>(n, 100000));\n for...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
0
{ "code": "class Solution {\npublic:\n vector<vector<int>>mat;\n // unordered_map<int,int>dp;\n int count=0;\n\n int floyd(int bit){\n vector<vector<int>>gr=mat;\n int n=mat.size();\n for(int i=0; i<n; i++){\n for(int j=0; j<n; j++){\n if((bit&(1<<i)) || (bit...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
1
{ "code": "class Solution {\npublic:\n int numberOfSets(int n, int maxDistance, vector<vector<int>>& roads) {\n int result = 0;\n\n //O(2^n * (n^3))\n for(int set = 0; set < (1 << n); set++) { //trying all possible subsets\n\n vector<vector<int>> grid(n, vector<int>(n, 1e9)); //upda...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
1
{ "code": "class Solution {\npublic:\n int numberOfSets(int n, int maxDistance, vector<vector<int>>& roads) {\n int ans = 0;\n for(int mask = 0; mask < (1 << n); mask++){\n vector<vector<int>> distance(n, vector<int>(n, 1e7));\n for(int branch = 0; branch < n; branch++){\n ...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
1
{ "code": "class Solution {\npublic:\n int solve(map<pair<int,int>,int>&m,int mask,int n){\n vector<vector<int>>v(n,vector<int>(n,1e9));\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n if(i==j) {v[i][j] = 0; continue;}\n if( ( (mask>>i)&1 )==1 ) continue;\n ...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
1
{ "code": "class Solution {\npublic:\n bool fun(vector<int>&ds,int n,int maxDistance,vector<vector<int>>&roads)\n {\n vector<vector<int>>matrix(n,vector<int>(n,INT_MAX));\n for (const auto& road : roads) {\n int u = road[0];\n int v = road[1];\n int w = road[2];\n\n...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
1
{ "code": "#define check(n,i) (((n)>>(i))&1LL)\n\nclass Solution {\nprivate:\nint get_diameter(vector<vector<int>> dist, vector<int> &indices) {\n for (auto &k:indices) for (auto &i:indices) for (auto &j:indices) \n if (dist[i][j]>dist[i][k]+dist[k][j])\n dist[i][j]=dist[i][k]+dist[k][j];\n \...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
1
{ "code": "class Solution {\npublic:\n int numberOfSets(int n, int md, vector<vector<int>>& roads) {\n int ans = 0, time = (1<<n);\n vector<vector<int>> dis(n, vector<int>(n, 1e9));\n for (int i = 0; i < n; i++) dis[i][i] = 0;\n for (auto& it : roads) {\n int u = it[0], v = i...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
1
{ "code": "class Solution {\npublic:\n int FinalAns = 0;\n int MaxDist;\n\n void check(vector<vector<int>>& srtDist, int n, string& nodes) {\n // Disable nodes not in the current subset\n vector<vector<int>> dist = srtDist;\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j <...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
1
{ "code": "class Solution {\npublic:\nbool floyd(int n, unordered_set<int> &remove,vector<vector<int>> dist, int maxi)\n{\n for(int k=0;k<n;k++)\n {\n if(remove.find(k)!=remove.end()) continue;\n for(int i=0;i<n;i++)\n {\n if(remove.find(i)!=remove.end()) continue;\n f...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
2
{ "code": "class Solution {\npublic:\n int numberOfSets(int n, int maxDistance, vector<vector<int>>& roads) {\n valid = vector<int>(n);\n\n return GenerateSubsequences(0, n, roads, maxDistance);\n }\nprivate:\n vector<int> valid;\n\n int GenerateSubsequences(int i, int n, vector<vector<int>>...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
2
{ "code": "class Solution {\npublic:\n int numberOfSets(int n, int maxDistance, vector<vector<int>>& roads) {\n valid = vector<int>(n);\n\n return GenerateSubsequences(0, n, roads, maxDistance);\n }\nprivate:\n vector<int> valid;\n\n int GenerateSubsequences(int i, int n, vector<vector<int>>...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
2
{ "code": "int res = 0;\nint inf = 1e9;\nclass Solution {\npublic:\n int Distance(int n, vector<int>& cur_nodes, vector<vector<int>> dis) {\n for (auto k : cur_nodes) {\n for (auto i : cur_nodes) {\n for (auto j : cur_nodes) {\n dis[i][j] = min(dis[i][j], dis[i][...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
2
{ "code": "class Solution {\npublic:\n vector<int> a;\n int ans = 0;\n void rec(int node, vector<vector<int>>& roads, int maxdist, int n) {\n if (node == n)\n return;\n\n a.push_back(node);\n // for(auto it:a)cout<<it<<\" \";\n // cout<<endl;\n\n int maxi = 0;\n ...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
2
{ "code": "class Solution {\npublic:\n void func(int i, int n, map<pair<int,int>,int> &mp, set<int> &v, int &ans, int &maxDistance){\n if(i==n){\n vector<vector<int>> dp(n,vector<int> (n,1e9));\n for(int i=0; i<n; i++){\n for(int j=i+1; j<n; j++){\n if...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
2
{ "code": "class Solution {\npublic:\n void generateSubsets(std::vector<std::vector<int>>& result, std::vector<int>& subset, int index, int n) {\n if (index == n) {\n result.push_back(subset);\n return;\n }\n\n // Exclude the current element\n generateSubsets(resul...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
2
{ "code": "class Solution {\npublic:\n bool canForm(int mask, vector<vector<long long>> &adj, int maxDistance){\n int n = adj.size();\n vector<vector<long long>> dist = adj;\n long long maxm = 0;\n \n for(int i = 0;i<n;i++){\n if((mask>>i & 1)) continue;\n f...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
2
{ "code": "class Solution {\npublic:\n int solve(vector<vector<long long>> dist,int mask,int n){\n for(int k=0;k<n;k++){\n if(mask&(1<<k)){\n for(int i=0;i<n;i++){\n if(mask&(1<<i)){\n for(int j=0;j<n;j++){\n if(m...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
2
{ "code": "class Solution {\n typedef long long ll;\n\npublic:\n int numberOfSets(int n, int maxDistance, vector<vector<int>>& roads) {\n int result = 0;\n for (int set = 0; set < (1 << n); set++) {\n vector<vector<ll>> matrix(n, vector<ll>(n, INT_MAX));\n\n for (auto& it : r...
3,217
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p> <p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, t...
2
{ "code": "class Solution {\npublic:\n int numberOfSets(int n, int maxDistance, vector<vector<int>>& roads) {\n vector<pair<int,int>>adj[n];\n for(auto it:roads){\n adj[it[0]].push_back({it[1],it[2]});\n adj[it[1]].push_back({it[0],it[2]});\n }\n int ans=0;\n ...