id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
1,621
<p>You are given an array of integers <code>nums</code> and an integer <code>target</code>.</p> <p>Return <em>the number of <strong>non-empty</strong> subsequences of </em><code>nums</code><em> such that the sum of the minimum and maximum element on it is less or equal to </em><code>target</code>. Since the answer may...
0
{ "code": "class Solution {\npublic:\n int pow[100001];\n void power2(int x){\n pow[0]=1;\n for(int i=1;i<=x;i++)\n pow[i]=(pow[i-1]*2)%1000000007;\n }\n int numSubseq(vector<int>& nums, int target) {\n int n=nums.size();\n power2(n);\n sort(nums.begin(),nums.end(...
1,621
<p>You are given an array of integers <code>nums</code> and an integer <code>target</code>.</p> <p>Return <em>the number of <strong>non-empty</strong> subsequences of </em><code>nums</code><em> such that the sum of the minimum and maximum element on it is less or equal to </em><code>target</code>. Since the answer may...
0
{ "code": "class Solution {\npublic:\n int numSubseq(vector<int>& nums, int target) {\n int mod=(int)1e9+7;\n sort(nums.begin(), nums.end());\n int l=0;\n int n=nums.size();\n int r=n-1;\n int ans=0;\n int poww[n];\n poww[0]=1;\n for(int i=1;i<n;i++)\n...
1,621
<p>You are given an array of integers <code>nums</code> and an integer <code>target</code>.</p> <p>Return <em>the number of <strong>non-empty</strong> subsequences of </em><code>nums</code><em> such that the sum of the minimum and maximum element on it is less or equal to </em><code>target</code>. Since the answer may...
1
{ "code": "class Solution {\npublic:\n int pow[100001];\n void power2(int x){\n pow[0]=1;\n for(int i=1;i<=x;i++)\n pow[i]=(pow[i-1]*2)%1000000007;\n }\n int numSubseq(vector<int>& nums, int target) {\n int n=nums.size();\n power2(n);\n sort(nums.begin(),nums.end(...
1,621
<p>You are given an array of integers <code>nums</code> and an integer <code>target</code>.</p> <p>Return <em>the number of <strong>non-empty</strong> subsequences of </em><code>nums</code><em> such that the sum of the minimum and maximum element on it is less or equal to </em><code>target</code>. Since the answer may...
1
{ "code": "class Solution {\npublic:\n int pow[100001];\n\n void powerNew(int x){\n pow[0]=1;\n\n for(int i=1;i<=x;i++){\n pow[i]=(pow[i-1]*2)%1000000007;\n }\n }\n\n int numSubseq(vector<int>& nums, int target) {\n int n=nums.size();\n\n powerNew(n);\n\n ...
1,621
<p>You are given an array of integers <code>nums</code> and an integer <code>target</code>.</p> <p>Return <em>the number of <strong>non-empty</strong> subsequences of </em><code>nums</code><em> such that the sum of the minimum and maximum element on it is less or equal to </em><code>target</code>. Since the answer may...
1
{ "code": "class Solution {\npublic:\n int numSubseq(vector<int>& nums, int target) {\n sort(nums.begin(), nums.end()) ;\n int m = 1e9 + 7 , p[100000 + 2] = {1,};\n for(int i = 1 ; i <= 100000 ; i ++)\n p[i] = (p[i - 1] << 1) % m ;\n long long l = 0 , r = nums.size() - 1 , ans = ...
1,621
<p>You are given an array of integers <code>nums</code> and an integer <code>target</code>.</p> <p>Return <em>the number of <strong>non-empty</strong> subsequences of </em><code>nums</code><em> such that the sum of the minimum and maximum element on it is less or equal to </em><code>target</code>. Since the answer may...
1
{ "code": "class Solution {\n public:\n int numSubseq(vector<int>& nums, int target) {\n constexpr int kMod = 1'000'000'007;\n const int n = nums.size();\n int ans = 0;\n vector<int> pows(n, 1); // pows[i] = 2^i % kMod\n\n for (int i = 1; i < n; ++i)\n pows[i] = pows[i - 1] * 2 % kMod;\n\n ra...
1,621
<p>You are given an array of integers <code>nums</code> and an integer <code>target</code>.</p> <p>Return <em>the number of <strong>non-empty</strong> subsequences of </em><code>nums</code><em> such that the sum of the minimum and maximum element on it is less or equal to </em><code>target</code>. Since the answer may...
1
{ "code": "class Solution {\n public:\n int numSubseq(vector<int>& nums, int target) {\n constexpr int kMod = 1'000'000'007;\n const int n = nums.size();\n int ans = 0;\n vector<int> pows(n, 1); // pows[i] = 2^i % kMod\n\n for (int i = 1; i < n; ++i)\n pows[i] = pows[i - 1] * 2 % kMod;\n\n ra...
1,621
<p>You are given an array of integers <code>nums</code> and an integer <code>target</code>.</p> <p>Return <em>the number of <strong>non-empty</strong> subsequences of </em><code>nums</code><em> such that the sum of the minimum and maximum element on it is less or equal to </em><code>target</code>. Since the answer may...
1
{ "code": "class Solution {\npublic:\n int numSubseq(vector<int>& nums, int target) {\n sort(nums.begin(), nums.end());\n\n int modNumber = 1000000007;\n auto start = nums.begin();\n auto stop = lower_bound(start, nums.end(), 1+target-*start);\n \n vector<int> seqs(2+dista...
1,621
<p>You are given an array of integers <code>nums</code> and an integer <code>target</code>.</p> <p>Return <em>the number of <strong>non-empty</strong> subsequences of </em><code>nums</code><em> such that the sum of the minimum and maximum element on it is less or equal to </em><code>target</code>. Since the answer may...
1
{ "code": "class Solution {\npublic:\n int numSubseq(vector<int>& nums, int target) {\n const int MOD = 1e9 + 7;\n int n = nums.size();\n \n \n sort(nums.begin(), nums.end());\n \n \n vector<int> pow(n, 1);\n for (int i = 1; i < n; ++i) {\n pow[i] = (...
1,621
<p>You are given an array of integers <code>nums</code> and an integer <code>target</code>.</p> <p>Return <em>the number of <strong>non-empty</strong> subsequences of </em><code>nums</code><em> such that the sum of the minimum and maximum element on it is less or equal to </em><code>target</code>. Since the answer may...
1
{ "code": "class Solution {\npublic:\n int numSubseq(vector<int>& nums, int target) {\n\n \n int m= 1e9 + 7;\n int n=nums.size();\n int ans=0;\n sort(nums.begin(),nums.end());\n\n vector<int> power(n);\n power[0]=1;\n for(int i=1;i<n;i++)\n {\n ...
1,621
<p>You are given an array of integers <code>nums</code> and an integer <code>target</code>.</p> <p>Return <em>the number of <strong>non-empty</strong> subsequences of </em><code>nums</code><em> such that the sum of the minimum and maximum element on it is less or equal to </em><code>target</code>. Since the answer may...
1
{ "code": "// Constraints:\n// 1 <= nums.length <= 10^5\n// 1 <= nums[i] <= 10^6\n// 1 <= target <= 10^6\n\nclass Solution\n{\npublic:\n int numSubseq(vector<int>& nums, int target)\n {\n const int cDivisor = 1e9 + 7;\n int n = nums.size();\n int totalCount = 0;\n int curCou...
1,621
<p>You are given an array of integers <code>nums</code> and an integer <code>target</code>.</p> <p>Return <em>the number of <strong>non-empty</strong> subsequences of </em><code>nums</code><em> such that the sum of the minimum and maximum element on it is less or equal to </em><code>target</code>. Since the answer may...
1
{ "code": "class Solution {\npublic:\n int numSubseq(vector<int>& nums, int target) {\n long long INF = 1e9+ 7;\n long long total = 0;\n int n = size(nums);\n int curr = 0, last = n - 1;\n sort(nums.begin(), nums.end());\n\n vector<int> p2(n, 1);\n for(int i=1;i<n;i...
1,621
<p>You are given an array of integers <code>nums</code> and an integer <code>target</code>.</p> <p>Return <em>the number of <strong>non-empty</strong> subsequences of </em><code>nums</code><em> such that the sum of the minimum and maximum element on it is less or equal to </em><code>target</code>. Since the answer may...
1
{ "code": "class Solution {\npublic:\n \n#include <iostream>\n#include <vector>\n#include <cmath>\n#include <algorithm>\n\nint numSubseq(std::vector<int>& nums, int target) {\n int mod = 1e9 + 7;\n int n = nums.size();\n int left = 0, right = n - 1;\n int sum = 0;\n\n // Sort the array first to effic...
1,621
<p>You are given an array of integers <code>nums</code> and an integer <code>target</code>.</p> <p>Return <em>the number of <strong>non-empty</strong> subsequences of </em><code>nums</code><em> such that the sum of the minimum and maximum element on it is less or equal to </em><code>target</code>. Since the answer may...
1
{ "code": "class Solution {\npublic:\n int numSubseq(vector<int>& nums, int target) {\n int MOD = 1e9 + 7;\n int n = nums.size();\n \n sort(nums.begin(), nums.end());\n \n vector<int> power(n, 1);\n for (int i = 1; i < n; i++) {\n power[i] = (power[i - 1...
1,621
<p>You are given an array of integers <code>nums</code> and an integer <code>target</code>.</p> <p>Return <em>the number of <strong>non-empty</strong> subsequences of </em><code>nums</code><em> such that the sum of the minimum and maximum element on it is less or equal to </em><code>target</code>. Since the answer may...
2
{ "code": "class Solution {\npublic:\n int M =1e9 + 7;\n int numSubseq(vector<int>& nums, int target) {\n int n = nums.size();\n \n sort(nums.begin(),nums.end());\n vector<int>power(n);\n power[0]=1;\n for(int i=1;i<n;i++){\n power[i] = (power[i-1]*2) % M;\n ...
1,621
<p>You are given an array of integers <code>nums</code> and an integer <code>target</code>.</p> <p>Return <em>the number of <strong>non-empty</strong> subsequences of </em><code>nums</code><em> such that the sum of the minimum and maximum element on it is less or equal to </em><code>target</code>. Since the answer may...
3
{ "code": "class Solution {\npublic:\n long mod = 1e9+7;\n long findPow(int x){\n long temp=1;\n long ans=1;\n if(x==1){\n return x;\n }\n // x = (n>=0 ? x : 1/x);\n long long m=abs(x);\n if(m==1)\n return x;\n while(m > 1)\n {...
1,621
<p>You are given an array of integers <code>nums</code> and an integer <code>target</code>.</p> <p>Return <em>the number of <strong>non-empty</strong> subsequences of </em><code>nums</code><em> such that the sum of the minimum and maximum element on it is less or equal to </em><code>target</code>. Since the answer may...
3
{ "code": "class Solution {\npublic:\n const int mod = 1e9 + 7; \n static vector<long long> pow2s;\n \n Solution() {\n if(pow2s.empty()) {\n ios::sync_with_stdio(0);\n cin.tie(0);\n pow2s.resize(100010);\n pow2s[0] = 1; \n for(int i = 1;i<pow2s...
1,621
<p>You are given an array of integers <code>nums</code> and an integer <code>target</code>.</p> <p>Return <em>the number of <strong>non-empty</strong> subsequences of </em><code>nums</code><em> such that the sum of the minimum and maximum element on it is less or equal to </em><code>target</code>. Since the answer may...
3
{ "code": "class Solution {\npublic:\nconst int mod = 1e9 + 7;\nint dp[100001];\n int mypow(int b)\n {\n if(b<0) return 0;\n if(dp[b]!=-1) return dp[b];\n if(b == 0) return dp[b] = 1;\n if(b == 1) return dp[b] = 2;\n return dp[b] = (2*mypow(b-1))%mod;\n }\n int numSubseq...
1,621
<p>You are given an array of integers <code>nums</code> and an integer <code>target</code>.</p> <p>Return <em>the number of <strong>non-empty</strong> subsequences of </em><code>nums</code><em> such that the sum of the minimum and maximum element on it is less or equal to </em><code>target</code>. Since the answer may...
3
{ "code": "class Solution {\npublic:\n int numSubseq(vector<int>& nums, int target) {\n \n int sum = 0 ;\n long long count = 0 ; \n int mini = 0 ;\n int maxi = 0 ;\n sort(begin(nums) , end(nums));\n \n int n = nums.size();\n vector<long long> power(n + 1);...
1,621
<p>You are given an array of integers <code>nums</code> and an integer <code>target</code>.</p> <p>Return <em>the number of <strong>non-empty</strong> subsequences of </em><code>nums</code><em> such that the sum of the minimum and maximum element on it is less or equal to </em><code>target</code>. Since the answer may...
3
{ "code": "class Solution {\npublic:\n int numSubseq(vector<int>& nums, int target) {\n int mod = 1e9+7;\n int n = nums.size();\n sort(nums.begin(), nums.end());\n\n vector<long long int> power(n);\n power[0] = 1;\n\n for(int i=1;i<n;i++)\n {\n power[i] =...
1,621
<p>You are given an array of integers <code>nums</code> and an integer <code>target</code>.</p> <p>Return <em>the number of <strong>non-empty</strong> subsequences of </em><code>nums</code><em> such that the sum of the minimum and maximum element on it is less or equal to </em><code>target</code>. Since the answer may...
3
{ "code": "class Solution {\n int mod = 1e9 + 7;\npublic:\n int numSubseq(vector<int>& nums, int target) {\n int ans = 0;\n int n = nums.size();\n int l = 0,r = n-1;\n vector<long long> pow(n+1,1);\n for(int i=1;i<=n;i++){\n pow[i] = (pow[i-1] * 2)%mod;\n }\n...
1,621
<p>You are given an array of integers <code>nums</code> and an integer <code>target</code>.</p> <p>Return <em>the number of <strong>non-empty</strong> subsequences of </em><code>nums</code><em> such that the sum of the minimum and maximum element on it is less or equal to </em><code>target</code>. Since the answer may...
3
{ "code": "class Solution {\n int mod = 1e9 + 7;\npublic:\n int numSubseq(vector<int>& nums, int target) {\n int ans = 0;\n int n = nums.size();\n int l = 0,r = n-1;\n vector<long long> pow(n+1,1);\n for(int i=1;i<=n;i++){\n pow[i] = (pow[i-1]%mod * 2)%mod;\n ...
1,621
<p>You are given an array of integers <code>nums</code> and an integer <code>target</code>.</p> <p>Return <em>the number of <strong>non-empty</strong> subsequences of </em><code>nums</code><em> such that the sum of the minimum and maximum element on it is less or equal to </em><code>target</code>. Since the answer may...
3
{ "code": "class Solution {\npublic:\n int numSubseq(vector<int>& nums, int target) {\n sort(nums.begin(),nums.end());\n int MOD=1e9+7;\n long long res=0;\n int n=nums.size();\n vector<long long> power(n, 1); // Precompute powers of 2\n for (int i = 1; i < n; i++) \n ...
1,621
<p>You are given an array of integers <code>nums</code> and an integer <code>target</code>.</p> <p>Return <em>the number of <strong>non-empty</strong> subsequences of </em><code>nums</code><em> such that the sum of the minimum and maximum element on it is less or equal to </em><code>target</code>. Since the answer may...
3
{ "code": "// Idea: (1) reverse thinking: what subseqs are bad?\n// instead of \"what are good subseqs?\"\n// (2) sort the array.\n// (3) if a number x >= target, then x cannot appear in any good subseq,\n// because the min element >= 1. So we should exclude such x directly.\n// (4) as for numbers that <= targ...
1,621
<p>You are given an array of integers <code>nums</code> and an integer <code>target</code>.</p> <p>Return <em>the number of <strong>non-empty</strong> subsequences of </em><code>nums</code><em> such that the sum of the minimum and maximum element on it is less or equal to </em><code>target</code>. Since the answer may...
3
{ "code": "// Idea: (1) reverse thinking: what subseqs are bad?\n// instead of \"what are good subseqs?\"\n// (2) sort the array.\n// (3) if a number x >= target, then x cannot appear in any good subseq,\n// because the min element >= 1. So we should exclude such x directly.\n// (4) as for numbers that <= targ...
1,621
<p>You are given an array of integers <code>nums</code> and an integer <code>target</code>.</p> <p>Return <em>the number of <strong>non-empty</strong> subsequences of </em><code>nums</code><em> such that the sum of the minimum and maximum element on it is less or equal to </em><code>target</code>. Since the answer may...
3
{ "code": "class Solution {\npublic:\n int MOD = 1e9 + 7;\n int numSubseq(vector<int>& nums, int target) {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n\n int n = nums.size();\n sort(nums.begin(), nums.end());\n vector<int> power;\n ...
1,621
<p>You are given an array of integers <code>nums</code> and an integer <code>target</code>.</p> <p>Return <em>the number of <strong>non-empty</strong> subsequences of </em><code>nums</code><em> such that the sum of the minimum and maximum element on it is less or equal to </em><code>target</code>. Since the answer may...
3
{ "code": "class Solution {\n const int MOD = 1e9+7;\n // vector\n // int calcPowerOfTwo(int n){\n // int i=0;\n // for(i=0; i<=n; i++){\n // ans\n // }\n // }\npublic:\n int numSubseq(vector<int>& nums, int target) {\n sort(nums.begin(), nums.end());\n\n\n ...
1,621
<p>You are given an array of integers <code>nums</code> and an integer <code>target</code>.</p> <p>Return <em>the number of <strong>non-empty</strong> subsequences of </em><code>nums</code><em> such that the sum of the minimum and maximum element on it is less or equal to </em><code>target</code>. Since the answer may...
3
{ "code": "class Solution {\npublic:\n int MOD = 1e9 + 7;\n int numSubseq(vector<int>& nums, int target) {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n\n int n = nums.size();\n sort(nums.begin(), nums.end());\n vector<int> power;\n ...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
0
{ "code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n while(edges[node1]>=0||edges[node2]>=0)\n {\n if(edges[node1]>=0)\n {\n int temp=node1;\n node1=edges[node1];\n edges[temp]=-3...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
0
{ "code": "class Solution {\npublic:\n int closestMeetingNode(const vector<int>& edges, int node1, int node2) {\n vector<bool> visited1(edges.size(), false),\n visited2(edges.size(), false);\n pair<int,int> nodes = {node1, node2};\n while(nodes.first != -1 || nodes.s...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
0
{ "code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n \n vector<int> visited(edges.size(), -1);\n int distance = 0;\n\n do{\n visited[node1] = distance++;\n node1 = edges[node1];\n }while(node1 != -1 && v...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
0
{ "code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n \n vector<int> distances(edges.size(), -1);\n bool visited[edges.size()];\n memset(visited, false, edges.size());\n\n\n int node = node1;\n int pathLength = 0;\n ...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
0
{ "code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n if(node1==node2)return node1;\n queue<int> f,s;\n if(node2<node1){\n swap(node1, node2);\n }\n f.push(node1);\n s.push(node2);\n if(edges[node2]==n...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
0
{ "code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n if(node1==node2) return min(node1, node2);\n int lens = edges.size();\n queue<int> q1{};\n queue<int> q2{};\n q1.push(node1);\n q2.push(node2);\n vector<int> ...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
0
{ "code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n if(node1==node2) return node1;\n int n=edges.size();\n vector<int> visited(n,INT_MAX);\n queue<pair<int,int>> q;\n visited[node1]=node1;\n visited[node2]=node2;\n ...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
0
{ "code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n if(node1==node2) return node1;\n int n=edges.size();\n vector<int> visited(n,INT_MAX);\n queue<pair<int,int>> q;\n visited[node1]=node1;\n visited[node2]=node2;\n ...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
0
{ "code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n queue<pair<int,int>>q;\n if(node1==node2)\n return node1;\n q.push({node1,1});\n q.push({node2,2});\n vector<int>vis(edges.size(),0);\n vis[node1]=1;\n ...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
0
{ "code": "class Solution {\npublic:\n void dfs(vector<int>& edges, int node, vector<int> &dis, int cnt) {\n if (node == -1 || dis[node] >= 0) return;\n\n // cout << node << \" \";\n dis[node] = cnt;\n // dis[node] = -2;\n\n dfs(edges, edges[node], dis, cnt+1);\n }\n int cl...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
0
{ "code": "class Solution {\n void dfs(vector<int>& edges, int root, vector<int>& distFromRoot){\n int depth = 0;\n while(root >= 0 && distFromRoot[root] == -1){\n distFromRoot[root] = depth;\n int neighbor = edges[root];\n root = neighbor;\n depth++;\n ...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
0
{ "code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n int n = edges.size();\n vector<int> dis1 = BFS(node1, n, edges);\n vector<int> dis2 = BFS(node2, n, edges);\n\n int minNode = -1, maxDistance = INT_MAX;\n for (int i = 0; i...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
0
{ "code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n int n = edges.size();\n auto dis1 = BFS(node1, n, edges);\n auto dis2 = BFS(node2, n, edges);\n\n int node = -1, x = INT_MAX;\n for (int i = 0; i < n; ++i) {\n i...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
0
{ "code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n int n = edges.size();\n vector<int> dist1(n, 1e9);\n vector<int> dist2(n, 1e9);\n\n // Calculate distances from node1\n queue<int> q;\n q.push(node1);\n dist1...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
1
{ "code": "class Solution {\n\npublic:\n\n void bfs(int startNode, vector<int>& edges, vector<int>& dist) {\n\n int n = edges.size();\n\n queue<int> q;\n\n q.push(startNode);\n\n vector<bool> visit(n);\n\n dist[startNode] = 0;\n\n while (!q.empty()) {\n\n int no...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
1
{ "code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n // Initialize distance arrays and visited arrays\n vector<int> dist1(edges.size(), -1);\n vector<int> dist2(edges.size(), -1);\n vector<bool> visited1(edges.size(), false);\n ...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
1
{ "code": "class Solution {\npublic:\n vector<int> dfs1;\n vector<int> dfs2;\n void util(int i, int dist, vector<int> &dfs_i, vector<int>&edges) {\n if (edges[i] == -2) return;\n dfs_i[i] = dist;\n if (edges[i] == -1) return;\n \n int temp = edges[i];\n edges[i] = -2...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
1
{ "code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n // int n=edges.size();\n // vector<int> d1(n,-1);\n // vector<int> d2(n,-1);\n // queue<pair<int,int>> q;\n // q.push({node1,0});\n // while(!q.empty()){\n // ...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
1
{ "code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n int n=edges.size();\n vector<int>dist1(n,1e9);\n queue<pair<int,int>>q;\n q.push({node1,0});\n dist1[node1]=0;\n while(!q.empty()){\n int node=q.front().first;\n i...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
1
{ "code": "#include <vector>\n#include <queue>\n#include <limits.h> // for INT_MAX\nusing namespace std;\n\nclass Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n int n = edges.size();\n \n // Perform BFS for both node1 and node2\n vector<int> ...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
1
{ "code": "class Solution {\npublic:\n void dfs(int node,vector<int>&edges,vector<int>&distN1,int dist,vector<int>&vis){\n if(node == -1 || vis[node] == 1)return;\n vis[node] = 1;\n distN1[node] = dist;\n dfs(edges[node],edges,distN1,dist+1,vis);\n }\n int closestMeetingNode(vecto...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
1
{ "code": "class Solution {\npublic:\n void dfs(vector<int>&dis,vector<int>&edges,int node,int cnt,vector<int>&vist){\n \n if(vist[node])return;\n vist[node]=1;\n dis[node]=cnt;\n if(edges[node]==-1)return;\n dfs(dis,edges,edges[node],cnt+1,vist);\n }\n int closestMee...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
1
{ "code": "class Disjoint{\npublic:\n vector<int>parent;\n vector<int>size;\n\n Disjoint(int node){\n parent.resize(node + 1);\n size.resize(node + 1, 1);\n for(int i = 0; i <= node; i++){\n parent[i] = i;\n }\n }\n\n int findParent(int node){\n if(node == ...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
1
{ "code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n\n if(node1 == node2) return node1;\n\n unordered_set<int> vis1, vis2;\n int x = node1, y = node2;\n while(x != -1 or y != -1){\n if(x!=-1) vis1.insert(x);\n ...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
1
{ "code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n \n int n = edges.size();\n vector<int> vis1(n,0);\n vector<int> dis1(n,1e6);\n dis1[node1] = 0;\n vis1[node1] = 1;\n\n queue<int> q;\n q.push(node1);\n...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
1
{ "code": "class Solution {\n void bfs(vector<int>& edges, int node, vector<int>& dist) {\n queue<int> q;\n q.push(node);\n \n vector<int> visited(edges.size(), 0);\n visited[node] = 1;\n dist[node] = 0;\n\n while (!q.empty()) {\n int u = q.front();\n ...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
1
{ "code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n std::queue<int> q1, q2;\n std::set<int> seen1, seen2;\n seen1.insert(node1);\n seen2.insert(node2);\n q1.push(node1);\n q2.push(node2);\n if (node1 == node2) ...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
1
{ "code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n int n=edges.size();\n function<void(int,int,vector<int>&)> dfs=[&](int node,int d,vector<int>&dist){\n dist[node]=d;\n if(edges[node]!=-1&&dist[edges[node]]==-1)\n ...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
1
{ "code": "class Solution {\npublic:\n int ans = -1;\n int mnDis = INT_MAX;\n void dfs(vector<int>& edges, vector<int>& vis, unordered_map<int, int> &dic, int node, int dis = 0) {\n while(node > -1 && !vis[node]) {\n vis[node] = 1;\n if(dic.contains(node)) {\n if(m...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
1
{ "code": "class Solution {\npublic:\n vector<int> disFromNode(vector<int> edges, int src){\n int n = edges.size(); \n vector<int> ans(n,1e6); \n vector<int> vis(n,0); \n ans[src] = 0; vis[src] = 1; \n queue<pair<int,int>> q; \n q.push({src,0}); \n while(!q.empty()...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
1
{ "code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n int n = edges.size();\n int start = node1;\n unordered_map<int, pair<int, int>> m;\n int dist = 0;\n while(start!=-1 && m[start].first!=1){\n m[start] = {1, dist...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
1
{ "code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n int n=edges.size();\n queue<vector<int>>st;\n if(node1<node2){\n int temp=node1;\n node1=node2;\n node2=temp;\n }\n st.push({0,node1,1});\n...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
2
{ "code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n int n = edges.size();\n vector<int> dist(n,INT_MAX);\n dist[node1] = 0;\n priority_queue<vector<int>,vector<vector<int>>,greater<vector<int>>> pq;\n pq.push({0,node1});\n ...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
2
{ "code": "#define INF 0x3f3f3f3f\n\nclass Solution {\n int res = -1;\n int minDist = INF;\n vector<int> dist;\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n int n = edges.size();\n dist.resize(n, INF);\n vector<bool> visited(n, false);\n dfs1(n...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
2
{ "code": "class Solution {\npublic:\n void dfs(int node, vector<int>& dist_node, vector<bool>& visited,\n vector<int>& edges, int distance) {\n if (node == -1 || node >= visited.size()) return; // Check for valid node index\n if (!visited[node]) {\n visited[node] = true;\n ...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
2
{ "code": "class Solution {\npublic:\n void dfs(int node, vector<int>& edges, vector<int>& dist, vector<bool>& visit) {\n visit[node] = true;\n int neighbor = edges[node];\n if (neighbor != -1 && !visit[neighbor]) {\n dist[neighbor] = 1 + dist[node];\n dfs(neighbor, edges...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
2
{ "code": "/*\nclass Solution {\n void dfs(vector<vector<int>>& graph, vector<int> &mp, vector<bool>& vis, int node, int w){\n mp[node] = w;\n vis[node] = true;\n for(int i = 0; i < graph[node].size(); i++){\n int child = graph[node][i];\n if(vis[child] == false || w + 1 ...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
2
{ "code": "class Solution {\npublic:\n unordered_map<int,int>mp;\n void f(vector<int>adj, int node, vector<int>&vis){\n queue<pair<int,int>>q;\n q.push({node,0});\n vis[node] = 1;\n while(!q.empty()){\n int currNode = q.front().first;\n int currDis = q.front().s...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
2
{ "code": "#define inc() \\\n ios_base::sync_with_stdio(0); \\\n cin.tie(0); \\\n cout.tie(0)\n\nclass Solution {\npublic:\n void dfs...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
2
{ "code": "class Solution {\npublic:\n int n;\n \n void dfs(vector<int>& edges, int startNode, vector<int>& dist_node, vector<bool>& visited) {\n visited[startNode] = true;\n \n int v = edges[startNode];\n \n if(v != -1 && !visited[v]) {\n visited[v] = true;\n ...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
2
{ "code": "class Solution {\npublic:\n\nvoid dfs(vector<int>&edges,int startNode,vector<int>&dist_node,vector<bool>&vist,int n){\n\n vist[startNode] = true;\n\n int v = edges[startNode];\n\n if(v!=-1 && !vist[v]){\n vist[v] = true;\n dist_node[v] = 1+dist_node[startNode];\n dfs(edges,v,d...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
2
{ "code": "#define mp make_pair\n#define pb push_back\n#define fi first\n#define se second\n#define i64 long long\ntypedef vector<i64> vi;\ntypedef pair<int,int> pii;\nclass Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node0, int node1) {\n queue<pii>q;\n q.push(mp(node0,0));\...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
2
{ "code": "class Solution {\npublic:\n typedef pair<int, int> pi;\n void distance(int i, vector<int> &dist, vector<int> adj[]){\n priority_queue<pi, vector<pi>, greater<pi>> pq;\n pq.push({0, i});\n dist[i]=0;\n while(!pq.empty()){\n auto itr=pq.top();\n int dis...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
2
{ "code": "class Solution {\npublic:\n vector<int> dfs(int n,vector<int>adj[],int s){\n vector<int>dis(n+1,INT_MAX);\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;\n pq.push({0,s});\n dis[s]=0;\n while(!pq.empty()){\n auto it=pq.top();\...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
2
{ "code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n unordered_map<int, int> mp1, mp2;\n\n for(int i = 0, x = node1; x != -1 && !mp1.count(x); ++i) {\n mp1[x] = i;\n x = edges[x];\n }\n\n int ans = -1;\n ...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
2
{ "code": "vector<vector<int>>g;\nvector<int>disn1,disn2,visn1,visn2;\n\nvoid bfs1(int node){\n disn1[node]=0;\n cout<<\"node\"<<disn1[node]<<endl;\n queue<int>q;\n q.push(node);\n while(!q.empty()){\n int x=q.front();q.pop();\n visn1[x]=1;\n for(auto nbr:g[x]){\n if(!vi...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
2
{ "code": "class Solution {\npublic:\n vector<int>g[100001];\n int vis[100001];\n int vis1[100001];\n int dis[100001];\n int dis1[100001];\n void dfs(int node) {\n vis[node]=1;\n for(int child:g[node]) {\n if(!vis[child]) {\n dis[child]=dis[node]+1;\n ...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
2
{ "code": "class Solution {\n int getMini(vector<int>&dist1, vector<int>&dist2, int mid){\n int n = dist1.size();\n for(int i = 0 ; i<n ; i++){\n if(dist1[i] <= mid && dist2[i] <= mid){\n return i;\n }\n }\n return -1;\n }\npublic:\n int closes...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
2
{ "code": "\n\nconst int N = 1e5+5;\nvector<int> g[N];\nclass Solution {\n\nvoid dfs(int src, int depth, vector<bool>& visited, vector<int>& distance){\n\n if(visited[src]) return;\n\n visited[src] = true;\n distance[src] = depth;\n \n for(auto i : g[src]) dfs(i,depth+1,visited,dist...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
2
{ "code": "class Solution {\n void helper(int node1, vector<int>& edges, map<int, int>& distanceFrom1) {\n int currentNode1 = node1;\n distanceFrom1[node1] = 1;\n while(edges[currentNode1] != -1) {\n if (distanceFrom1.find(edges[currentNode1]) != distanceFrom1.end()) {\n ...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
2
{ "code": "class Solution {\npublic:\nint n;\nvector<int>func(vector<int>adj[],int i){\n queue<pair<int,int>>q; \n vector<int>dis(n,1e9);\n q.push({0,i});\n dis[i]=0;\n while(!q.empty()){\n auto it =q.front();\n q.pop();\n int node=it.second;\n int d=it.first;\n for(...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
2
{ "code": "class Solution {\npublic:\n vector<int> bfs(vector<int> adj[], int src, int n) {\n queue<pair<int, int>> q;\n vector<int> dist(n, INT_MAX);\n dist[src] = 0;\n q.push({0, src}); // steps, node\n while (!q.empty()) {\n int node = q.front().second;\n ...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
2
{ "code": "class Solution {\npublic:\n void bfs(int src,vector<int> adj[], vector<int> &vis,int n){\n queue<pair<int,int>>q;\n q.push({src,0});\n vis[src] = 0;\n while(!q.empty()){\n int node = q.front().first;\n int level = q.front().second;\n \n ...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
2
{ "code": "class Solution {\npublic:\n int n;\n void bfs(vector<int> adj[], vector<int> & dist, int node){\n \n\n queue<pair<int,int>> q;\n q.push({node, 0});\n\n while(!q.empty()){\n int node = q.front().first;\n int steps = q.front().second;\n q.pop...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
2
{ "code": "class Solution {\npublic:\n\n void dfs(int it, vector<int> &visited,\n vector<int> adj[],vector<int> &distance)\n {\n visited[it]=1;\n if(adj[it].size()==1 && visited[adj[it][0]]==0)\n { distance[adj[it][0]] = distance[it] + 1;\n dfs(adj[it][0],vis...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
2
{ "code": "class Solution {\npublic:\n \n // void dfs(vector<int>& edges, int curr, vector<int>& vis, int step, unordered_map<int, int> &mp)\n // {\n // if (curr == -1) return;\n \n // if (!vis[curr])\n // {\n // if (mp.find(curr) != mp.end()) mp[curr] = min(step, mp[cu...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
2
{ "code": "class Solution {\npublic:\n void dfs(int node , vector<int> &dist , vector<int> adj[]){\n for(auto it: adj[node]){\n if(dist[it] == -1){\n dist[it] = dist[node] + 1;\n dfs(it, dist,adj);\n }\n }\n }\n \n int closestMeetingNode(ve...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
2
{ "code": "class Solution {\nprivate:\n void dfs(int src, vector<int>adj[], vector<int>&timer) {\n for(auto &v : adj[src]) {\n if(timer[v]==-1) {\n timer[v] = timer[src] + 1;\n dfs(v, adj, timer);\n }\n }\n }\n \npublic:\n int closestMeetin...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
2
{ "code": "class Solution {\n void rec(int node,vector<int>adj[],vector<int>&dist){\n int n=dist.size();\n vector<int>vis(n+1,0);\n queue<int>q;\n q.push(node);\n dist[node]=0;\n vis[node]=1;\n while(!q.empty()){\n int sz=q.size();\n for(int i=...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
2
{ "code": "class Solution {\npublic:\n vector<int> bfs(int node ,vector<int>adj[],int n){\n vector<int>dis(n,1e9),vis(n,0);\n queue<int>q;\n q.push(node);\n vis[node]=1;\n dis[node]=0;\n int lvl=0;\n while(!q.empty()){\n int cur=q.front();\n q....
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
3
{ "code": "class Solution {\npublic:\n vector<int> bfs(const vector<int>& edges, int node){\n unordered_set<int> visited;\n queue<int> q;\n q.push(node);\n int dist = 0;\n vector<int> nodeDist(edges.size(), INT_MAX);\n while(!q.empty()){\n int size = q.size();\n...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
3
{ "code": "class Solution {\npublic:\n void bfs(int node, vector<int>& dist, vector<int>& edges) {\n unordered_set<int> vis;\n\n queue<int> q;\n q.push(node);\n vis.insert(node);\n int lvl = 0;\n while (!q.empty()) {\n int sz = q.size();\n\n for (int ...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
3
{ "code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n int n=edges.size();\n if(node1==node2){\n return node1;\n }\n vector<int> adj[n];\n for(int i=0;i<n;i++){\n if(edges[i]!=-1){\n adj[i]....
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
3
{ "code": "class Solution {\npublic:\n void dfs(vector<int>adj[],int node,vector<int>&vis,vector<int>&v,int dis){\n vis[node]=1;\n v[node]=dis;\n for(auto s:adj[node]){\n if(!vis[s]) dfs(adj,s,vis,v,dis+1);\n }\n }\n int closestMeetingNode(vector<int>& edges, int node1,...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
3
{ "code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n vector<int> distances1 = bfs(edges, node1);\n vector<int> distances2 = bfs(edges, node2);\n int min_max = numeric_limits<int>::max();\n int res = -1;\n\n for (int i = 0; i ...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
3
{ "code": "class Solution {\npublic:\n void solve(int &n, vector<int> &edges, int &u, int &v, int &ans, int &maxDist) {\n unordered_map<int, int> umap;\n vector<int> path(n, 0);\n int i = 0, node = u;\n\n while(node != -1 && !path[node]) {\n path[node] = 1;\n umap[...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
3
{ "code": "class Solution {\nprivate:\n vector<int> bfs(vector<int>adj[],int source,int n)\n {\n vector<int>res(n,1e8);\n vector<int>vis(n,0);\n queue<pair<int,int>>q;\n q.push({source,0});\n vis[source]=1;\n res[source]=0;\n while(!q.empty())\n {\n ...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
3
{ "code": "class Solution {\nprivate:\n vector<int> bfs(vector<int>adj[],int source,int n)\n {\n vector<int>res(n,1e8);\n vector<int>vis(n,0);\n queue<pair<int,int>>q;\n q.push({source,0});\n vis[source]=1;\n res[source]=0;\n while(!q.empty())\n {\n ...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
3
{ "code": "class Solution {\npublic:\n void dfs(vector<int> adj[],vector<int> &vis,vector<int> &dis,int node,int steps){\n vis[node] = true;\n dis[node] = steps;\n\n for(auto i : adj[node]){\n if(!vis[i])\n dfs(adj,vis,dis,i,steps+1);\n }\n }\n...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
3
{ "code": "class Solution {\npublic:\n void dfs(int node, vector<int> &dist, vector<int> &vis, vector<int> adj[])\n {\n vis[node] = 1;\n for (auto i : adj[node])\n {\n if (!vis[i])\n {\n dist[i] = 1 + dist[node];\n dfs(i, dist, vis, adj);\...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
3
{ "code": "class Solution {\npublic:\n vector<int> minDist(vector<vector<int>>& adj, int node1) {\n int n = adj.size();\n vector<int> ans(n, INT_MAX - 1);\n ans[node1] = 0;\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;\n pq.push({0, node1})...
2,438
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p> <p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat...
3
{ "code": "class Solution {\npublic:\n \n void solve(int src,vector<vector<int>>&adj , vector<int>&dist){\n priority_queue<pair<int,int> , vector<pair<int,int>> , greater<pair<int,int>>>pq;\n pq.push({0,src});\n dist[src]=0;\n while(!pq.empty()){\n auto d = pq.top().first;...