id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
3,479
<p>You are given a binary string <code>s</code>.</p> <p>Return the number of <span data-keyword="substring-nonempty">substrings</span> with <strong>dominant</strong> ones.</p> <p>A string has <strong>dominant</strong> ones if the number of ones in the string is <strong>greater than or equal to</strong> the <strong>sq...
3
{ "code": "class Solution {\npublic:\n int numberOfSubstrings(string s) {\n int n = s.size(), res = 0, k = 0;\n\n for (int i = 1; i * i <= n; i++) {\n deque<int> z; \n int lz = -1, o = 0; \n for (int j = 0; j < n; j++) {\n if (s[j] == '0') {\n ...
3,479
<p>You are given a binary string <code>s</code>.</p> <p>Return the number of <span data-keyword="substring-nonempty">substrings</span> with <strong>dominant</strong> ones.</p> <p>A string has <strong>dominant</strong> ones if the number of ones in the string is <strong>greater than or equal to</strong> the <strong>sq...
3
{ "code": "class Solution {\npublic:\n int numberOfSubstrings(string s) {\n int n = s.size();\n int NO = ceil(sqrt(n));\n\n vector<int> prefix(n, 0); // prefix 1\n for (int i = 0; i < n; i++) {\n prefix[i] = (s[i] == '1' ? 1 : 0) + (i == 0 ? 0 : prefix[i-1]);\n }\n\n ...
3,479
<p>You are given a binary string <code>s</code>.</p> <p>Return the number of <span data-keyword="substring-nonempty">substrings</span> with <strong>dominant</strong> ones.</p> <p>A string has <strong>dominant</strong> ones if the number of ones in the string is <strong>greater than or equal to</strong> the <strong>sq...
3
{ "code": "class Solution {\npublic:\n int numberOfSubstrings(string s) {\n int n = s.size();\n int NO = ceil(sqrt(n));\n\n vector<int> prefix(n, 0); // prefix 1\n for (int i = 0; i < n; i++) {\n prefix[i] = (s[i] == '1' ? 1 : 0) + (i == 0 ? 0 : prefix[i-1]);\n }\n\n ...
3,479
<p>You are given a binary string <code>s</code>.</p> <p>Return the number of <span data-keyword="substring-nonempty">substrings</span> with <strong>dominant</strong> ones.</p> <p>A string has <strong>dominant</strong> ones if the number of ones in the string is <strong>greater than or equal to</strong> the <strong>sq...
3
{ "code": "class Solution {\npublic:\n int numberOfSubstrings(string s) {\n int n = s.size();\n int cnt = 0;\n vector<int> pref(n+5);\n int sum = 0;\n for (int g=0; g<s.size(); g++){\n sum+=(s[g] == '1') ? 1 : 0;\n pref[g] = sum;\n }\n for (int...
3,479
<p>You are given a binary string <code>s</code>.</p> <p>Return the number of <span data-keyword="substring-nonempty">substrings</span> with <strong>dominant</strong> ones.</p> <p>A string has <strong>dominant</strong> ones if the number of ones in the string is <strong>greater than or equal to</strong> the <strong>sq...
3
{ "code": "class Solution {\npublic:\n int helper(int z, string &s, map<int,int> &m){\n int ans=0,prev=-1,one=0,n=s.length();\n deque<int> q;\n\n for(int i=0;i<s.length();i++){\n if(s[i]=='0'){\n q.push_back(i);\n if(q.size()==z+1){\n ...
3,479
<p>You are given a binary string <code>s</code>.</p> <p>Return the number of <span data-keyword="substring-nonempty">substrings</span> with <strong>dominant</strong> ones.</p> <p>A string has <strong>dominant</strong> ones if the number of ones in the string is <strong>greater than or equal to</strong> the <strong>sq...
3
{ "code": "class Solution {\npublic:\n int numberOfSubstrings(string s) {\n vector<long long> need(100000);\n for (int i = 1; i < 100000; i++)\n need[i] = 1ll * i * i + i;\n vector<int> zero;\n for (int i = 0; i < s.size(); i++) {\n if (s[i] == '0') zero.push_back(i);\n }\n i...
3,479
<p>You are given a binary string <code>s</code>.</p> <p>Return the number of <span data-keyword="substring-nonempty">substrings</span> with <strong>dominant</strong> ones.</p> <p>A string has <strong>dominant</strong> ones if the number of ones in the string is <strong>greater than or equal to</strong> the <strong>sq...
3
{ "code": "class Solution {\npublic:\n int numberOfSubstrings(string s) {\n int n=s.size(),ans=0;\n int co=0;\n for(int i=0;i<n;i++){\n if(s[i]=='0'){\n ans+=(co*(co+1))/2;\n co=0;\n } else co++;\n } ans+=(co*(co+1))/2;\n int xx...
3,479
<p>You are given a binary string <code>s</code>.</p> <p>Return the number of <span data-keyword="substring-nonempty">substrings</span> with <strong>dominant</strong> ones.</p> <p>A string has <strong>dominant</strong> ones if the number of ones in the string is <strong>greater than or equal to</strong> the <strong>sq...
3
{ "code": "class Solution {\npublic:\n int numberOfSubstrings(string s) {\n int n=s.size();\n vector<int> zeros(1e5,-2);\n vector<int> ones(1e5,-2);\n \n zeros[0]=-1;\n ones[0]=-1;\n \n int z=0;\n int o=0;\n \n for(int i=0;i<s.size();i++)...
3,479
<p>You are given a binary string <code>s</code>.</p> <p>Return the number of <span data-keyword="substring-nonempty">substrings</span> with <strong>dominant</strong> ones.</p> <p>A string has <strong>dominant</strong> ones if the number of ones in the string is <strong>greater than or equal to</strong> the <strong>sq...
3
{ "code": "class Solution {\npublic:\n static const int maxn = 4e4, maxv = 2e2;\n vector<int> pre = vector<int>(maxn+5);\n vector<int> nex = vector<int>(maxn+5);\n int numberOfSubstrings(string s) {\n int n = s.size();\n int las = 0;\n for(int i = 1; i <= n; ++i) {\n if(s[i...
3,479
<p>You are given a binary string <code>s</code>.</p> <p>Return the number of <span data-keyword="substring-nonempty">substrings</span> with <strong>dominant</strong> ones.</p> <p>A string has <strong>dominant</strong> ones if the number of ones in the string is <strong>greater than or equal to</strong> the <strong>sq...
3
{ "code": "class Solution {\npublic:\n int numberOfSubstrings(string s) {\n int n = s.size(), cnt0 = 0, cnt1 = 0, ans = 0;\n vector<int> mp0(40001, -2), mp1(40001, -2);\n mp0[0] = -1;\n // for(int i = 0; i < n; i++){\n // if(s[i] == '1'){\n // int j = i, cnt = ...
3,479
<p>You are given a binary string <code>s</code>.</p> <p>Return the number of <span data-keyword="substring-nonempty">substrings</span> with <strong>dominant</strong> ones.</p> <p>A string has <strong>dominant</strong> ones if the number of ones in the string is <strong>greater than or equal to</strong> the <strong>sq...
3
{ "code": "class Solution {\n int ansForKzeros(const string s, const int K){\n const int N = s.size();\n queue<int> zeroPos;\n int i=0, j=0, ones=0, zeros=0, res = 0;\n while(j<N){\n if(s[j]=='0'){\n zeroPos.push(j);\n zeros++;\n } els...
3,479
<p>You are given a binary string <code>s</code>.</p> <p>Return the number of <span data-keyword="substring-nonempty">substrings</span> with <strong>dominant</strong> ones.</p> <p>A string has <strong>dominant</strong> ones if the number of ones in the string is <strong>greater than or equal to</strong> the <strong>sq...
3
{ "code": "class Solution {\npublic:\n int fun(int x,string &str){\n int n=str.size();\n vector<int> pos;\n for(int i=0;i<n;i++){\n if(str[i]=='0')pos.push_back(i);\n }\n int ans=0;\n int minlen=x+x*x;\n for(int i=0;i+x-1<pos.size();i++){\n int...
3,479
<p>You are given a binary string <code>s</code>.</p> <p>Return the number of <span data-keyword="substring-nonempty">substrings</span> with <strong>dominant</strong> ones.</p> <p>A string has <strong>dominant</strong> ones if the number of ones in the string is <strong>greater than or equal to</strong> the <strong>sq...
3
{ "code": "class Solution {\npublic:\n int numberOfSubstrings(string s) {\n int res = 0;\n for (int k = 1; k * k < s.size(); ++k) {\n std::queue<size_t> zeros;\n int left = 0;\n int right = 0;\n int last_zero = -1;\n while (right < s.size()) {\n ...
3,479
<p>You are given a binary string <code>s</code>.</p> <p>Return the number of <span data-keyword="substring-nonempty">substrings</span> with <strong>dominant</strong> ones.</p> <p>A string has <strong>dominant</strong> ones if the number of ones in the string is <strong>greater than or equal to</strong> the <strong>sq...
3
{ "code": "class Solution {\npublic:\n int numberOfSubstrings(string s) {\n int n = s.size(), res = 0, k = 0;\n\n for(long long int i=1;i*i<=n;i++)\n {\n deque<long long int> z;\n long long int last_zero=-1, ones=0;\n for(long long int j=0;j<n;j++)\n ...
3,479
<p>You are given a binary string <code>s</code>.</p> <p>Return the number of <span data-keyword="substring-nonempty">substrings</span> with <strong>dominant</strong> ones.</p> <p>A string has <strong>dominant</strong> ones if the number of ones in the string is <strong>greater than or equal to</strong> the <strong>sq...
3
{ "code": "/*\n\tJAI JAGANNATH!\n*/\n//@Author : zanj0\n\n#include<bits/stdc++.h>\nusing namespace std;\n\n#define ff first\n#define ss second\n#define pb push_back\n#define MOD 1000000007\n#define inf 1e18\n\ntypedef long long int lli;\n\nclass Solution ...
3,479
<p>You are given a binary string <code>s</code>.</p> <p>Return the number of <span data-keyword="substring-nonempty">substrings</span> with <strong>dominant</strong> ones.</p> <p>A string has <strong>dominant</strong> ones if the number of ones in the string is <strong>greater than or equal to</strong> the <strong>sq...
3
{ "code": "#include <bits/stdc++.h>\nusing namespace std;\n#define fo(i, n) for (ll i = 0; i < n; i++)\n#define Fo(i, a, n) for (int i = a; i < n; i++)\n#define ll long long\n#define deb(x) cout << #x << \"=\" << x << endl\n#define deb2(x, y) cout << #x << \"=\" << x << \",\" << #y << \"=\" << y << endl\n#define pb p...
3,479
<p>You are given a binary string <code>s</code>.</p> <p>Return the number of <span data-keyword="substring-nonempty">substrings</span> with <strong>dominant</strong> ones.</p> <p>A string has <strong>dominant</strong> ones if the number of ones in the string is <strong>greater than or equal to</strong> the <strong>sq...
3
{ "code": "const int N=200;\nconst int M=4e4;\nint p[M+1][N+1];\nclass Solution {\npublic:\n int numberOfSubstrings(string s) {\n int n=s.size();\n int ans=0;\n for (int cnt=0;cnt<=N;cnt++){\n int i=0,j=0;\n queue <int> q;\n int f=0;\n while (j<n){\n...
3,479
<p>You are given a binary string <code>s</code>.</p> <p>Return the number of <span data-keyword="substring-nonempty">substrings</span> with <strong>dominant</strong> ones.</p> <p>A string has <strong>dominant</strong> ones if the number of ones in the string is <strong>greater than or equal to</strong> the <strong>sq...
3
{ "code": "class Solution {\npublic:\nint n;\nint find(int c,string &s){\n int l=0;\n int r=0;\nint cur=0;\nint ans=0;\nqueue<int> q;\nwhile(r<n){\nif(s[r]=='0'){\n cur++;\n q.push(r);\n}\nwhile(cur>c){\nif(s[l]=='0'){\n q.pop();\n cur--;\n}\nl++;\n}\nif(cur==c&&(r-l+1-cur)>=(c)*(c)){\nint ml=(r-l+1...
3,479
<p>You are given a binary string <code>s</code>.</p> <p>Return the number of <span data-keyword="substring-nonempty">substrings</span> with <strong>dominant</strong> ones.</p> <p>A string has <strong>dominant</strong> ones if the number of ones in the string is <strong>greater than or equal to</strong> the <strong>sq...
3
{ "code": "typedef int ll;\n\nclass Solution {\npublic:\n int numberOfSubstrings(string s) {\n ll n = s.size();\n ll oc = 0;\n\n vector<ll> pre(n+1, 0);\n for (ll i = 0; i < n; i++) {\n pre[i+1] = pre[i] + 1 - (s[i]-'0');\n oc+=(s[i] - '0');\n }\n ll ...
3,479
<p>You are given a binary string <code>s</code>.</p> <p>Return the number of <span data-keyword="substring-nonempty">substrings</span> with <strong>dominant</strong> ones.</p> <p>A string has <strong>dominant</strong> ones if the number of ones in the string is <strong>greater than or equal to</strong> the <strong>sq...
3
{ "code": "class Solution {\nprivate:\n queue<int> Q;\npublic:\n int numberOfSubstrings(string s) {\n int ret = 0;\n int n = (int)s.length();\n Q.push(-1);\n for(int i=0;i<n;i++){\n if(s[i]=='0'){\n Q.push(i);\n if((int)Q.size() > 201){\n ...
3,479
<p>You are given a binary string <code>s</code>.</p> <p>Return the number of <span data-keyword="substring-nonempty">substrings</span> with <strong>dominant</strong> ones.</p> <p>A string has <strong>dominant</strong> ones if the number of ones in the string is <strong>greater than or equal to</strong> the <strong>sq...
3
{ "code": "class Solution {\nprivate:\n queue<int> Q;\npublic:\n int numberOfSubstrings(string s) {\n int ret = 0;\n int n = (int)s.length();\n \n Q.push(-1);\n for(int i=0;i<n;i++){\n if(s[i]=='0'){\n Q.push(i);\n if((int)Q.size() > 20...
3,479
<p>You are given a binary string <code>s</code>.</p> <p>Return the number of <span data-keyword="substring-nonempty">substrings</span> with <strong>dominant</strong> ones.</p> <p>A string has <strong>dominant</strong> ones if the number of ones in the string is <strong>greater than or equal to</strong> the <strong>sq...
3
{ "code": "class Solution {\npublic:\n int numberOfSubstrings(string s) {\n int p = sqrt(s.size()) + 2;\n int l =0,ans = 0;;\n while(l < s.size()){\n if(s[l] == '0'){\n l++;\n } else {\n int r = l + 1;\n while(r < s.size() && s...
3,479
<p>You are given a binary string <code>s</code>.</p> <p>Return the number of <span data-keyword="substring-nonempty">substrings</span> with <strong>dominant</strong> ones.</p> <p>A string has <strong>dominant</strong> ones if the number of ones in the string is <strong>greater than or equal to</strong> the <strong>sq...
3
{ "code": "class Solution {\n int solve(string s,int spec)\n {\n int l=0,r=0;\n int zero=0,one=0;\n int n=s.size();\n vector<int> v;\n for(int i=0;i<s.size();i++)\n {\n if(s[i]=='0')\n v.push_back(i);\n }\n int cnt=0;\n int k=0...
3,479
<p>You are given a binary string <code>s</code>.</p> <p>Return the number of <span data-keyword="substring-nonempty">substrings</span> with <strong>dominant</strong> ones.</p> <p>A string has <strong>dominant</strong> ones if the number of ones in the string is <strong>greater than or equal to</strong> the <strong>sq...
3
{ "code": "class Solution {\npublic:\n int numberOfSubstrings(const string& s) {\n int result = 0;\n vector<vector<int>> cnt_zero(s.size() + 1);\n cnt_zero[0] = {0};\n int cnt = 0;\n for (int i = 1; i <= s.size(); ++i) {\n cnt += s[i - 1] == '0';\n cnt_zero[...
3,479
<p>You are given a binary string <code>s</code>.</p> <p>Return the number of <span data-keyword="substring-nonempty">substrings</span> with <strong>dominant</strong> ones.</p> <p>A string has <strong>dominant</strong> ones if the number of ones in the string is <strong>greater than or equal to</strong> the <strong>sq...
3
{ "code": "class Solution {\npublic:\nint numberOfSubstrings(string &s)\n{\n int n = s.size();\n int ans = 0 ; \n vector<int> zeroes ; \n\n for(int i = 0 ; i < n ; i++){\n if(s[i] == '0'){\n zeroes.push_back(i) ; \n }\n }\n\n if(zeroes.size() == 0){\n return n * (n + ...
3,479
<p>You are given a binary string <code>s</code>.</p> <p>Return the number of <span data-keyword="substring-nonempty">substrings</span> with <strong>dominant</strong> ones.</p> <p>A string has <strong>dominant</strong> ones if the number of ones in the string is <strong>greater than or equal to</strong> the <strong>sq...
3
{ "code": "class Solution {\npublic:\n int numberOfSubstrings(string s) {\n int n = s.length();\n int cnt = 0;\n \n vector<int> zro;\n for (int i = 0; i < n; i ++)\n if (s[i] == '0') zro.push_back(i);\n \n if (zro.empty()) return n*(n+1)/2;\n int z...
3,479
<p>You are given a binary string <code>s</code>.</p> <p>Return the number of <span data-keyword="substring-nonempty">substrings</span> with <strong>dominant</strong> ones.</p> <p>A string has <strong>dominant</strong> ones if the number of ones in the string is <strong>greater than or equal to</strong> the <strong>sq...
3
{ "code": "// class Solution {\n// public:\n// int numberOfSubstrings(string s) {\n// int n = s.size(), counter = 0;\n// vector<int> zeros;\n\n// for (int i = 0; i < n; ++i) {\n// if (s[i] == '0') {\n// zeros.push_back(i);\n// }\n// }\n\n// ...
3,479
<p>You are given a binary string <code>s</code>.</p> <p>Return the number of <span data-keyword="substring-nonempty">substrings</span> with <strong>dominant</strong> ones.</p> <p>A string has <strong>dominant</strong> ones if the number of ones in the string is <strong>greater than or equal to</strong> the <strong>sq...
3
{ "code": "class Solution {\npublic:\n int numberOfSubstrings(string s) {\n int n = s.length();\n int cnt = 0;\n \n vector<int> zro;\n for (int i = 0; i < n; i ++)\n if (s[i] == '0') zro.push_back(i);\n \n if (zro.empty()) return n*(n+1)/2;\n int z...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
0
{ "code": "class Solution {\npublic:\n // do bfs everytime from the node is dist %3 == 0 add\n // number of valid nodes to the right * number of valid nodes to the left == number of connections through \n int dfs(int i, int p, int dist, int ss, vector<pair<int,int>> adjL[]){\n int cnt = dist>0 && dist...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
0
{ "code": "class Solution {\n int dfs(vector<pair<int,int> > adj[],int node,int par,int dist,int speed){\n int ans=0;\n if(dist%speed==0){\n ans++;\n }\n for(auto nbr:adj[node]){\n int nnode=nbr.first;\n if(nnode==par){\n continue;\n ...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
1
{ "code": "class Solution {\npublic:\n void dfs(vector<pair<long long, long long>> adj[], long long node, long long p, long long& count,\n long long dist, long ss) {\n if (dist % ss == 0) {\n count++;\n }\n for (auto x : adj[node]) {\n if (x.first != p) {\n ...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
1
{ "code": "class Solution {\npublic:\n int fun(int node,vector<pair<int,int>> adj[],int s,int par,int dis)\n {\n int sum=0;\n if(dis%s==0)\n {\n sum++;\n }\n for(auto i:adj[node])\n {\n if(i.first!=par)\n {\n sum+=fun(i.fi...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
2
{ "code": "class Solution {\npublic:\n void dfs(int node,int parent,int dis,int ss,unordered_map<int,vector<pair<int,int>>>&adj,int &connectable_node){\n if(dis % ss == 0) \n connectable_node++;\n // go to all child nodes\n for(auto [child,d] : adj[node]){\n if(child == parent) continue;...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
2
{ "code": "class Solution {\npublic:\n void dfs(int node,int parent,int dis,int ss,unordered_map<int,vector<pair<int,int>>>&adj,int &connectable_node){\n if(dis % ss == 0) \n connectable_node++;\n // go to all child nodes\n for(auto [child,d] : adj[node]){\n if(child == parent) continue;...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
2
{ "code": "class Solution {\npublic:\n \n int c;\n \n void dfs(vector<vector<pair<int,int>>> &adj, vector<bool>& vis, int s, int signal, int cur_sum)\n {\n vis[s]=true;\n if((cur_sum%signal)==0)\n {\n c++;\n }\n \n for(auto x: adj[s])\n {\n ...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
2
{ "code": "class Solution {\npublic:\n void dfs(int node, const unordered_map<int, vector<pair<int, int>>> & mp, int par, int & count, int dis, int signalspeed) {\n for(auto neighbor : mp.at(node)) {\n if(neighbor.first != par) {\n int edgeWeight = neighbor.second;\n ...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
2
{ "code": "class Solution {\npublic:\n int cnt=0;\n int div=0;\n \n void dfs(int u,int par,vector<vector<vector<int>>> &adj,int curr){\n \n if(curr%div==0)\n cnt++;\n \n for(auto &i:adj[u]){\n if(i[0]!=par)\n dfs(i[0],u,adj,curr+i[1]);\n ...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
2
{ "code": "class Solution {\npublic:\n\n void dfs(int x, vector<vector<pair<int, int>>> &edge, int anc, int dis, map<int, int> &mp, int sS, vector<bool> &vis) {\n if (dis % sS == 0) mp[anc]++;\n for(auto e: edge[x]) {\n int y = e.first, d = e.second;\n if (vis[y]) continue;\n ...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "// Time: O(n^2)\n// Space: O(n)\n\n// iterative dfs\nclass Solution {\npublic:\n vector<int> countPairsOfConnectableServers(vector<vector<int>>& edges, int signalSpeed) {\n vector<vector<pair<int, int>>> adj(size(edges) + 1);\n for (const auto& e : edges) {\n adj[e[0]].emplace_...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution {\npublic:\n vector<int> countPairsOfConnectableServers(vector<vector<int>>& edges, int signalSpeed) {\n \n /*\n do dfs from that node, and color the neighbors along with the distance\n */\n \n vector<int> ret;\n \n map<int, vector<i...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "vector<bool> vis;\nvector<int> cnt; // first - in, second - out\n\nclass Solution {\n int dfs(int u, int from, int len, long long cur, int speed, vector<vector<pair<int, int>>>& g) {\n vis[u] = 1;\n\n int c = 0, c1 = 0;\n for (auto& v : g[u]) {\n int to = v.first, w = v.s...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "\n#define pii pair<int,int> \nclass Solution {\n\n\nint helper( vector<vector<pii>> &adj, int node , int ss, int parent, int path){\n\n\n int currans = 0;\n if(path > 0 && path % ss == 0) currans = 1;\n \n int pairs = 0;\n\n for(auto it: adj[node]){\n\n int wt = it.second;\n i...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "\n#define pii pair<int,int> \nclass Solution {\n\n\nint helper( vector<vector<pii>> &adj, int node , int ss, int parent, int path){\n\n\n int currans = 0;\n if(path > 0 && path % ss == 0) currans = 1;\n\n \n \n int pairs = 0;\n\n for(auto it: adj[node]){\n\n int wt = it.second;\n ...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution {\npublic:\n int M;\n vector<int> vans;\n vector<map<int,int>> vm;\n vector<vector<array<int,2>>> edge;\n void dfs1(int curr,int parent)\n {\n int sum0=0;\n map<int,int> &mc=vm[curr];\n for(auto [i,len]:edge[curr])\n {\n if(i==parent) ...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "typedef long long ll;\n\nstruct Node {\n vector<pair<ll, ll>> adj;\n vector<ll> cd; \n};\n\n\nclass Solution {\npublic:\n int dfs(vector<Node> &nodes, int parent, int next, int w, int signalSpeed) {\n int cts = 0;\n for(int i = 0; i < nodes[next].adj.size(); i++) {\n int n...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution {\npublic:\n int signalSpeedx;\n int dfs(vector<pair<int, int>> adj[], int &node, vector<int> &vis, int dist){\n vis[node]=1;\n int ct=0;\n for(auto it:adj[node]){\n if(!vis[it.first]){\n int x = dfs(adj, it.first, vis, dist + it.second); ...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution {\npublic:\n void dfs(int src,vector<pair<int,int>>adj[],vector<int>&vis,int dis,int signal,int &ans){\n vis[src]=1;\n int count=0;\n if(dis%signal==0){\n count=1;\n }\n for(auto x:adj[src]){\n int node=x.first;\n int wei...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution {\npublic:\n // DFS to calculate distances from the main node to all other nodes and\n // DFS to count valid subtrees\n int countSubtrees(vector<vector<pair<int,int>>>& graph, int parent, int current, int signalSpeed, int mainNode, vector<vector<int>>& dist, int totalDistance) {\n ...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution {\npublic:\n int dfs(int node, int x, vector<int>& vis, vector<int>& count, int totaldist, int speed, vector<pair<int, int>> adj[]){\n vis[node]=1;\n int ans=0;\n for(auto& nn: adj[node]){\n int nei=nn.first, dist=nn.second;\n int cnt=0;\n ...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution {\npublic:\n int dfs(int node,int sum,int t,vector<int>&vis,vector<vector<pair<int, int>>>&adj){\n vis[node]=1;\n int cnt=0;\n if(sum%t==0)cnt++;\n for(auto child: adj[node]){\n if(vis[child.first])continue;\n cnt+=dfs(child.first,sum+chil...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution {\npublic:\n void dfs(vector<vector<pair<int,int>>>& adj,int node,vector<int>& visited,int dict,int sp,int *temp){\n if(dict%sp==0)\n *temp+=1;\n visited[node]=1;\n\n for(auto& itr:adj[node]){\n if(!visited[itr.first])\n dfs(adj,it...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution {\npublic:\n int maxi;\n int sig;\n int dfs(vector<vector<pair<int,int>>>&adj, int i,int path,vector<int> &vis){\n\n vis[i]=1;\n int ans=0;\n if(path%sig==0)ans++;\n for(auto it:adj[i]){\n if(vis[it.first]==0)\n ans+=dfs(adj,it.first...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution {\npublic:\n int dfs(vector<int>& s,vector<vector<pair<int,int>>>& adj,int node,int y,int distance,int signal,vector<int>& visited)\n {\n visited[node]=1;\n int total=0;\n for(auto i:adj[node])\n {\n if(!visited[i.first]){\n int a= dfs(s,...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution {\npublic:\n int dfs(vector<int>& store,vector<vector<pair<int,int>>>& adjlist,int node,int y,int distance,int signal,vector<int>& visited)\n {\n visited[node]=1;\n int total=0;\n for(auto i:adjlist[node])\n {\n if(!visited[i.first]){\n i...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution {\npublic:\n int dfs(vector<int>& store,vector<vector<pair<int,int>>>& adjlist,int node,int y,int distance,int signal,vector<int>& visited)\n {\n visited[node]=1;\n int total=0;\n for(auto i:adjlist[node])\n {\n if(!visited[i.first]){\n i...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution \n{\npublic:\n int dfs(int node, int x, vector<pair<int,int>> adj[], vector<int> &vis, int signalSpeed, int distance, vector<int> &count)\n {\n vis[node]=1;\n int ans=0;\n for(auto it: adj[node])\n {\n int neigh=it.first, dist=it.second;\n ...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution {\n int getDivisibleCount(std::vector<std::vector<int>>& v, std::vector<std::vector<int>>& weights, int cur, int prev, int curSum, int signalSpeed) {\n int cnt = 0;\n if ((curSum + weights[cur][prev]) % signalSpeed == 0) {\n cnt++;\n }\n for (int ver...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "#define ll long long\nclass Solution {\npublic:\n\n int dfs(int V , int pV,ll dis, vector<int>& cnt,vector<vector<vector<int>>>& adj, int k){\n int ans = 0;\n if(dis%k == 0)ans++;\n for(auto &e:adj[V]){\n int v = e[0], w = e[1];\n if(v == pV)continue;\n ...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution {\npublic:\n vector<vector<pair<int,int>>>gr;\n int signal;\n int n;\n\n int dfs(int node, int curcost, vector<int>&vis){\n int ans=0;\n if(curcost%signal==0) ans=1;\n\n for(auto x:gr[node]){\n int nextnode=x.first, cost=x.second;\n \n ...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution {\n int root,speed;\n vector<int>vis;\n void dfs(int node,int d,int count,vector<vector<pair<int,int>>>&adj, map<int,int>&m){\n vis[node]=1;\n if(node==root){\n int i=0;\n for(auto x:adj[node]){\n\n if(vis[x.first]) continue;\n ...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution {\nprivate:\n int dfs(int vertex,int time,int sum,vector<int>&vis,vector<vector<pair<int,int>>>&adj)\n {\n vis[vertex]=1;\n int cnt=0;\n if(sum%time==0) cnt++;\n for(auto child:adj[vertex])\n {\n if(!vis[child.first]) cnt+=dfs(child.first,t...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution {\npublic:\n int n;\n unordered_map<int, vector<vector<int>>> mp;\n vector<vector<int>> ss;\n void count(int src,int par,int d,int signal,int& cnt){\n if(d % signal == 0){\n cnt++;\n }\n\n for(auto &i:mp[src]){\n if(i[0] != par){\n ...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution {\npublic:\n int temp=0;\n void helper(vector<pair<int,int>> adj[],int node,int dis,vector<int> &vis,int sp){\n if(dis%sp==0){\n temp++;\n }\n vis[node]=1;\n for(auto a:adj[node]){\n if(vis[a.first]==0){\n helper(adj,a.fi...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution {\npublic:\n int temp=0;\n void helper(vector<pair<int,int>> adj[],int node,int dis,vector<int> &vis,int sp){\n if(dis%sp==0){\n temp++;\n }\n vis[node]=1;\n for(auto a:adj[node]){\n if(vis[a.first]==0){\n helper(adj,a.fi...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution {\npublic:\n int temp=0;\n void helper(vector<pair<int,int>> adj[],int node,int dis,vector<int> &vis,int sp){\n if(dis%sp==0){\n temp++;\n }\n vis[node]=1;\n for(auto a:adj[node]){\n if(vis[a.first]==0){\n helper(adj,a.fi...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution {\npublic:\n int temp=0;\n void helper(vector<pair<int,int>> adj[],int node,int dis,vector<int> &vis,int sp){\n if(dis%sp==0){\n temp++;\n }\n vis[node]=1;\n for(auto a:adj[node]){\n if(vis[a.first]==0){\n helper(adj,a.fi...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution {\npublic:\n int n;\n int sSpeed;\n map<int, vector<pair<int, int>>> mp;\n\n void dfs(pair<int, int> node, int& cnt, int sums, vector<int>& visited)\n {\n visited[node.first] = true;\n sums += node.second;\n if (sums%sSpeed == 0)\n {\n cn...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution {\npublic:\n unordered_map<int, vector<int>> adjacency;\n unordered_map<int, vector<int>> weights;\n\n vector<int> countPairsOfConnectableServers(vector<vector<int>>& edges, int signalSpeed) {\n int n = edges.size() + 1;\n if (n == 2) return {0, 0};\n vector<int...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution {\npublic:\n int n;\n int sSpeed;\n map<int, vector<pair<int, int>>> mp;\n\n void dfs(pair<int, int> node, int& cnt, int sums, vector<int>& visited)\n {\n visited[node.first] = true;\n sums += node.second;\n if (sums%sSpeed == 0)\n {\n cn...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution {\npublic:\n int n;\n int sSpeed;\n map<int, vector<pair<int, int>>> mp;\n\n void dfs(pair<int, int> node, int& cnt, int sums, vector<int>& visited)\n {\n visited[node.first] = true;\n sums += node.second;\n // cout << \"node: \" << node.first << \"---sum:...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution {\npublic:\n int n;\n void dfs(int i,int dist,vector<int>&vis,vector<vector<pair<int,int>>>&adj,int k,int &cnt){\n if(vis[i])return;\n if(dist%k==0)cnt++;\n vis[i]=1;\n for(int j=0;j<adj[i].size();j++){\n dfs(adj[i][j].first,dist+adj[i][j].second,...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution {\npublic:\nvector<int> ans;\nint dfs(int node,int dist,int x,vector<pair<int,int>> adj[],vector<int> &dp,int par,int fix)\n{\n if (dist%x==0){\n dp[node]=1;\n }\n else {\n dp[node]=0;\n }\n for (auto it:adj[node]){\n int fir=it.first;\n int sec=it....
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution {\npublic:\n void dfs(vector<vector<pair<int, int>>>& adj, int u, int dis, int& ans, int speed, vector<int>& visited) {\n if (dis % speed == 0) ans++;\n visited[u] = 1;\n for (auto& [v, w] : adj[u]) {\n if (!visited[v]) { \n dfs(adj, v, dis ...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution {\npublic:\nvoid solveNum(vector<pair<int,int>> adj[],int node,int dis,int& cnt,int signalSpeed,vector<int>& vis){\n vis[node]=1;\n for(auto it:adj[node]){\n if(vis[it.first]==0){\n if((dis+it.second)%signalSpeed==0){\n cnt++;\n }\n ...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution {\npublic:\n set<pair<int,int>> g[1010];\n int k, n;\n long long dist[1010], cnt;\n void bfs2(queue<pair<int,int>> &q){\n while(!q.empty()){\n auto curr = q.front();q.pop();\n int node = curr.first;\n int wt = curr.second;\n for(...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution {\npublic:\n void solve(vector<int>& temp,vector<vector<pair<int,int>>>& adj,int src,int parent,int dist)\n {\n for(auto it:adj[src])\n {\n int dest=it.first;\n int wt=it.second;\n if(dest!=parent)\n {\n temp.push...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution {\npublic:\n void solve(vector<int>& temp,vector<vector<pair<int,int>>>& adj,int src,int parent,int dist)\n {\n for(auto it:adj[src])\n {\n int dest=it.first;\n int wt=it.second;\n if(dest!=parent)\n {\n temp.push...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution {\n typedef vector<vector<pair<int, int>>> graph;\n typedef unsigned long long ull;\n\npublic:\n int bfs(int startpoint, graph &g, int magicNumber) {\n int ans = 0;\n vector<int> reachableNodes;\n for (int i = 0; i < g[startpoint].size(); ++i) {\n int...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution {\npublic:\n vector<int> countPairsOfConnectableServers(vector<vector<int>>& edges,\n int signal_speed) {\n int n = edges.size() + 1;\n unordered_map<int, unordered_map<int, int>> neighbours;\n for (int i = 0; i < n - 1; i...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution {\npublic:\n void dfs(int index, vector<int>&visited,vector<vector<pair<int,int>>>& adj, int &cnt, int speed, int dist){\n if(visited[index]==1)return;\n if(dist%speed==0)cnt++;\n visited[index]=1;\n for(auto i:adj[index]){\n dfs(i.first,visited,adj...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "class Solution {\npublic:\n int helper(int node, vector<pair<int, int>>adj[], int dist, int signalSpeed, vector<int> &visited)\n {\n int ans = 0;\n queue<pair<int, int>> q;\n q.push({node, dist});\n while(q.size())\n {\n int a = q.front().first, w = q.fro...
3,326
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</su...
3
{ "code": "typedef long long int ll;\n\n// Macro //\n#define X first\n#define Y second\n#define pii pair<int, int>\n#define pll pair<ll, ll>\n\nclass Solution {\npublic:\n vector<int> countPairsOfConnectableServers(vector<vector<int>>& edges, int signalSpeed) {\n const int n = edges.size() + 1;\n vec...
3,347
<p>You are given a <strong>1-indexed</strong> array of <strong>distinct</strong> integers <code>nums</code> of length <code>n</code>.</p> <p>You need to distribute all the elements of <code>nums</code> between two arrays <code>arr1</code> and <code>arr2</code> using <code>n</code> operations. In the first operation, a...
0
{ "code": "class Solution {\npublic:\n vector<int> resultArray(vector<int>& nums) {\n\n int n = nums.size();\n\n int left = 0, right = n-1;\n vector<int>ans(n);\n\n ans[0] = nums[0];\n ans[n-1] = nums[1];\n\n\n for(int i = 2; i < n; i++)\n {\n if(ans[left...
3,347
<p>You are given a <strong>1-indexed</strong> array of <strong>distinct</strong> integers <code>nums</code> of length <code>n</code>.</p> <p>You need to distribute all the elements of <code>nums</code> between two arrays <code>arr1</code> and <code>arr2</code> using <code>n</code> operations. In the first operation, a...
0
{ "code": "class Solution {\npublic:\n vector<int> resultArray(vector<int>& nums) {\n int a=0, j=nums.size()-1, n=j+1;\n nums.push_back(nums[1]);\n nums.erase(nums.begin()+1);\n for (int i=1; i<j; i++){\n if (nums[i-1]>nums[n-1]){\n continue;\n } els...
3,347
<p>You are given a <strong>1-indexed</strong> array of <strong>distinct</strong> integers <code>nums</code> of length <code>n</code>.</p> <p>You need to distribute all the elements of <code>nums</code> between two arrays <code>arr1</code> and <code>arr2</code> using <code>n</code> operations. In the first operation, a...
0
{ "code": "class Solution {\npublic:\n vector<int> resultArray(vector<int>& nums) {\n vector<int> v1(1, nums[0]);\n vector<int>v2(1, nums[1]);\n for(int i = 2; i < nums.size(); i++) {\n if(v1.back() > v2.back()) {\n v1.push_back(nums[i]);\n }\n e...
3,347
<p>You are given a <strong>1-indexed</strong> array of <strong>distinct</strong> integers <code>nums</code> of length <code>n</code>.</p> <p>You need to distribute all the elements of <code>nums</code> between two arrays <code>arr1</code> and <code>arr2</code> using <code>n</code> operations. In the first operation, a...
0
{ "code": "class Solution {\npublic:\n vector<int> resultArray(vector<int>& nums) {\n vector<int> ans;\n int i=0;\n ans.push_back(nums[0]);\n ans.push_back(nums[1]);\n cout<<ans[0]<<ans[1];\n for(int k=2;k<nums.size();k++)\n {\n if(ans[i]>*(ans.end()-1)){...
3,347
<p>You are given a <strong>1-indexed</strong> array of <strong>distinct</strong> integers <code>nums</code> of length <code>n</code>.</p> <p>You need to distribute all the elements of <code>nums</code> between two arrays <code>arr1</code> and <code>arr2</code> using <code>n</code> operations. In the first operation, a...
0
{ "code": "class Solution {\npublic:\n vector<int> resultArray(vector<int>& nums) {\n vector<int> arr1 = { nums[0] };\n vector<int> arr2 = { nums[1] };\n\n for (int i = 2; i < nums.size(); ++i) {\n if (arr1[arr1.size() - 1] > arr2[arr2.size() - 1]) {\n arr1.push_back(...
3,347
<p>You are given a <strong>1-indexed</strong> array of <strong>distinct</strong> integers <code>nums</code> of length <code>n</code>.</p> <p>You need to distribute all the elements of <code>nums</code> between two arrays <code>arr1</code> and <code>arr2</code> using <code>n</code> operations. In the first operation, a...
1
{ "code": "class Solution {\npublic:\n vector<int> resultArray(vector<int>& nums) {\n vector<int> arr1,arr2;\n arr1.push_back(nums[0]);\n arr2.push_back(nums[1]);\n for(int i=2;i<nums.size();i++)\n {\n if(arr1[arr1.size()-1]>arr2[arr2.size()-1])\n arr1.push_...
3,347
<p>You are given a <strong>1-indexed</strong> array of <strong>distinct</strong> integers <code>nums</code> of length <code>n</code>.</p> <p>You need to distribute all the elements of <code>nums</code> between two arrays <code>arr1</code> and <code>arr2</code> using <code>n</code> operations. In the first operation, a...
2
{ "code": "class Solution {\npublic:\n vector<int> resultArray(vector<int>& arr) {\n int n= arr.size();\n int k=0; int l=0;\n vector<int> arr1(1,arr[0]);\n vector<int> arr2(1,arr[1]);\n for(int i=2; i<n; i++){\n if(arr1[k]> arr2[l]){\n arr1.push_back(arr...
3,347
<p>You are given a <strong>1-indexed</strong> array of <strong>distinct</strong> integers <code>nums</code> of length <code>n</code>.</p> <p>You need to distribute all the elements of <code>nums</code> between two arrays <code>arr1</code> and <code>arr2</code> using <code>n</code> operations. In the first operation, a...
2
{ "code": "class Solution {\npublic:\n vector<int> resultArray(vector<int>& v) {\n int n=v.size();\n vector<int>ans;\n vector<int>v1,v2;\n v1.push_back(v[0]);\n v2.push_back(v[1]);\n for(int i=2;i<n;i++) {\n if(v1.back()>v2.back())\n v1.push_back(...
3,347
<p>You are given a <strong>1-indexed</strong> array of <strong>distinct</strong> integers <code>nums</code> of length <code>n</code>.</p> <p>You need to distribute all the elements of <code>nums</code> between two arrays <code>arr1</code> and <code>arr2</code> using <code>n</code> operations. In the first operation, a...
2
{ "code": "class Solution {\npublic:\n vector<int> resultArray(vector<int>& nums) {\n vector<int>a1,a2,Ans;\n \n a1.push_back(nums[0]);\n a2.push_back(nums[1]);\n \n for(int i=2;i<nums.size();i++)\n {\n if(a1[a1.size()-1]>a2[a2.size()-1])\n {\n...
3,347
<p>You are given a <strong>1-indexed</strong> array of <strong>distinct</strong> integers <code>nums</code> of length <code>n</code>.</p> <p>You need to distribute all the elements of <code>nums</code> between two arrays <code>arr1</code> and <code>arr2</code> using <code>n</code> operations. In the first operation, a...
2
{ "code": "class Solution {\npublic:\n vector<int> resultArray(vector<int>& arr) {\n int n= arr.size();\n int k=0; int l=0;\n vector<int> arr1(1,arr[0]);\n vector<int> arr2(1,arr[1]);\n for(int i=2; i<n; i++){\n if(arr1[k]> arr2[l]){\n arr1.push_back(arr...
3,338
<p>You are given a <strong>0-indexed</strong> integer matrix <code>grid</code> and an integer <code>k</code>.</p> <p>Return <em>the <strong>number</strong> of <span data-keyword="submatrix">submatrices</span> that contain the top-left element of the</em> <code>grid</code>, <em>and have a sum less than or equal to </em...
0
{ "code": "class Solution {\npublic:\n int countSubmatrices(vector<vector<int>>& grid, int k) {\n int count = 0;\n for (int i = 0; i < grid.size(); i++)\n for (int j = 1; j < grid[0].size(); j++)\n grid[i][j] += grid[i][j-1];\n\n for (int i = 1; i < grid.size(); i++) ...
3,338
<p>You are given a <strong>0-indexed</strong> integer matrix <code>grid</code> and an integer <code>k</code>.</p> <p>Return <em>the <strong>number</strong> of <span data-keyword="submatrix">submatrices</span> that contain the top-left element of the</em> <code>grid</code>, <em>and have a sum less than or equal to </em...
0
{ "code": "class Solution {\npublic:\n int countSubmatrices(vector<vector<int>>& grid, int k) {\n int ans = 0;\n for(int i = 0; i < grid.size(); ++i){\n for(int j = 0; j < grid[0].size(); ++j){\n if(i >= 1) grid[i][j] += grid[i-1][j];\n if(j >= 1) grid[i][j] += grid[i][j-1];\n ...
3,338
<p>You are given a <strong>0-indexed</strong> integer matrix <code>grid</code> and an integer <code>k</code>.</p> <p>Return <em>the <strong>number</strong> of <span data-keyword="submatrix">submatrices</span> that contain the top-left element of the</em> <code>grid</code>, <em>and have a sum less than or equal to </em...
0
{ "code": "class Solution {\npublic:\n int countSubmatrices(vector<vector<int>>& grid, int k) {\n int m = grid.size();\n int n =grid[0].size();\n int count = 0;\n for (int i=0; i<m; i++) {\n for (int j=0; j<n; j++) {\n if (i>0) grid[i][j]+=grid[i-1][j];\n ...
3,338
<p>You are given a <strong>0-indexed</strong> integer matrix <code>grid</code> and an integer <code>k</code>.</p> <p>Return <em>the <strong>number</strong> of <span data-keyword="submatrix">submatrices</span> that contain the top-left element of the</em> <code>grid</code>, <em>and have a sum less than or equal to </em...
0
{ "code": "class Solution {\npublic:\n int countSubmatrices(vector<vector<int>>& grid, int k) {\n int ans=0;\n int n=grid.size();\n int m=grid[0].size();\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n if(j!=0) grid[i][j]+=grid[i][j-1];\n if(i!...
3,338
<p>You are given a <strong>0-indexed</strong> integer matrix <code>grid</code> and an integer <code>k</code>.</p> <p>Return <em>the <strong>number</strong> of <span data-keyword="submatrix">submatrices</span> that contain the top-left element of the</em> <code>grid</code>, <em>and have a sum less than or equal to </em...
0
{ "code": "class Solution {\npublic:\n int countSubmatrices(vector<vector<int>>& grid, int k) {\n int ans = 0;\n for(int i = 0; i < grid.size(); ++i){\n for(int j = 0; j < grid[0].size(); ++j){\n if(i >= 1) grid[i][j] += grid[i-1][j];\n if(j >= 1) grid[i][j] += grid[i][j-1];\n ...
3,338
<p>You are given a <strong>0-indexed</strong> integer matrix <code>grid</code> and an integer <code>k</code>.</p> <p>Return <em>the <strong>number</strong> of <span data-keyword="submatrix">submatrices</span> that contain the top-left element of the</em> <code>grid</code>, <em>and have a sum less than or equal to </em...
0
{ "code": "class Solution {\npublic:\n int countSubmatrices(vector<vector<int>>& v, int k) {\n int n=v.size(),m=v[0].size();\n for(int i=0;i<n;i++) {\n for(int j=1;j<m;j++) {\n v[i][j]+=v[i][j-1];\n }\n }\n for(int i=0;i<m;i++) {\n for(int...
3,338
<p>You are given a <strong>0-indexed</strong> integer matrix <code>grid</code> and an integer <code>k</code>.</p> <p>Return <em>the <strong>number</strong> of <span data-keyword="submatrix">submatrices</span> that contain the top-left element of the</em> <code>grid</code>, <em>and have a sum less than or equal to </em...
1
{ "code": "class Solution {\npublic:\n int countSubmatrices(vector<vector<int>>& grid, int k) {\n int ans = 0;\n for(int i = 0; i < grid.size(); ++i){\n for(int j = 0; j < grid[0].size(); ++j){\n if(i >= 1) grid[i][j] += grid[i-1][j];\n if(j >= 1) grid[i][j] += grid[i][j-1];\...
3,338
<p>You are given a <strong>0-indexed</strong> integer matrix <code>grid</code> and an integer <code>k</code>.</p> <p>Return <em>the <strong>number</strong> of <span data-keyword="submatrix">submatrices</span> that contain the top-left element of the</em> <code>grid</code>, <em>and have a sum less than or equal to </em...
1
{ "code": "class Solution {\npublic:\n int countSubmatrices(vector<vector<int>>& grid, int k) {\n int count=0;\n if(grid[0][0]<=k)\n count++;\n for(int i=1;i<grid.size();i++){\n grid[i][0]+=grid[i-1][0];\n if(grid[i][0]<=k)\n count++;\n }\...