question_slug stringlengths 3 77 | title stringlengths 1 183 | slug stringlengths 12 45 | summary stringlengths 1 160 ⌀ | author stringlengths 2 30 | certification stringclasses 2
values | created_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | updated_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | hit_count int64 0 10.6M | has_video bool 2
classes | content stringlengths 4 576k | upvotes int64 0 11.5k | downvotes int64 0 358 | tags stringlengths 2 193 | comments int64 0 2.56k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
count-valid-paths-in-a-tree | Video Explanation [Complete Brute Force --> DSU journey - O(N)] | video-explanation-complete-brute-force-d-q84x | Explanation\n\nClick here for the video\n\n# Code\n\nstruct DSU {\n vector<int> par;\n vector<int> cnt;\n \n DSU (int n) {\n par.resize(n+1, | codingmohan | NORMAL | 2023-09-24T04:50:41.949463+00:00 | 2023-09-24T04:50:41.949483+00:00 | 890 | false | # Explanation\n\n[Click here for the video](https://youtu.be/8XVSfMU-Y68)\n\n# Code\n```\nstruct DSU {\n vector<int> par;\n vector<int> cnt;\n \n DSU (int n) {\n par.resize(n+1, 0);\n cnt.resize(n+1, 1);\n \n for (int j = 0; j <= n; j ++) par[j] = j;\n }\n \n int Leader ... | 24 | 0 | ['C++'] | 2 |
count-valid-paths-in-a-tree | ✅☑[C++/ C / Java/ Python/ JavaScript ] || Beats 100% || DP ||EXPLAINED🔥 | c-c-java-python-javascript-beats-100-dp-ct4wh | PLEASE UPVOTE IF IT HELPED\n\n# Approach\n\n(Also explained in the code)\n\n1. genPrimes Function:\n\n- This function generates a list of prime numbers up to th | MarkSPhilip31 | NORMAL | 2023-09-24T04:42:43.137998+00:00 | 2023-10-18T09:48:30.692830+00:00 | 1,601 | false | # *PLEASE UPVOTE IF IT HELPED*\n\n# Approach\n\n***(Also explained in the code)***\n\n1. `genPrimes` Function:\n\n- This function generates a list of prime numbers up to the given integer \'n\' using the Sieve of Eratosthenes algorithm.\n- It initializes a boolean vector `prime` of size `(n + 1)` to `true` for all numb... | 18 | 0 | ['Array', 'Math', 'Dynamic Programming', 'Tree', 'Depth-First Search', 'C', 'C++', 'Java', 'Python3', 'JavaScript'] | 4 |
count-valid-paths-in-a-tree | C++ DSU Solution | c-dsu-solution-by-anujjadhav-2yss | Intuition\nWe are grouping the non-primes together separated by prime number\nAnd then for each primt numbers we try to find all the neighbouring groups of non- | AnujJadhav | NORMAL | 2023-09-24T07:11:26.802976+00:00 | 2023-09-24T07:11:26.803002+00:00 | 1,254 | false | # Intuition\nWe are grouping the non-primes together separated by prime number\nAnd then for each primt numbers we try to find all the neighbouring groups of non-prime\nthen total paths going through that prime no. can be easily calculated using simple maths\n\n# Code\n```\n#define ll long long\n\nclass Sieve {\npublic... | 17 | 0 | ['C++'] | 5 |
count-valid-paths-in-a-tree | [Python] DFS | python-dfs-by-awice-cjgr | If a node is prime, consider it to have weight 1. We want the number of paths with weight exactly 1.\n\nRoot the tree at 1 and consider the subtree at node. N | awice | NORMAL | 2023-09-24T04:31:54.950219+00:00 | 2023-09-24T04:31:54.950238+00:00 | 541 | false | If a node is prime, consider it to have weight 1. We want the number of paths with weight exactly 1.\n\nRoot the tree at `1` and consider the subtree at `node`. Now `c0, c1 = dfs(node, parent)` will be the number of paths starting at `node` in this subtree with sum `0` or `1` respectively. For a node $u$, lets call ... | 11 | 0 | ['Python3'] | 1 |
count-valid-paths-in-a-tree | 🔥Just 2 DFS || C++ || | just-2-dfs-c-by-jainwinn-l6rt | \n# Code\n\nclass Solution {\npublic:\n //here for each node we store sum of nodes below a given node such that there is no prime along the path in sum array | JainWinn | NORMAL | 2023-09-24T04:20:48.066881+00:00 | 2023-09-24T07:04:38.739184+00:00 | 937 | false | \n# Code\n```\nclass Solution {\npublic:\n //here for each node we store sum of nodes below a given node such that there is no prime along the path in sum array.\n //also we store number of nodes without prime for each path from the node in dist array(only for primes) .\n long long dfs(int curr,vector<vector<i... | 4 | 0 | ['Depth-First Search', 'C++'] | 0 |
count-valid-paths-in-a-tree | Java | Dfs | One pass | self explanatory | java-dfs-one-pass-self-explanatory-by-je-c1qg | Approach\nRun dfs starting from first node.\nFor every iteration return 2 values\n - numbers of pathes discovered so far which has exactly one prime\n - number | jeckafedorov | NORMAL | 2023-09-24T04:18:15.691614+00:00 | 2023-09-24T04:18:15.691633+00:00 | 498 | false | # Approach\nRun dfs starting from first node.\nFor every iteration return 2 values\n - numbers of pathes discovered so far which has exactly one prime\n - number of pathes discovered so far which has no prime\n\nDepends on current node (if it is prime or node) add to result\npathes which ends in current node and pathes... | 4 | 0 | ['Java'] | 4 |
count-valid-paths-in-a-tree | [C++] DFS solution O(N) | c-dfs-solution-on-by-pr0d1g4ls0n-4is0 | Intuition\n Describe your first thoughts on how to solve this problem. \nDFS through path, intuition similar to binary tree maximum path.\nhttps://leetcode.com/ | pr0d1g4ls0n | NORMAL | 2023-09-24T04:15:42.950337+00:00 | 2023-09-24T12:37:13.461649+00:00 | 514 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nDFS through path, intuition similar to binary tree maximum path.\nhttps://leetcode.com/problems/binary-tree-maximum-path-sum/\n\nAt each node:\n1. Calculate subtree path ending at node.\n2. Calculate two subtree paths that go through curr... | 4 | 0 | ['C++'] | 3 |
count-valid-paths-in-a-tree | Java | Dfs | One pass with comments | java-dfs-one-pass-with-comments-by-jecka-frs8 | Approach\nRun dfs starting from first node.\nFor every iteration return 2 values\n - numbers of pathes discovered so far which has exactly one prime\n - number | jeckafedorov | NORMAL | 2023-09-25T22:38:22.810223+00:00 | 2023-09-25T22:38:22.810257+00:00 | 91 | false | # Approach\nRun dfs starting from first node.\nFor every iteration return 2 values\n - numbers of pathes discovered so far which has exactly one prime\n - number of pathes discovered so far which has no prime\n\nDepends on current node (if it is prime or node) add to result\npathes which ends in current node and pathes... | 3 | 0 | ['Java'] | 0 |
count-valid-paths-in-a-tree | O(N+ N log N) Solution using DFS| C++| | on-n-log-n-solution-using-dfs-c-by-coder-9vyn | Approach: \nFor every node I will keep track of two things:\n1) Number of nodes in its subtree that have non-prime labels and the path to them also contains onl | coder_gogeta | NORMAL | 2023-09-25T11:27:59.297964+00:00 | 2023-09-26T13:26:49.929851+00:00 | 513 | false | ### Approach: \nFor every node I will keep track of two things:\n1) Number of nodes in its subtree that have non-prime labels and the path to them also contains only non-prime labels, I will call such nodes in the subtree as non-prime nodes. \n2) Number of nodes in its subtree that have prime labels and the path to the... | 3 | 0 | [] | 1 |
count-valid-paths-in-a-tree | ✅ C++ || DSU || Beats 100% || Intuition and Explanation 🔥🔥🔥 | c-dsu-beats-100-intuition-and-explanatio-lgcd | Intuition\n Describe your first thoughts on how to solve this problem.\n \nThe main concept is to count non-prime children of a prime node and merging them usin | RuntimeErr0r | NORMAL | 2023-09-25T11:07:58.949864+00:00 | 2023-09-25T11:07:58.949887+00:00 | 202 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem.\n -->\nThe main concept is to count non-prime children of a prime node and merging them using disjoint set algorithm.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Get all the prime numbers upto n and store them in... | 3 | 0 | ['Union Find', 'Graph', 'C++'] | 0 |
count-valid-paths-in-a-tree | Complete Contest Video Solution | Explanation With Drawings | In Depth | complete-contest-video-solution-explanat-0701 | Intuition and approach discussed in detail in video solution\nhttps://youtu.be/A2ynfP81UW4\n# Code\n\n// https://cp-algorithms.com/data_structures/disjoint_set_ | Fly_ing__Rhi_no | NORMAL | 2023-09-24T10:32:51.145964+00:00 | 2023-09-24T10:32:51.145982+00:00 | 215 | false | # Intuition and approach discussed in detail in video solution\nhttps://youtu.be/A2ynfP81UW4\n# Code\n```\n// https://cp-algorithms.com/data_structures/disjoint_set_union.html\n// https://cp-algorithms.com/algebra/sieve-of-eratosthenes.html\n// ---------------------------------------------------------------------------... | 3 | 0 | ['C++'] | 0 |
count-valid-paths-in-a-tree | Very intuitive approach using tree in-out dp (beats 100% in memory use) | very-intuitive-approach-using-tree-in-ou-7iay | Intuition\n Describe your first thoughts on how to solve this problem. \n Lets consider each vertex , if we can find the number of path starting from this verte | i_m_karank | NORMAL | 2023-09-24T06:49:06.851994+00:00 | 2023-09-24T06:49:06.852024+00:00 | 149 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n Lets consider each vertex , if we can find the number of path starting from this vertex and have exactly one prime number , then we are done, we will add the contribution of each vertex to get our final answer \n# Approach\n<!-- Describ... | 3 | 0 | ['C++'] | 3 |
count-valid-paths-in-a-tree | Union Find Based Solution | union-find-based-solution-by-sushantk_04-d9h7 | Intuition\n Describe your first thoughts on how to solve this problem. \nEach prime node in the tree will split the tree and groups will form within the tree.\n | sushantk_04 | NORMAL | 2023-09-24T05:12:04.612896+00:00 | 2023-09-24T06:07:36.585372+00:00 | 408 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nEach prime node in the tree will split the tree and groups will form within the tree.\n\nNow suppose this node have 3 neighbours so the total no of paths for this prime node will be - \n\nNegh 1 belong to Group A total node in group is 5\... | 3 | 0 | ['C++'] | 1 |
count-valid-paths-in-a-tree | Divide and Conquer + Backtracking [EXPLAINED] | divide-and-conquer-backtracking-explaine-iso4 | IntuitionThe key to solving this problem is recognizing that a valid path between two nodes in a tree must contain exactly one prime number. To do this, we need | r9n | NORMAL | 2025-01-15T23:02:17.218204+00:00 | 2025-01-15T23:02:17.218204+00:00 | 12 | false | # Intuition
The key to solving this problem is recognizing that a valid path between two nodes in a tree must contain exactly one prime number. To do this, we need to traverse the tree, count the prime numbers along the way, and check if the path meets the condition of having exactly one prime number.
# Approach
Use D... | 2 | 0 | ['Math', 'Divide and Conquer', 'Dynamic Programming', 'Backtracking', 'Depth-First Search', 'Breadth-First Search', 'Graph', 'Combinatorics', 'Python3'] | 0 |
count-valid-paths-in-a-tree | Python (Simple DFS) | python-simple-dfs-by-rnotappl-iicn | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | rnotappl | NORMAL | 2023-10-11T21:17:51.184608+00:00 | 2023-10-11T21:17:51.184638+00:00 | 37 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 2 | 0 | ['Python3'] | 0 |
count-valid-paths-in-a-tree | Easy and Beginner friendly || Sieve || DFS || DP | easy-and-beginner-friendly-sieve-dfs-dp-kcc8h | Java []\nclass Solution {\n List<Integer>[] adj;\n int cnt[]; long res=0;\n int isprime[];\n private void ini(int n) { //sieve\'s algo\n ispr | Lil_ToeTurtle | NORMAL | 2023-09-24T14:04:40.712873+00:00 | 2023-09-24T14:04:40.712893+00:00 | 246 | false | ```Java []\nclass Solution {\n List<Integer>[] adj;\n int cnt[]; long res=0;\n int isprime[];\n private void ini(int n) { //sieve\'s algo\n isprime[1]=1;\n for(int i=2; i<=Math.sqrt(n); i++){\n if(isprime[i]==1) continue;\n for(int j=i+i;j<=n;j+=i){\n ispri... | 2 | 0 | ['Divide and Conquer', 'Dynamic Programming', 'Java'] | 2 |
count-valid-paths-in-a-tree | DFS Tree | Beats 100 percent | dfs-tree-beats-100-percent-by-ultrasonic-p6nu | \n# Approach\n Describe your approach to solving the problem. \nDFS way postorder Tree traversal\n\n- Counting number of distinct paths from node u to all it\'s | UltraSonic | NORMAL | 2023-09-24T08:04:11.987521+00:00 | 2023-09-26T17:17:30.441360+00:00 | 234 | false | \n# Approach\n<!-- Describe your approach to solving the problem. -->\nDFS way postorder Tree traversal\n\n- Counting number of distinct paths from node u to all it\'s children and grandchildren in two buckets - \n bucket-1 : Paths with zero prime-value-node\n bucket-2 : Paths with one prime-value-node\n\nUsing the... | 2 | 0 | ['Tree', 'Depth-First Search', 'C++'] | 1 |
count-valid-paths-in-a-tree | simple dfs, optimized memory usage, concise code | simple-dfs-optimized-memory-usage-concis-muou | \n\n# Approach\n Describe your approach to solving the problem. \n1. using list \'prime\' to record whether the element at index i is a prime or not;\n2. using | akaka54321 | NORMAL | 2023-09-24T07:36:16.788749+00:00 | 2023-09-24T07:37:49.559481+00:00 | 57 | false | \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. using list \'prime\' to record whether the element at index i is a prime or not;\n2. using dictionary \'g\' to record the edge connection;\n3. with function \'dfs\' to traverse all the edges, it is for sure that all edges will be visited if we ... | 2 | 0 | ['Python3'] | 0 |
count-valid-paths-in-a-tree | Python Easy To understand , Thought Process | python-easy-to-understand-thought-proces-7qek | Intuition\n Describe your first thoughts on how to solve this problem. \nRequired to separate prime with non prime so used sieve of erastosthenes, build the tre | directioner1d | NORMAL | 2023-09-24T04:28:40.689856+00:00 | 2023-09-24T04:28:40.689879+00:00 | 97 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nRequired to separate prime with non prime so used sieve of erastosthenes, build the tree, noticed that if a non prime makes longest chain of length k containing all non primes then all the k non primes in the chain will also make chain of... | 2 | 0 | ['Python3'] | 0 |
count-valid-paths-in-a-tree | Fun Combinatorics Solution | fun-combinatorics-solution-by-babir-iita | Intuition\nTraverse the tree by dfs, for each node add the number of paths that has the current node as the top node.\n\n# Approach\nWe have to maintain two typ | Babir | NORMAL | 2024-10-23T19:26:26.426974+00:00 | 2024-10-23T19:26:26.427017+00:00 | 31 | false | # Intuition\nTraverse the tree by dfs, for each node add the number of paths that has the current node as the top node.\n\n# Approach\nWe have to maintain two types of paths for each node, where each path only goes upward from some node to the current node.\n1) The number of paths with exactly one node prime\n2) The nu... | 1 | 0 | ['C++'] | 0 |
count-valid-paths-in-a-tree | Easy C++ Solution || (Using DFS) Beats 96% ✅✅ | easy-c-solution-using-dfs-beats-96-by-ab-94n7 | Code\n\nclass Solution {\npublic:\n pair<long,long> helper(vector<int> adj[],int pa,int node,long long &r,vector<bool> &prime){\n pair<long,long> use= | Abhi242 | NORMAL | 2024-05-02T09:20:10.465318+00:00 | 2024-05-02T09:20:10.465340+00:00 | 64 | false | # Code\n```\nclass Solution {\npublic:\n pair<long,long> helper(vector<int> adj[],int pa,int node,long long &r,vector<bool> &prime){\n pair<long,long> use={!prime[node],prime[node]};\n for(int a:adj[node]){\n if(a==pa) continue;\n const auto &p=helper(adj,node,a,r,prime);\n ... | 1 | 0 | ['Dynamic Programming', 'Tree', 'Depth-First Search', 'C++'] | 0 |
count-valid-paths-in-a-tree | Sieve + DFS + DP | sieve-dfs-dp-by-karthikeysaxena-5bny | Intuition\n Describe your first thoughts on how to solve this problem. \nDFS + DP + Sieve\n\n# Approach\n Describe your approach to solving the problem. \nFor e | karthikeysaxena | NORMAL | 2024-04-16T09:19:21.857591+00:00 | 2024-04-16T09:19:21.857621+00:00 | 21 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nDFS + DP + Sieve\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nFor each non prime node, we find and store the count of non prime nodes connected to it. The for each prime node, we calculate the number of pair of ... | 1 | 0 | ['C++'] | 0 |
count-valid-paths-in-a-tree | rerooting + dp | rerooting-dp-by-abhijith89-o1qt | Intuition\n Tree rerooting\n\n# Complexity\n- Time complexity:\n O(nlog(logn)) + O(n)\n\n- Space complexity:\n O(n)\n\n# Code\n\nclass Solution {\npublic:\n | abhijith89 | NORMAL | 2024-02-06T06:42:38.620322+00:00 | 2024-02-06T06:42:38.620393+00:00 | 28 | false | # Intuition\n Tree rerooting\n\n# Complexity\n- Time complexity:\n O(nlog(logn)) + O(n)\n\n- Space complexity:\n O(n)\n\n# Code\n```\nclass Solution {\npublic:\n vector<vector<int>>g;\n vector<int>prime;\n vector<long long> child,cnt;\n long long ans=0;\n void seive(int n){\n prime[0]=0;\n ... | 1 | 0 | ['C++'] | 0 |
count-valid-paths-in-a-tree | C++ Solution - Like DSU (more like bruteforce) | c-solution-like-dsu-more-like-bruteforce-iccx | Intuition\nSee, we have to find paths which are having only 1 prime number, so, for all prime number nodes, if we are able to find pool of non prime nodes count | kingsman007 | NORMAL | 2023-10-23T07:48:02.219877+00:00 | 2023-10-23T07:50:23.615512+00:00 | 71 | false | # Intuition\nSee, we have to find paths which are having only 1 prime number, so, for all prime number nodes, if we are able to find pool of non prime nodes count which are connected to it, we can easiely find answer for that node using permutations. Say, for a prime node, three connected components of non prime nodes ... | 1 | 0 | ['Breadth-First Search', 'C++'] | 1 |
count-valid-paths-in-a-tree | Rust/Python. Almost linear time with detailed explanation. Beats 100% | rustpython-almost-linear-time-with-detai-4pa9 | Intuition\n\nYou can easily see that you do not care about values in the tree, the only thing you care is whether the node is prime or no. So we can just consid | salvadordali | NORMAL | 2023-09-26T00:08:51.318869+00:00 | 2023-09-26T00:08:51.318888+00:00 | 10 | false | # Intuition\n\nYou can easily see that you do not care about values in the tree, the only thing you care is whether the node is prime or no. So we can just consider the graph with only zeros (not-primes) and ones (primes). I will ignore the part how to find whether something is prime (the most efficient is to implemetn... | 1 | 0 | ['Python', 'Rust'] | 0 |
count-valid-paths-in-a-tree | DFS/DP + Sieve of Eratosthenes. beats 99% time 100% memory. With clear comments in code. | dfsdp-sieve-of-eratosthenes-beats-99-tim-i7ca | Intuition\n Describe your first thoughts on how to solve this problem. \nUsually for problem related to ALL PATH IN TREE, we can use DFS.\n# Approach\n Describe | DongmingShen | NORMAL | 2023-09-25T03:59:36.883440+00:00 | 2023-09-25T03:59:36.883465+00:00 | 591 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nUsually for problem related to ALL PATH IN TREE, we can use DFS.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nBefore solving the question, to effectively check prime, we need a prime tester => Sieve of Eratosthene... | 1 | 0 | ['Math', 'Dynamic Programming', 'Tree', 'Depth-First Search', 'Graph', 'Python3'] | 0 |
count-valid-paths-in-a-tree | Python/C++, dfs solution with explanation | pythonc-dfs-solution-with-explanation-by-218s | dfs\n\n\n\nwe can eunmerate each prime node,\nand count valid paths will pass through this node,\nso, we should node size of connected component consit of non-p | shun6096tw | NORMAL | 2023-09-24T09:47:40.477594+00:00 | 2023-09-25T08:16:50.524723+00:00 | 214 | false | ### dfs\n\n\n\nwe can eunmerate each prime node,\nand count valid paths will pass through this node,\nso, we should node size of connected component consit of non-prime nodes connect to this node,\nthe nodes in... | 1 | 0 | ['Math', 'Depth-First Search', 'C', 'Python'] | 0 |
count-valid-paths-in-a-tree | Rerooting of Tree + underStanding of maths. + sieve algo | rerooting-of-tree-understanding-of-maths-vwde | Intuition\n Describe your first thoughts on how to solve this problem. \n- we have to calculate the answer of number of path from each of the node which are pri | aryan20022003 | NORMAL | 2023-09-24T07:09:25.972916+00:00 | 2023-09-24T07:09:25.972944+00:00 | 434 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- we have to calculate the answer of number of path from each of the node which are prime \n- For that we need to know the wheather the provided node is prime or not so we can precompute the using sieve algo\n- now let say I know the answ... | 1 | 0 | ['Math', 'Tree', 'Depth-First Search', 'C++'] | 0 |
count-valid-paths-in-a-tree | JAVA DP ON TREES || EASY SOLUTION | java-dp-on-trees-easy-solution-by-akash0-cc7x | \nclass Solution {\n public long countPaths(int n, int[][] edges) {\n long dp[][]=new long[n+1][3];\n ArrayList<ArrayList<Integer>> child=new A | akash0228 | NORMAL | 2023-09-24T05:23:52.281397+00:00 | 2023-09-24T05:23:52.281427+00:00 | 162 | false | ```\nclass Solution {\n public long countPaths(int n, int[][] edges) {\n long dp[][]=new long[n+1][3];\n ArrayList<ArrayList<Integer>> child=new ArrayList<>();\n \n for(int i=0;i<=n;i++){\n child.add(new ArrayList<>());\n }\n \n for(int i=0;i<n-1;i++){\n ... | 1 | 0 | ['Dynamic Programming', 'Tree', 'Java'] | 1 |
count-valid-paths-in-a-tree | C# Union-Find + Math Theory | c-union-find-math-theory-by-enze_zhang-iw06 | Code\n\npublic class Solution {\n Dictionary<int, int> map = new();\n const int N = (int)1e5 + 10, M = N * 2;\n int[] h = new int[N], e = new int[M], n | enze_zhang | NORMAL | 2023-09-24T04:32:58.427829+00:00 | 2023-09-24T04:32:58.427864+00:00 | 35 | false | # Code\n```\npublic class Solution {\n Dictionary<int, int> map = new();\n const int N = (int)1e5 + 10, M = N * 2;\n int[] h = new int[N], e = new int[M], ne = new int[M];\n int[] cr = new int[N], node = new int[N];\n int idx = 0;\n \n public long CountPaths(int n, int[][] edges) {\n Sieve(n... | 1 | 0 | ['C#'] | 0 |
count-valid-paths-in-a-tree | [Python] Only use BFS | python-only-use-bfs-by-m2h1b8-sxqj | \n# Code\n\nclass Solution:\n def countPaths(self, n: int, edges: List[List[int]]) -> int:\n if n <= 2:\n return n - 1\n G = default | m2h1b8 | NORMAL | 2023-09-24T04:26:30.920735+00:00 | 2023-09-24T04:28:41.755039+00:00 | 59 | false | \n# Code\n```\nclass Solution:\n def countPaths(self, n: int, edges: List[List[int]]) -> int:\n if n <= 2:\n return n - 1\n G = defaultdict(list)\n for a, b in edges:\n G[a].append(b)\n G[b].append(a)\n \n isPrime = [1] * (n + 1)\n isPrime[0]... | 1 | 0 | ['Breadth-First Search', 'Python3'] | 0 |
count-valid-paths-in-a-tree | Simple cpp solution with explanation | simple-cpp-solution-with-explanation-by-6vczg | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | Code_Of_Duty | NORMAL | 2023-09-24T04:10:08.776560+00:00 | 2023-09-24T04:10:08.776590+00:00 | 148 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | 0 | ['Math', 'Dynamic Programming', 'Depth-First Search', 'C++'] | 0 |
count-valid-paths-in-a-tree | Math + Components of non-prime nodes | math-components-of-non-prime-nodes-by-ce-6vwl | Intuition\n Describe your first thoughts on how to solve this problem. \nWe can change our perspectives a bit to make this problem easier: for every node that i | CelonyMire | NORMAL | 2023-09-24T04:06:32.371284+00:00 | 2023-09-24T04:06:32.371308+00:00 | 155 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe can change our perspectives a bit to make this problem easier: **for every node that is a prime number, how many pairs of nodes of different "components" of the prime node can we make"?\n\nHere we define a component as a group of direc... | 1 | 0 | ['Union Find', 'Number Theory', 'C++'] | 0 |
count-valid-paths-in-a-tree | Lobster? Count "cliques" & "Sum of pair-wise products" | lobster-count-cliques-sum-of-pair-wise-p-fvu4 | Intuition\n"Lobster Graph" may be visually similar although not exactly identical.\n\n# Approach\n1) BFS and find how many non-prime nodes can be reached. Get a | quadpixels | NORMAL | 2023-09-24T04:04:59.004295+00:00 | 2023-09-24T04:04:59.004323+00:00 | 107 | false | # Intuition\n"Lobster Graph" may be visually similar although not exactly identical.\n\n# Approach\n1) BFS and find how many non-prime nodes can be reached. Get a list of # of reachable nodes.\n * Note: cache the results along the way\n2) Apply an algorithm to compute the sum of pairwise products: ((Sum of elements)^2... | 1 | 0 | ['C++'] | 0 |
count-valid-paths-in-a-tree | Union Find || C++ | union-find-c-by-jakevk-79h4 | Group composite nodes together using union find.\n\nThen iterate over primes and use math to count paths going through prime nodes.\n\n\nclass Solution {\npubli | jakevk | NORMAL | 2023-09-24T04:01:27.935015+00:00 | 2023-09-24T04:05:48.626892+00:00 | 254 | false | Group composite nodes together using union find.\n\nThen iterate over primes and use math to count paths going through prime nodes.\n\n```\nclass Solution {\npublic:\n #define ll long long\n #define N 100001\n \n int p[N], sz[N], is_prime[N];\n \n // dsu helper functions (template)\n int find(int v... | 1 | 0 | ['C++'] | 0 |
count-valid-paths-in-a-tree | Python Hard | python-hard-by-lucasschnee-dn5w | null | lucasschnee | NORMAL | 2025-02-11T02:08:51.393334+00:00 | 2025-02-11T02:08:51.393334+00:00 | 3 | false | ```python3 []
class UnionFind:
def __init__(self, N):
self.count = N
self.parent = [i for i in range(N)]
self.rank = [1] * N
def find(self, p):
if p != self.parent[p]:
self.parent[p] = self.find(self.parent[p])
return self.par... | 0 | 0 | ['Python3'] | 0 |
count-valid-paths-in-a-tree | ◈ Python ◈ Tree ◈ DP | python-tree-dp-by-zurcalled_suruat-x8n8 | Glossary of Terms and SymbolsP(x): A boolean function indicating if node x is prime.
C(x): The set of children of node x.
VP0(x): The number of valid paths en | zurcalled_suruat | NORMAL | 2025-01-14T18:10:29.675452+00:00 | 2025-01-14T18:15:02.154779+00:00 | 8 | false |
#### Glossary of Terms and Symbols
$P(x)$: A boolean function indicating if node $x$ is prime.
$C(x)$: The set of children of node $x$.
$VP_0(x)$: The number of valid paths ending at $x$ with 0 prime nodes.
$VP_1(x)$: The number of valid paths ending at $x$ with 1 prime node.
$IP(x)$: The number of valid internal pa... | 0 | 0 | ['Dynamic Programming', 'Tree', 'Python3'] | 0 |
count-valid-paths-in-a-tree | Easy C++ solution using DSU. | easy-c-solution-using-dsu-by-aadikashyap-h2cg | IntuitionApproachmerge all the non-prime nodes for a subtree into a component and using these non-prime nodes we can make a path to the prime nodes.Complexity
T | spidycoder178 | NORMAL | 2024-12-20T12:37:06.124414+00:00 | 2024-12-20T12:37:06.124414+00:00 | 9 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
merge all the non-prime nodes for a subtree into a component and using these non-prime nodes we can make a path to the prime nodes.
# Complexity
- Time complexity: O(N)
<!... | 0 | 0 | ['Math', 'Dynamic Programming', 'Tree', 'Depth-First Search', 'Union Find', 'Number Theory', 'C++'] | 0 |
count-valid-paths-in-a-tree | C++ Solution | c-solution-by-jeffrey288-yamf | Approach\n- think of the number of paths passing through each prime number node p\n - the path must pass through two distinct edges incident to p\n - if y | Jeffrey288 | NORMAL | 2024-10-17T14:27:08.764911+00:00 | 2024-10-17T14:27:08.764944+00:00 | 6 | false | # Approach\n- think of the number of paths passing through each prime number node `p`\n - the path must pass through two distinct edges incident to `p`\n - if you remove the prime number nodes, you can segment the whole tree into multiple connected components\n - the start and end nodes must come from **two di... | 0 | 0 | ['C++'] | 0 |
count-valid-paths-in-a-tree | Elegant DSU merge composite subgraphs | elegant-dsu-merge-composite-subgraphs-by-nxhi | \nMAX = 1 + 10 ** 5\n\nv = [False] * MAX\nsp = [0] * MAX\n\nfor i in range(2, MAX, 2):\n sp[i] = 2\n\nfor i in range(3, MAX, 2):\n if not v[i]:\n s | theabbie | NORMAL | 2024-10-15T03:08:54.521250+00:00 | 2024-10-15T03:08:54.521271+00:00 | 1 | false | ```\nMAX = 1 + 10 ** 5\n\nv = [False] * MAX\nsp = [0] * MAX\n\nfor i in range(2, MAX, 2):\n sp[i] = 2\n\nfor i in range(3, MAX, 2):\n if not v[i]:\n sp[i] = i\n j = i\n while j * i < MAX:\n if not v[j * i]:\n v[j * i] = True\n sp[j * i] = i\n ... | 0 | 0 | ['Python'] | 0 |
count-valid-paths-in-a-tree | DFS/DP + Sieve of Eratosthenes O(n) clean C++ code with comments | dfsdp-sieve-of-eratosthenes-on-clean-c-c-5p51 | Intuition\nAt each node, we need to compute 2 sums -\n\n1. dp[0][u]: number of vertical paths starting at u that contain no prime number (0 primes => index 0 fo | user9276g | NORMAL | 2024-10-09T16:51:53.343893+00:00 | 2024-10-09T16:58:31.212299+00:00 | 4 | false | # Intuition\nAt each node, we need to compute 2 sums -\n\n1. `dp[0][u]`: number of vertical paths starting at `u` that contain no prime number (0 primes => index 0 for convention)\n2. `dp[1][u]`: number of vertical paths starting at `u` that contain exactly 1 prime number (1 prime => index 1 for convention)\n\nThese su... | 0 | 0 | ['Dynamic Programming', 'Tree', 'Depth-First Search', 'C++'] | 0 |
count-valid-paths-in-a-tree | C++ | Simple Solution | Sieve | DFS | c-simple-solution-sieve-dfs-by-deepakcha-kg7e | \n\n# Code\ncpp []\nint MAX_N = 100001;\nvector<int> is_prime(MAX_N, true);\nclass Solution {\npublic:\n\n Solution(){\n // Generate Sieve\n if | deepakchaurasiya | NORMAL | 2024-09-21T19:33:39.247317+00:00 | 2024-09-21T19:33:39.247339+00:00 | 3 | false | \n\n# Code\n```cpp []\nint MAX_N = 100001;\nvector<int> is_prime(MAX_N, true);\nclass Solution {\npublic:\n\n Solution(){\n // Generate Sieve\n if(!is_prime[1]) return;\n is_prime[0] = is_prime[1] = 0;\n for(int i=2; i*i<MAX_N; i++){\n if(is_prime[i]){\n for(int ... | 0 | 0 | ['C++'] | 0 |
count-valid-paths-in-a-tree | DSU solution | dsu-solution-by-rahulkumar665550-m8z4 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | rahulkumar665550 | NORMAL | 2024-09-04T04:09:23.673666+00:00 | 2024-09-04T04:09:23.673694+00:00 | 3 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | 0 | ['C++'] | 0 |
count-valid-paths-in-a-tree | DSU_________________________________________________________________AVS | dsu_____________________________________-lff7 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | Vishal1431 | NORMAL | 2024-08-29T22:27:22.309295+00:00 | 2024-08-29T22:27:22.309323+00:00 | 1 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | 0 | ['Tree', 'Depth-First Search', 'C++'] | 0 |
count-valid-paths-in-a-tree | Simplest Solution with Easy Explanation ( using DSU) | simplest-solution-with-easy-explanation-nmt9f | Intuition\n Describe your first thoughts on how to solve this problem. \nWe have to calculate number of pair of vertices which have exactly one prime node in th | vikram_8009 | NORMAL | 2024-08-20T07:10:21.831943+00:00 | 2024-08-20T07:10:21.831974+00:00 | 1 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe have to calculate number of pair of vertices which have exactly one prime node in their simple path. We can solve this problem by reconstructing the graph .\n# Approach\n<!-- Describe your approach to solving the problem. -->\n. We wil... | 0 | 0 | ['Union Find', 'Number Theory', 'C++'] | 0 |
count-valid-paths-in-a-tree | Rust: sieve & DFS | rust-sieve-dfs-by-minamikaze392-tqlg | Approach\n1. Find all prime numbers which are <= n with Sieve of Eratosthenes (O(n)).\n2. Pick a node and do recursive DFS. The recursive function returns three | Minamikaze392 | NORMAL | 2024-07-23T07:45:29.803193+00:00 | 2024-07-23T07:45:40.004355+00:00 | 3 | false | # Approach\n1. Find all prime numbers which are `<= n` with Sieve of Eratosthenes ($$O(n)$$).\n2. Pick a node and do recursive DFS. The recursive function returns three values:\n- `ans`: Number of all **valid** paths in subtree.\n- `one`: Number of all paths which **ends** at the subtree **root**, and involves **one** ... | 0 | 0 | ['Dynamic Programming', 'Depth-First Search', 'Rust'] | 0 |
count-valid-paths-in-a-tree | Simple DFS || No DP|| No DSU||O(N) Solution | simple-dfs-no-dp-no-dsuon-solution-by-sd-zbb2 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | Sdas_1905 | NORMAL | 2024-07-04T19:45:14.043975+00:00 | 2024-07-04T19:45:14.044004+00:00 | 14 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(sqrt(n))\n<!-- Add your space complexity here, e... | 0 | 0 | ['Depth-First Search', 'C++'] | 0 |
count-valid-paths-in-a-tree | Recursive solution | | easy to understand | | c++ | recursive-solution-easy-to-understand-c-sdgp3 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | dl720125 | NORMAL | 2024-07-01T19:16:09.256148+00:00 | 2024-07-01T19:16:09.256167+00:00 | 5 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | 0 | ['C++'] | 0 |
count-valid-paths-in-a-tree | C++ || DSU || Sieve | c-dsu-sieve-by-akash92-mwq2 | \n# Approach\n Describe your approach to solving the problem. \nUse sieve to find prime numbers in range [1,n]. Then use dsu to first form union of all non prim | akash92 | NORMAL | 2024-06-01T05:15:58.295361+00:00 | 2024-06-01T05:15:58.295404+00:00 | 5 | false | \n# Approach\n<!-- Describe your approach to solving the problem. -->\nUse sieve to find prime numbers in range [1,n]. Then use dsu to first form union of all non prime nodes(node and adjNode are not prime), then form union where node is prime, adjNode is not prime.if(adjNode!=prime) find its ulp, and see if it is visi... | 0 | 0 | ['Union Find', 'C++'] | 0 |
count-valid-paths-in-a-tree | Easy Solution :) | easy-solution-by-user20222-miya | \n# Code\n\nvector<bool>pms(100005,1);\nauto a=[]()->int{\n pms[0]=0;\n pms[1]=0;\n for(int i=2;i<=1e5;i++){\n if(pms[i]){\n for(int | user20222 | NORMAL | 2024-05-18T18:38:17.729265+00:00 | 2024-05-18T18:38:17.729298+00:00 | 3 | false | \n# Code\n```\nvector<bool>pms(100005,1);\nauto a=[]()->int{\n pms[0]=0;\n pms[1]=0;\n for(int i=2;i<=1e5;i++){\n if(pms[i]){\n for(int j=i+i;j<=1e5;j+=i){\n pms[j]=0;\n }\n }\n }\n return 0;\n}();\n\nclass Solution {\npublic:\n long long ans = 0;\n ... | 0 | 0 | ['C++'] | 0 |
count-valid-paths-in-a-tree | Dp + tree py3 | dp-tree-py3-by-maxorgus-u6a7 | This is really a hard one, I first tried dp by calculating number of paths without prime nodes starting from each prime node, but that gave me TLE in the last f | MaxOrgus | NORMAL | 2024-04-25T04:39:14.688296+00:00 | 2024-04-25T04:39:14.688332+00:00 | 18 | false | This is really a hard one, I first tried dp by calculating number of paths without prime nodes starting from each prime node, but that gave me TLE in the last few test cases.\n\nThen I followed the hint to restructure the tree so that edges be visited less, it barely passed. Also note that precomputing all prime number... | 0 | 0 | ['Dynamic Programming', 'Tree', 'Python3'] | 0 |
count-valid-paths-in-a-tree | My code shows exceed time limite for n=10000; anyone has any advice how to make it pass thank you | my-code-shows-exceed-time-limite-for-n10-x0bm | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | shirley716 | NORMAL | 2024-03-25T01:15:23.096019+00:00 | 2024-03-25T01:15:23.096048+00:00 | 6 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | 0 | ['Java'] | 0 |
count-valid-paths-in-a-tree | dfs | dfs-by-kebao-quan-hhdr | \nclass Solution {\n static constexpr uint32_t N = 100001;\n bool isPrime[100001];\n void dfs(vector<vector<int>> &G, vector<int> &seen, int i, int pre | kebao-quan | NORMAL | 2024-03-15T22:22:01.227091+00:00 | 2024-03-15T22:22:01.227112+00:00 | 2 | false | ```\nclass Solution {\n static constexpr uint32_t N = 100001;\n bool isPrime[100001];\n void dfs(vector<vector<int>> &G, vector<int> &seen, int i, int pre) {\n seen.emplace_back(i);\n\n for (int j : G[i]) {\n if (isPrime[j] || j == pre) continue;\n dfs(G, seen, j, i);\n ... | 0 | 0 | ['C++'] | 0 |
count-valid-paths-in-a-tree | Union Find and Counting | union-find-and-counting-by-trusty42-69dq | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | trusty42 | NORMAL | 2024-02-28T12:57:32.207141+00:00 | 2024-02-28T12:57:32.207169+00:00 | 13 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | 0 | ['Java'] | 0 |
count-valid-paths-in-a-tree | Go solution || Beat 100% | go-solution-beat-100-by-constantinejin-94c1 | Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(n)\n\n# Code\n\nconst mx = 1e5 + 1\n\nvar isPrime [mx]bool\n\nfunc init() {\n\tvar primes []int\n\t | ConstantineJin | NORMAL | 2024-02-27T09:59:17.885775+00:00 | 2024-02-27T09:59:17.885808+00:00 | 4 | false | # Complexity\n- Time complexity: $$O(n)$$\n\n- Space complexity: $$O(n)$$\n\n# Code\n```\nconst mx = 1e5 + 1\n\nvar isPrime [mx]bool\n\nfunc init() {\n\tvar primes []int\n\tfor i := 2; i < mx; i++ {\n\t\tisPrime[i] = true\n\t}\n\tfor i := 2; i < mx; i++ {\n\t\tif isPrime[i] {\n\t\t\tprimes = append(primes, i)\n\t\t}\n\... | 0 | 0 | ['Go'] | 0 |
count-valid-paths-in-a-tree | [Python3] Easy to understand solution | python3-easy-to-understand-solution-by-d-bqbt | Just leaving it here:\n\nBuild the directed graph where prime nodes point to non-prime nodes, then for each prime node count number of all possible routes and t | dilshodbek | NORMAL | 2024-02-10T12:32:14.420690+00:00 | 2024-02-10T12:36:46.901367+00:00 | 0 | false | Just leaving it here:\n\nBuild the directed graph where prime nodes point to non-prime nodes, then for each prime node count number of all possible routes and their combinations. \n\nLet me know if you need any clarification/explanation. \n\n\n```\nclass Solution:\n def countPaths(self, n: int, edges: List[List[int]... | 0 | 0 | [] | 0 |
count-valid-paths-in-a-tree | 98.82% Fastest Prime Sieve + DFS + Combinatorics O(n*log(log(n)) | 9882-fastest-prime-sieve-dfs-combinatori-he6f | Submission Link: https://leetcode.com/submissions/detail/1161565654/\n\n# Approach\n- Set up Sieve of Eratosthenes in O(nlog(log(n))) time for O(1) later access | jasonpyau | NORMAL | 2024-01-31T01:53:12.874760+00:00 | 2024-01-31T03:26:16.533660+00:00 | 5 | false | Submission Link: https://leetcode.com/submissions/detail/1161565654/\n\n# Approach\n- Set up Sieve of Eratosthenes in O(n*log(log(n))) time for O(1) later access.\n- Create Graph\n- Run DFS, maintain accurate values, and run some combinatorics on each node\'s descendent.\n\n\n# Complexity\n- Time complexity:\nO(n*log(l... | 0 | 0 | ['Depth-First Search', 'Combinatorics', 'Number Theory', 'C++'] | 0 |
count-valid-paths-in-a-tree | C++ Simple DFS O(nlogn) | c-simple-dfs-onlogn-by-user9107q-iqtm | \n\n# Approach\n- Compute primes using sieve\n- For each non prime node, compute number of nodes in connected component, bounded by either prime node or leaf no | user9107q | NORMAL | 2024-01-03T17:50:43.407484+00:00 | 2024-01-03T17:51:16.657445+00:00 | 18 | false | \n\n# Approach\n- Compute primes using sieve\n- For each non prime node, compute number of nodes in connected component, bounded by either prime node or leaf node\n- For each prime node, compute number of valid paths passing through this node \n\n\n# Code\n```\n#define ll long long\nclass Solution {\npublic:\n void ... | 0 | 0 | ['Dynamic Programming', 'Depth-First Search', 'C++'] | 0 |
count-valid-paths-in-a-tree | Brute force optimized | brute-force-optimized-by-kingsenior-5g74 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | kingsenior | NORMAL | 2024-01-02T16:46:31.971580+00:00 | 2024-01-02T16:46:31.971612+00:00 | 2 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ --> \n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -... | 0 | 0 | ['C++'] | 0 |
count-valid-paths-in-a-tree | O(N) time complecity C++ solution | on-time-complecity-c-solution-by-chan818-28ff | There are two paths to satisfy the question conditions.\n1. one endpoint is a prime, the other endpoint is not a prime\n2. both endpoints are not primes, the pa | chan818234 | NORMAL | 2023-12-30T09:33:30.190288+00:00 | 2023-12-30T12:00:13.351333+00:00 | 1 | false | There are two paths to satisfy the question conditions.\n1. one endpoint is a prime, the other endpoint is not a prime\n2. both endpoints are not primes, the path cross a prime.\n\nA path between two primes doesn\'t help for this problem.\n\nTraversing each prime number, we can use the disjoint set to count the states ... | 0 | 0 | ['Union Find'] | 0 |
count-valid-paths-in-a-tree | Only one DFS solution | C++ | only-one-dfs-solution-c-by-hagoromo-1w8c | \n# Code\n\nclass Solution {\n vector<bool> sieve(int n){\n vector<bool> v(n+1,true);\n v[1]=false;\n for(int i=2;i*i<=n;i++){\n | Hagoromo | NORMAL | 2023-12-10T10:37:32.697191+00:00 | 2023-12-10T10:37:32.697223+00:00 | 5 | false | \n# Code\n```\nclass Solution {\n vector<bool> sieve(int n){\n vector<bool> v(n+1,true);\n v[1]=false;\n for(int i=2;i*i<=n;i++){\n if(v[i]==true){\n for(int j=i*2;j<=n;j+=i) v[j]=false;\n }\n }\n return v;\n }\n pair<int,int> dfs(vector<v... | 0 | 0 | ['C++'] | 0 |
count-valid-paths-in-a-tree | [C++] DSU | c-dsu-by-quater_nion-rvb9 | \nclass Solution {\n vector<int> parent;\n vector<int> size;\n int find(int a){\n return parent[a]==a ? a : parent[a] = find(parent[a]);\n }\ | quater_nion | NORMAL | 2023-11-14T18:56:03.024314+00:00 | 2023-11-14T18:56:03.024345+00:00 | 3 | false | ```\nclass Solution {\n vector<int> parent;\n vector<int> size;\n int find(int a){\n return parent[a]==a ? a : parent[a] = find(parent[a]);\n }\n void connect(int a, int b){\n int parentA = find(a);\n int parentB = find(b);\n if(parentA==parentB) return;\n if(size[paren... | 0 | 0 | ['Union Find', 'C++'] | 0 |
count-valid-paths-in-a-tree | DFS + Casework in O(n) (NO DSU OR LCA!) | dfs-casework-in-on-no-dsu-or-lca-by-gtsm-w1sb | Intuition\n Describe your first thoughts on how to solve this problem. \nUnfortunately, I was not clever enough to come up with a slick LCA or DSU algo. I just | gtsmarmy | NORMAL | 2023-10-12T18:28:52.720743+00:00 | 2023-10-14T22:39:18.725911+00:00 | 21 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nUnfortunately, I was not clever enough to come up with a slick LCA or DSU algo. I just used casework (and a whole lot of that!) along with some simple maths.\n# Approach\n\nFirst, we use a sieve (any *fast* sieve works, I used Eratosthene... | 0 | 0 | ['Math', 'Depth-First Search', 'Recursion', 'C++'] | 0 |
count-valid-paths-in-a-tree | c++ tree-dp and dfs solution | c-tree-dp-and-dfs-solution-by-vedantnaud-lo0s | Intuition\n Describe your first thoughts on how to solve this problem. \nUse eratosthanes for is_prime array.\nfind the valid and non valid paths down a node in | vedantnaudiyal | NORMAL | 2023-10-12T09:04:52.942377+00:00 | 2023-10-12T09:04:52.942400+00:00 | 15 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nUse eratosthanes for is_prime array.\nfind the valid and non valid paths down a node in the tree using dfs and a dp table starting from any node and then for each node find the horizontal paths passing through the node extending from one ... | 0 | 0 | ['C++'] | 0 |
count-valid-paths-in-a-tree | scala | scala-by-len_master-sjim | scala\nimport scala.collection.mutable\n\nobject Solution {\n private val MX: Int = 1e5.toInt\n private val np: Array[Boolean] = Array.ofDim[Boolean](MX + 1)\ | len_master | NORMAL | 2023-10-06T09:50:07.218183+00:00 | 2023-10-06T09:50:07.218203+00:00 | 1 | false | ``` scala\nimport scala.collection.mutable\n\nobject Solution {\n private val MX: Int = 1e5.toInt\n private val np: Array[Boolean] = Array.ofDim[Boolean](MX + 1)\n\n np(1) = true\n (2 to math.sqrt(MX).toInt)\n .withFilter(i => !np(i))\n .foreach(i => (i * i to MX by i)\n .foreach(j => np(j) = true))\n\n ... | 0 | 0 | ['Scala'] | 0 |
count-valid-paths-in-a-tree | Simple Solution Using DSU and DFS | simple-solution-using-dsu-and-dfs-by-wol-vgff | \n# Code\n\nstruct DSU{\n vector<int> par, size;\n DSU(int n){\n par.resize(n+1); size.resize(n+1, 1);\n for(int i=0;i<=n;i++) par[i] = i;\n | Wolfester | NORMAL | 2023-10-04T07:47:43.935878+00:00 | 2023-10-04T07:47:43.935898+00:00 | 21 | false | \n# Code\n```\nstruct DSU{\n vector<int> par, size;\n DSU(int n){\n par.resize(n+1); size.resize(n+1, 1);\n for(int i=0;i<=n;i++) par[i] = i;\n }\n int find(int a){\n if(par[a] == a) return a;\n return par[a] = find(par[a]);\n }\n void Union(int a, int b){\n a = find... | 0 | 0 | ['Depth-First Search', 'Union Find', 'C++'] | 0 |
count-valid-paths-in-a-tree | C++ || Crystal Clear Code | c-crystal-clear-code-by-raunakmishra1243-t2ih | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nhttps://leetcode.com/pr | raunakmishra1243 | NORMAL | 2023-10-02T23:36:56.687143+00:00 | 2023-10-02T23:36:56.687163+00:00 | 69 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n[https://leetcode.com/problems/count-valid-paths-in-a-tree/solutions/4082775/dfs-tree-dp-in-c-java-python/](Reference )\nIn DFS/Tree DP, return 2 values:\n(1) The numb... | 0 | 0 | ['C++'] | 1 |
count-valid-paths-in-a-tree | C# Union finder ,Sieve | c-union-finder-sieve-by-julian_meng_chan-thtc | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | julian_meng_chang | NORMAL | 2023-10-02T01:46:32.595612+00:00 | 2023-10-02T01:46:32.595641+00:00 | 4 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | 0 | ['C#'] | 0 |
count-valid-paths-in-a-tree | Python | DFS | O(n) | python-dfs-on-by-aryonbe-jw5q | Code\n\nfrom collections import defaultdict\nclass Solution:\n def countPaths(self, n: int, edges: List[List[int]]) -> int:\n G = defaultdict(list)\n | aryonbe | NORMAL | 2023-10-02T00:38:06.127559+00:00 | 2023-10-02T00:38:06.127590+00:00 | 13 | false | # Code\n```\nfrom collections import defaultdict\nclass Solution:\n def countPaths(self, n: int, edges: List[List[int]]) -> int:\n G = defaultdict(list)\n for u, v in edges:\n G[u].append(v)\n G[v].append(u)\n prime = [True]*(n+1)\n prime[1] = False\n for i in... | 0 | 0 | ['Python3'] | 0 |
count-valid-paths-in-a-tree | C++ dfs down-to-top and top-to-down information flow | c-dfs-down-to-top-and-top-to-down-inform-qn3u | We first get information from the child up to the parent and then from the parent down to the child so that every node knows how many paths are there to leaves. | vvhack | NORMAL | 2023-09-29T19:59:04.742867+00:00 | 2023-09-29T20:03:33.439632+00:00 | 8 | false | We first get information from the child up to the parent and then from the parent down to the child so that every node knows how many paths are there to leaves.\nPlease note that "leaf" here refers to nodes that have no non-prime nodes.\nWe then go to every prime node and find out how many paths to leaves are there.\n`... | 0 | 0 | [] | 0 |
count-valid-paths-in-a-tree | Count Valid Paths in a Tree | C++ | Sieve | DFS | Clusters | count-valid-paths-in-a-tree-c-sieve-dfs-taxfz | Intuition\n Describe your first thoughts on how to solve this problem. \n1. Prime Number Sieve (Sieve of Eratosthenes):\n - Initially, it precomputes prime an | ArcticLights | NORMAL | 2023-09-29T19:50:46.344561+00:00 | 2023-09-29T19:50:46.344592+00:00 | 35 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n1. **Prime Number Sieve (Sieve of Eratosthenes):**\n - Initially, it precomputes prime and non-prime numbers up to \'n\' using the Sieve algorithm.\n - This step is crucial to later identify which nodes in the graph are prime and non-... | 0 | 0 | ['Dynamic Programming', 'Tree', 'Depth-First Search', 'C++'] | 0 |
count-valid-paths-in-a-tree | AAHHHHHH | Video Walkthrough | Python | aahhhhhh-video-walkthrough-python-by-bnc-rt76 | Click Here For Video Walkthrough\n\nclass Solution:\n def countPaths(self, n: int, edges: List[List[int]]) -> int:\n #n, m = len(edges)\n #SPAC | bnchn | NORMAL | 2023-09-28T18:53:49.981444+00:00 | 2023-09-28T18:53:49.981472+00:00 | 16 | false | [Click Here For Video Walkthrough](https://youtu.be/8YNvhcQB_mM)\n```\nclass Solution:\n def countPaths(self, n: int, edges: List[List[int]]) -> int:\n #n, m = len(edges)\n #SPACE: O(n+ m)\n #TIME: O(n* log(log(n)) + m + n)\n res = 0\n \n def isPrime(x):\n if x < ... | 0 | 1 | ['Python'] | 0 |
count-valid-paths-in-a-tree | Group non prime nodes + calculate combinations | group-non-prime-nodes-calculate-combinat-g1ym | idea : find the just non prime nodes connected to prime nodes and calculate combinations of path from each non prime nodes to other non prime nodes\n\nfirst cal | demon_code | NORMAL | 2023-09-28T13:20:56.352905+00:00 | 2023-09-28T13:20:56.352926+00:00 | 11 | false | idea : find the just non prime nodes connected to prime nodes and calculate combinations of path from each non prime nodes to other non prime nodes\n\nfirst calculate all prime number in range of n put it in a unordered_set\n\ncombine all nodes which do have have any link with prime nodes \nand assign rank (level of de... | 0 | 0 | ['Tree', 'Union Find'] | 0 |
count-valid-paths-in-a-tree | Sieve of Eratosthenes | dfs | dp | sieve-of-eratosthenes-dfs-dp-by-pr1yansh-thr7 | Time complexity : O(NlogN)\n\n# Code\n\nclass Solution {\npublic:\n \n vector<long long> isPrime, dp;\n\n //dfs to find number of non prime nodes\n | pr1yanshu | NORMAL | 2023-09-28T12:12:39.267184+00:00 | 2023-09-28T17:03:19.255491+00:00 | 13 | false | **Time complexity :** **O(NlogN)**\n\n# Code\n```\nclass Solution {\npublic:\n \n vector<long long> isPrime, dp;\n\n //dfs to find number of non prime nodes\n long long dfs(vector<int>g[],int node, int par=-1){\n int count=1;\n for(auto &i: g[node]){\n if(i==par or isPrime[i])contin... | 0 | 0 | ['C++'] | 0 |
count-valid-paths-in-a-tree | DSU + Seive beats 100% of the java solution : Complexity ~ O(n) | dsu-seive-beats-100-of-the-java-solution-7qu4 | \nclass Solution {\n int []prime;\n int []sizes;\n int []parent;\n List<List<Integer>> tree;\n \n public void create(int n) {\n for(int | namandeept | NORMAL | 2023-09-26T20:22:01.703871+00:00 | 2023-09-26T20:52:26.827591+00:00 | 7 | false | ```\nclass Solution {\n int []prime;\n int []sizes;\n int []parent;\n List<List<Integer>> tree;\n \n public void create(int n) {\n for(int i=1; i<=n; i++) {\n if(prime[i] == 0) {\n sizes[i] = 1;\n parent[i] = i;\n }\n }\n }\n \n ... | 0 | 0 | [] | 0 |
count-valid-paths-in-a-tree | A simple java dfs solution | a-simple-java-dfs-solution-by-dudadi-ac6o | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | dudadi | NORMAL | 2023-09-26T11:31:48.621049+00:00 | 2023-09-26T11:31:48.621077+00:00 | 21 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | 0 | ['Backtracking', 'Java'] | 0 |
count-valid-paths-in-a-tree | DFS and maintaining no. of paths with 1 and 0 prime numbers | dfs-and-maintaining-no-of-paths-with-1-a-qkvp | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | Code_Hard02 | NORMAL | 2023-09-25T22:16:19.181962+00:00 | 2023-09-25T22:16:19.181986+00:00 | 15 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | 0 | ['C++'] | 0 |
count-valid-paths-in-a-tree | Rust DFS | rust-dfs-by-xiaoping3418-qt7h | Intuition\n Describe your first thoughts on how to solve this problem. \n1) Randam pick a node as the root, using DFS to calculate count = Vec<(i32, i32); n + 1 | xiaoping3418 | NORMAL | 2023-09-25T22:06:47.929124+00:00 | 2023-09-29T22:33:34.889033+00:00 | 2 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n1) Randam pick a node as the root, using DFS to calculate count = Vec<(i32, i32); n + 1>; where count[u] = (# of paths in the sub-tree with 0 primes starting from u, # of paths in the sub-tree with 1 prime starting from u).\n2) Assuming u... | 0 | 0 | ['Rust'] | 0 |
count-valid-paths-in-a-tree | C++ | dp on trees + sieve | c-dp-on-trees-sieve-by-harsh_malik-ocpv | \n# Complexity\n- Time complexity:\n Add your time complexity here, e.g. O(n) \n\nN logN\n\n\n- Space complexity:\n Add your space complexity here, e.g. O(n) \n | harsh_malik_ | NORMAL | 2023-09-25T17:08:31.526326+00:00 | 2023-09-25T17:08:31.526347+00:00 | 39 | false | \n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n```\nN logN\n```\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n```\n4*N\n```\n\n# Code\n```\n#define ll long long\nconst int N = 1e5 + 5 ; \n\nclass Solution {\npublic:\n bool isprime[N+1] ;\n... | 0 | 0 | ['Dynamic Programming', 'Tree', 'Number Theory', 'C++'] | 0 |
count-valid-paths-in-a-tree | My Solutions | my-solutions-by-hope_ma-u7dm | 1. Use the Sieve of Eratosthenes Algorithm to get all prime numbers from 1 to n, and then use the DP\n\n/**\n * Time Complexity: O(n)\n * Space Complexity: O(n) | hope_ma | NORMAL | 2023-09-25T15:46:11.603049+00:00 | 2023-09-26T03:10:02.084902+00:00 | 6 | false | **1. Use the `Sieve of Eratosthenes` Algorithm to get all prime numbers from `1` to `n`, and then use the DP**\n```\n/**\n * Time Complexity: O(n)\n * Space Complexity: O(n)\n */\nclass Solution {\n private:\n /**\n * <first>: the number of paths which contain just one node whose label is a prime number\n * <secon... | 0 | 0 | [] | 0 |
count-valid-paths-in-a-tree | solution | solution-by-hemanth-123-ecoe | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | hemanth-123 | NORMAL | 2023-09-25T15:13:25.256438+00:00 | 2023-09-25T15:13:25.256465+00:00 | 6 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | 0 | ['Python3'] | 0 |
count-valid-paths-in-a-tree | Python 3 O(n*n^0.5)) solution | python-3-onn05-solution-by-vladislav_zav-oat9 | \nclass Solution:\n def is_prime(self, num):\n if num == 1:\n return False\n for i in range(2, int(num ** (0.5)) + 1):\n | vladislav_zavadski | NORMAL | 2023-09-25T10:19:07.336565+00:00 | 2023-09-25T10:19:07.336586+00:00 | 7 | false | ```\nclass Solution:\n def is_prime(self, num):\n if num == 1:\n return False\n for i in range(2, int(num ** (0.5)) + 1):\n if num % i == 0:\n return False\n return True\n \n def countPaths(self, n: int, edges: List[List[int]]) -> int:\n graph = ... | 0 | 0 | ['Math', 'Depth-First Search', 'Graph', 'Python'] | 0 |
count-valid-paths-in-a-tree | Solution using DFS+DP | solution-using-dfsdp-by-ksbu_2003-zu88 | Intuition\n Describe your first thoughts on how to solve this problem. \ninitially i thought this can be solved by using DP approach\n# Approach\n Describe your | ksbu_2003 | NORMAL | 2023-09-25T08:32:48.962716+00:00 | 2023-09-25T08:32:48.962740+00:00 | 68 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\ninitially i thought this can be solved by using DP approach\n# Approach\n<!-- Describe your approach to solving the problem. -->\nusing dfs we can find number of prime paths containing a single prime and number of non-prime paths for each... | 0 | 0 | ['Dynamic Programming', 'Tree', 'Depth-First Search', 'C++'] | 1 |
count-valid-paths-in-a-tree | Single DFS | single-dfs-by-raj_tirtha-2kwg | Code\n\nclass Solution {\npublic:\n long long ans=0;\n void dfs(int node,vector<vector<int>> &adj,int parent,vector<long long> &dp1,vector<long long> &dp2 | raj_tirtha | NORMAL | 2023-09-25T07:50:38.974954+00:00 | 2023-09-25T07:50:38.974972+00:00 | 5 | false | # Code\n```\nclass Solution {\npublic:\n long long ans=0;\n void dfs(int node,vector<vector<int>> &adj,int parent,vector<long long> &dp1,vector<long long> &dp2,vector<bool> &isprime){\n long long cnt=0;\n long long temp=0;\n for(auto it:adj[node]){\n if(it==parent) continue;\n ... | 0 | 0 | ['C++'] | 0 |
count-valid-paths-in-a-tree | C++ DFS & Seive Intutive | c-dfs-seive-intutive-by-vinayag29-dbet | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | vinayag29 | NORMAL | 2023-09-25T05:29:44.748977+00:00 | 2023-09-25T05:29:44.749001+00:00 | 11 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | 0 | ['C++'] | 0 |
count-valid-paths-in-a-tree | Python - DP count with union find and dfs from primes | python-dp-count-with-union-find-and-dfs-33ogx | Intuition\nUse union find structure variation with counting.\nConnect only non prime nodes.\nThis way count of parent can give us count of non primes in compone | Ante_ | NORMAL | 2023-09-24T22:38:12.989358+00:00 | 2023-09-24T22:40:09.432295+00:00 | 33 | false | # Intuition\nUse union find structure variation with counting.\nConnect only non prime nodes.\nThis way count of parent can give us count of non primes in component. This can be used later to do dfs from prime nodes and have something like memoization effect for that search.\nLater on just do dfs from prime nodes and c... | 0 | 0 | ['Math', 'Depth-First Search', 'Union Find', 'Graph', 'Python', 'Python3'] | 0 |
count-valid-paths-in-a-tree | Python (Beats 12.5%) | python-beats-125-by-trentono-mt0k | \nfrom collections import defaultdict\n\nclass Solution:\n def countPaths(self, n: int, edges: List[List[int]]) -> int: \n primes = self.fi | trentono | NORMAL | 2023-09-24T20:40:04.544063+00:00 | 2023-09-24T20:40:04.544082+00:00 | 6 | false | ```\nfrom collections import defaultdict\n\nclass Solution:\n def countPaths(self, n: int, edges: List[List[int]]) -> int: \n primes = self.find_primes(n)\n prime_neighbors = defaultdict(list)\n for a, b in edges:\n if a in primes and b not in primes:\n prime... | 0 | 0 | ['Python3'] | 0 |
count-valid-paths-in-a-tree | Tree Rerooting || clean & neat solution || Easy to understand | tree-rerooting-clean-neat-solution-easy-fn4qb | Intuition\n Describe your first thoughts on how to solve this problem. \nTree rerooting is all you need!\n\n# Approach\n Describe your approach to solving the p | Varan03 | NORMAL | 2023-09-24T20:28:02.405680+00:00 | 2023-09-24T20:29:15.026554+00:00 | 37 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTree rerooting is all you need!\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Find all primes in the range [1,n]\n2. Root the tree at any node, let\'s say 1\n3. Run a dfs to calculate no of paths starting from... | 0 | 0 | ['C++'] | 0 |
count-valid-paths-in-a-tree | DSU Solution || Beats 100% | dsu-solution-beats-100-by-hacker_antace-d4tr | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | hacker_antace | NORMAL | 2023-09-24T17:07:53.802445+00:00 | 2023-09-24T17:07:53.802470+00:00 | 19 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | 0 | ['Union Find', 'Graph', 'C++'] | 0 |
count-valid-paths-in-a-tree | 100/100 Beats | 100100-beats-by-ranilmukesh-9oeg | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | ranilmukesh | NORMAL | 2023-09-24T16:06:03.506301+00:00 | 2023-09-24T16:06:03.506334+00:00 | 26 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | 0 | ['Java'] | 0 |
count-valid-paths-in-a-tree | Sieve of Eratosthenes + DFS with kinda DSU O(n) clean code | sieve-of-eratosthenes-dfs-with-kinda-dsu-w0q2 | \n using ll = long long;\n ll countPaths(int n, vector<vector<int>>& edges) {\n const auto getPrimes = [](int n) {\n vector<int> values( | dmitrii_bokovikov | NORMAL | 2023-09-24T14:36:08.158578+00:00 | 2023-09-24T19:39:00.631414+00:00 | 14 | false | ```\n using ll = long long;\n ll countPaths(int n, vector<vector<int>>& edges) {\n const auto getPrimes = [](int n) {\n vector<int> values(n + 1);\n iota(begin(values) + 2, end(values), 2);\n for (int i = 2; i <= n; i++) {\n if (values[i] == 0) {\n ... | 0 | 0 | ['C++'] | 0 |
count-valid-paths-in-a-tree | Good Question. | good-question-by-shyamprajapati2672-cs3p | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | shyamprajapati2672 | NORMAL | 2023-09-24T12:34:41.839454+00:00 | 2023-09-24T12:34:41.839484+00:00 | 44 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | 0 | ['Math', 'Dynamic Programming', 'Depth-First Search', 'C++'] | 0 |
count-valid-paths-in-a-tree | Java 29ms faster than 100% DFS | java-29ms-faster-than-100-dfs-by-sedak82-9v7z | Code\n\nclass Solution {\n boolean[] isPrime;\n List<Integer>[] treeEdges;\n long r;\n \n boolean[] preparePrime(int n) {\n // Sieve of Er | sedak82 | NORMAL | 2023-09-24T12:28:06.909819+00:00 | 2023-09-24T12:28:06.909839+00:00 | 24 | false | # Code\n```\nclass Solution {\n boolean[] isPrime;\n List<Integer>[] treeEdges;\n long r;\n \n boolean[] preparePrime(int n) {\n // Sieve of Eratosthenes <3 \n boolean[] isPrime = new boolean[n+1];\n long paths = 0;\n for (int i = 2; i < n+1; i++) { isPrime[i] = true; }\n ... | 0 | 0 | ['Java'] | 0 |
count-valid-paths-in-a-tree | Disjoint set Data structure , Union By Size ! ! ! ! ! ! ! ! | disjoint-set-data-structure-union-by-siz-47mz | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | Vedant_Rathore | NORMAL | 2023-09-24T10:10:00.276529+00:00 | 2023-09-24T10:10:00.276550+00:00 | 11 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | 0 | ['C++'] | 0 |
count-valid-paths-in-a-tree | Extremely long C++ solution | DP on Trees | extremely-long-c-solution-dp-on-trees-by-r26s | Intuition\n Describe your first thoughts on how to solve this problem. \nWhen answer for current node is dependent on some answer from its child nodes and paren | biswajit_kaushik | NORMAL | 2023-09-24T09:45:06.182603+00:00 | 2023-09-24T09:45:06.182623+00:00 | 20 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWhen answer for current node is dependent on some answer from its child nodes and parent node, then DP on trees can be used to solve such type of problems.\n\nNote that my solution calculates the path for each node $$a$$ to node $$b$$ in ... | 0 | 0 | ['C++'] | 0 |
count-valid-paths-in-a-tree | C++ || Dp on Trees in Dp | c-dp-on-trees-in-dp-by-12345556-jdrk | Intuition\n Describe your first thoughts on how to solve this problem. \nmaintain 2 vales for every node in1 is for non prime paths starting in2 for prime paths | 12345556 | NORMAL | 2023-09-24T09:42:58.702541+00:00 | 2023-09-24T09:42:58.702561+00:00 | 34 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nmaintain 2 vales for every node in1 is for non prime paths starting in2 for prime paths starting from that node.\n\nso for if the node is prime value we need to take the non-prime paths of the childrean and non prime paths is zero.\n\nif ... | 0 | 0 | ['C++'] | 0 |
count-valid-paths-in-a-tree | SIEVE + DSU Approach EASY O(n), fast and memory efficient beats 100% | sieve-dsu-approach-easy-on-fast-and-memo-notl | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n 1. SIEve for finding all the primes till n, The complexity for this func | rahul19191 | NORMAL | 2023-09-24T08:29:43.564463+00:00 | 2023-09-24T08:29:43.564480+00:00 | 29 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n 1. SIEve for finding all the primes till $$n$$, The complexity for this function is $$O(sqrt(n))$$. \n 2. Now implemented DSU class with unionbySize method, The complexity of each function inside the class is $$O(4*\u03B1)$... | 0 | 0 | ['Union Find', 'Graph', 'C++'] | 0 |
count-valid-paths-in-a-tree | Used Disjoint Set Union || Beats 87.5% in runtime and 100% memory usage | used-disjoint-set-union-beats-875-in-run-f7e1 | \n# Approach\nJoin edges containing only composite number keeping track of sizes of each connected component.\nEdges containg two prime number are of no use.\nI | kb_00 | NORMAL | 2023-09-24T07:36:59.547281+00:00 | 2023-09-24T07:36:59.547300+00:00 | 18 | false | \n# Approach\nJoin edges containing only composite number keeping track of sizes of each connected component.\nEdges containg two prime number are of no use.\nInsert the edges with only one prime and update the ans.\n\n**Tag me for more clarification.**\n# Complexity\n- Time complexity:\n$$O(nlogn)$$\n\n- Space complex... | 0 | 0 | ['Union Find', 'C++'] | 0 |
valid-boomerang | [Java/C++/Python] Straight Forward | javacpython-straight-forward-by-lee215-3h33 | Assuming three points are A, B, C.\n\nThe first idea is that, calculate the area of ABC.\nWe can reuse the conclusion and prove in 812. Largest Triangle Area\n\ | lee215 | NORMAL | 2019-05-05T04:09:20.857739+00:00 | 2019-05-05T04:28:05.121602+00:00 | 12,222 | false | Assuming three points are A, B, C.\n\nThe first idea is that, calculate the area of ABC.\nWe can reuse the conclusion and prove in [812. Largest Triangle Area](https://leetcode.com/problems/largest-triangle-area/discuss/122711/C++JavaPython-Solution-with-Explanation-and-Prove)\n\nThe other idea is to calculate the slop... | 123 | 4 | [] | 9 |
valid-boomerang | C++ 1-liner: Triangle Area | c-1-liner-triangle-area-by-votrubac-qy0v | Intuition\nIn other words, we need to return true if the triangle area is not zero.\n\nFor the detailed explanation, see the comment by EOAndersson below.\n# So | votrubac | NORMAL | 2019-05-05T04:01:16.062852+00:00 | 2019-05-06T07:01:49.218353+00:00 | 5,353 | false | # Intuition\nIn other words, we need to return true if the triangle area is not zero.\n\nFor the detailed explanation, see the comment by [EOAndersson](https://leetcode.com/eoandersson/) below.\n# Solution\nCalculate the area of the triangle: ```x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)``` and compare it to zero... | 71 | 2 | [] | 10 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.