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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
delete-duplicate-folders-in-system | [Golang] Trie + DFS + Simple Hashing | golang-trie-dfs-simple-hashing-by-user04-mt6y | go\ntype TrieNode struct {\n hash string\n children map[string]*TrieNode\n}\n\nfunc deleteDuplicateFolder(paths [][]string) [][]string {\n root := &TrieNode{ | user0440H | NORMAL | 2023-02-12T03:56:55.936925+00:00 | 2023-02-12T03:56:55.936958+00:00 | 45 | false | ```go\ntype TrieNode struct {\n hash string\n children map[string]*TrieNode\n}\n\nfunc deleteDuplicateFolder(paths [][]string) [][]string {\n root := &TrieNode{children: make(map[string]*TrieNode)}\n for _, path := range paths {\n insert(root, path)\n }\n // Now we\'re going to do a DFS to compute hash for eac... | 0 | 0 | ['Depth-First Search', 'Trie', 'Go'] | 0 |
delete-duplicate-folders-in-system | Fast and readable C++ solution using tries | fast-and-readable-c-solution-using-tries-m8bn | Intuition\nConstruct a trie and compute a serial identifier for the nodes.\n\n# Approach\n1. Construct a trie from the input paths.\n2. Compute an identifier fo | daniel404 | NORMAL | 2023-02-02T00:43:12.888930+00:00 | 2023-02-02T00:43:12.888969+00:00 | 103 | false | # Intuition\nConstruct a trie and compute a serial identifier for the nodes.\n\n# Approach\n1. Construct a trie from the input paths.\n2. Compute an identifier for each node in the trie, and locate duplicates.\n3. Output nodes with unique identifiers.\n\nMost importantly, the identifier we compute in step 2 must have a... | 0 | 0 | ['C++'] | 0 |
delete-duplicate-folders-in-system | hash map by converting internal folder structures to strings | hash-map-by-converting-internal-folder-s-5o88 | Approach\n- build nested dictionary structure\n- create sets/maps of structures found\n- filter for unique structures\n\n# Complexity\nN = number of folders\n\n | kesslwovv | NORMAL | 2022-12-09T19:58:39.144373+00:00 | 2022-12-09T19:58:39.144419+00:00 | 130 | false | # Approach\n- build nested dictionary structure\n- create sets/maps of structures found\n- filter for unique structures\n\n# Complexity\nN = number of folders\n\n- Time complexity:\nbuild nested dictionary structure: O(N)\ncreate sets/maps of structures found: O(N * N)\nfilter for unique structures: O(N)\n__Overall: O(... | 0 | 0 | ['Python'] | 0 |
delete-duplicate-folders-in-system | [Java] Simple Trie + DFS Solution | java-simple-trie-dfs-solution-by-samuel3-p4h0 | Code\n\nclass Solution {\n class TrieNode {\n TreeMap<String, TrieNode> children;\n boolean isEndOfDir;\n boolean deleted;\n \n | Samuel3Shin | NORMAL | 2022-11-12T19:30:17.961848+00:00 | 2022-11-12T19:30:17.961896+00:00 | 265 | false | # Code\n```\nclass Solution {\n class TrieNode {\n TreeMap<String, TrieNode> children;\n boolean isEndOfDir;\n boolean deleted;\n \n public TrieNode() {\n children = new TreeMap<>();\n isEndOfDir = false;\n deleted = false;\n }\n }\n\n ... | 0 | 0 | ['Java'] | 0 |
delete-duplicate-folders-in-system | solutions in C++ | solutions-in-c-by-infox_92-5yqp | \nclass Solution {\n struct Node {\n map<string,Node*> child;\n bool duplicate=false;\n };\n \n void build(Node* root,vector<vecto | Infox_92 | NORMAL | 2022-10-31T10:44:10.621080+00:00 | 2022-10-31T10:44:10.621120+00:00 | 46 | false | ```\nclass Solution {\n struct Node {\n map<string,Node*> child;\n bool duplicate=false;\n };\n \n void build(Node* root,vector<vector<string>>& paths) {\n for (auto& dirs : paths) {\n Node* node=root;\n for (auto& dir : dirs) {\n if (node->child... | 0 | 0 | [] | 0 |
delete-duplicate-folders-in-system | Python3 with tree hash | python3-with-tree-hash-by-jeremyxu555-v388 | \nimport collections\nfrom typing import List\nfrom sortedcontainers import SortedDict\n\n\nclass TreeNode:\n def __init__(self, val):\n self.val = va | jeremyxu555 | NORMAL | 2022-10-31T00:38:25.644846+00:00 | 2022-10-31T00:38:25.644879+00:00 | 158 | false | ```\nimport collections\nfrom typing import List\nfrom sortedcontainers import SortedDict\n\n\nclass TreeNode:\n def __init__(self, val):\n self.val = val\n self.children = SortedDict()\n\n\nclass Solution:\n def __init__(self):\n self.key2Id = {}\n self.key2Count = collections.default... | 0 | 0 | ['Python3'] | 0 |
delete-duplicate-folders-in-system | C++ | Trie accepted solution | c-trie-accepted-solution-by-nick-thurn-0p53 | One thing that had me stuck was understanding that the whole subtree of a node needs to be compared. This is actually easier than trying to separate the individ | nick-thurn | NORMAL | 2022-10-03T05:07:38.468033+00:00 | 2022-10-03T05:32:21.901648+00:00 | 66 | false | One thing that had me stuck was understanding that the whole subtree of a node needs to be compared. This is actually easier than trying to separate the individual paths. \n\nThere is also no reason to do the pruning in two steps as all the data is available when you add a second/third/forth parent node to the same sub... | 0 | 0 | ['Depth-First Search', 'Trie', 'C'] | 0 |
delete-duplicate-folders-in-system | Java Solution | Using Trie With PostOrder And InOrder DFS Traversal | java-solution-using-trie-with-postorder-0i18j | Steps :\n\n1. Build a Trie. Here I am using map for the children node because node value is a string, not a single character.\n\n2. PostOrder - For duplication | nitwmanish | NORMAL | 2022-10-01T14:01:35.752117+00:00 | 2022-11-05T02:33:41.606851+00:00 | 187 | false | Steps :\n\n1. Build a Trie. Here I am using map for the children node because node value is a string, not a single character.\n\n2. PostOrder - For duplication detection in tree.\n* The substructure is encoded using \'(\' and \')\' to denote levels e.g. a (b, c,d (...),) \n* Order of node\'s children is important. F... | 0 | 0 | ['Tree', 'Depth-First Search', 'Trie', 'Java'] | 0 |
delete-duplicate-folders-in-system | Python solution | python-solution-by-chienwen-okr9 | My approach: generate a "signature" for each node as:\na(...)c(...)d(...), where the children a, c, d should be sorted.\n... is the signature of grandchildren.\ | chienwen | NORMAL | 2022-09-22T17:55:50.769376+00:00 | 2022-09-22T17:55:50.769421+00:00 | 101 | false | My approach: generate a "signature" for each node as:\n`a(...)c(...)d(...)`, where the children a, c, d should be sorted.\n`...` is the signature of grandchildren.\n\n```python\nclass Solution:\n def deleteDuplicateFolder(self, paths: List[List[str]]) -> List[List[str]]:\n \n # build the trie\n ... | 0 | 0 | ['Python'] | 0 |
delete-duplicate-folders-in-system | Python Implementation using concept of Trie | python-implementation-using-concept-of-t-t0go | \nclass TrieNode:\n def __init__(self):\n self.children = defaultdict(TrieNode)\n self.hash, self.isDeleted = \'\', False\n \n def ad | hemantdhamija | NORMAL | 2022-09-14T06:54:27.873459+00:00 | 2022-09-14T06:54:27.873500+00:00 | 144 | false | ```\nclass TrieNode:\n def __init__(self):\n self.children = defaultdict(TrieNode)\n self.hash, self.isDeleted = \'\', False\n \n def add(self, path, idx = 0):\n if idx != len(path):\n self.children[path[idx]].add(path, idx + 1)\n \n def calculateHash(self, hashes, nod... | 0 | 0 | ['Tree', 'Depth-First Search', 'Trie', 'Recursion', 'Python'] | 0 |
delete-duplicate-folders-in-system | [C++] O(N) Trie + Merkle Tree w/ Custom Hashing (GCC std::hash has collisions) | c-on-trie-merkle-tree-w-custom-hashing-g-vn9m | Basically, this is my thought process:\n- Trie: I need to "compress" all the paths with their parent paths into one tree, so [a], [a,b], [a,b,c] turns into [a,b | CelonyMire | NORMAL | 2022-09-03T18:27:59.580867+00:00 | 2022-09-03T18:30:57.112201+00:00 | 76 | false | Basically, this is my thought process:\n- Trie: I need to "compress" all the paths with their parent paths into one tree, so `[a], [a,b], [a,b,c]` turns into `[a,b,c]`. It is also good for getting the remaining paths after removal of duplicate folders.\n- Merkle Tree: Efficient computation of hashes for each subtree. N... | 0 | 0 | ['Depth-First Search', 'Trie', 'C', 'Hash Function'] | 0 |
delete-duplicate-folders-in-system | ✔️ [Javascript] solution || Very simple | javascript-solution-very-simple-by-kagoo-11bi | This is not my answer. I found it in this Github repository https://github.com/AnasImloul/Leetcode-solutions\nIt is very helpful, check it out.\n\nclass Node{\n | Kagoot | NORMAL | 2022-08-29T22:28:50.240961+00:00 | 2022-08-29T22:28:50.241001+00:00 | 92 | false | This is not my answer. I found it in this Github repository https://github.com/AnasImloul/Leetcode-solutions\nIt is very helpful, check it out.\n```\nclass Node{\n constructor(val,parent){\n this.val=val,this.parent=parent,this.children={},this.hash\n }\n}\nclass Trie{\n constructor(){\n this.roo... | 0 | 0 | ['JavaScript'] | 1 |
delete-duplicate-folders-in-system | Java | Concise | Briefly Explained + Comments | java-concise-briefly-explained-comments-dkvjd | I tried to hash the tree with long but there were hash collision, so I changed it to String.\n\nBasically 3 steps:\n1. Create the tree. It is similar to how one | Student2091 | NORMAL | 2022-08-08T05:25:14.227403+00:00 | 2022-08-08T05:25:43.332073+00:00 | 165 | false | I tried to hash the tree with long but there were hash collision, so I changed it to String.\n\nBasically 3 steps:\n1. Create the tree. It is similar to how one would build a Trie\n2. Hash The Tree with **Post-Order** DFS traversal & mark duplicate subtrees. This is linear time. \n3. DFS again to add all paths that are... | 0 | 0 | ['Java'] | 0 |
delete-duplicate-folders-in-system | Python Hashmap | python-hashmap-by-dlim-0qzm | Let N, M, and K be the number of folders, sum(len(paths[i])), and the maximum length of folder name respectively.\n1. Make a tree structure from \'paths\'\n | dlim_ | NORMAL | 2022-08-07T22:12:15.231749+00:00 | 2022-09-03T19:59:16.672929+00:00 | 70 | false | Let N, M, and K be the number of folders, sum(len(paths[i])), and the maximum length of folder name respectively.\n1. Make a tree structure from \'paths\'\n time complexity: O(M)\n\tspace compexity O(NK)\n```\nclass Node:\n def __init__(self, val, parent=None, children=None):\n self.val = val\n self... | 0 | 0 | [] | 0 |
delete-duplicate-folders-in-system | Java | java-by-alsamany-7a0j | class Solution {\n static class Node {\n String name;\n Map childern = new HashMap<>();\n\n private String hashCode = null;\n\n p | alsamany | NORMAL | 2022-08-03T14:27:50.924891+00:00 | 2022-08-03T14:27:50.924923+00:00 | 41 | false | class Solution {\n static class Node {\n String name;\n Map<String, Node> childern = new HashMap<>();\n\n private String hashCode = null;\n\n public Node(String _name) {\n name = _name;\n }\n\n public void add(List<String> path) {\n Node cur = this;\n ... | 0 | 0 | [] | 0 |
delete-duplicate-folders-in-system | Java - Somewhat brute force but beats 80% | java-somewhat-brute-force-but-beats-80-b-54td | The algorithm as 4 parts\n1. Convert the list to a tree\n2. Traverse the tree in post-order manner and generate a unique hash of all the subnodes in the tree. T | cutkey | NORMAL | 2022-07-31T02:14:31.124268+00:00 | 2022-07-31T08:46:37.221286+00:00 | 47 | false | The algorithm as 4 parts\n1. Convert the list to a tree\n2. Traverse the tree in post-order manner and generate a unique hash of all the subnodes in the tree. This is a very important step. The hash is a concatenation of all subnodes under a node. However we need to consider the following\n\t1. Make sure that the child... | 0 | 0 | [] | 0 |
delete-duplicate-folders-in-system | Golang solution based on trimming | golang-solution-based-on-trimming-by-sam-v8yw | Created a Trie and followed the deletion procedure. \nSince Golang does not have sorted map, it was necessary to sort the keys of children before deduplication\ | sameerpjoshi | NORMAL | 2022-07-24T11:36:22.441424+00:00 | 2022-07-24T11:36:22.441454+00:00 | 56 | false | Created a Trie and followed the deletion procedure. \nSince Golang does not have sorted map, it was necessary to sort the keys of children before deduplication\n``` \nfunc deleteDuplicateFolder(paths [][]string) [][]string {\n \n t := NewTrie()\n for _,path := range paths {\n t.insert(path)\n }\n ... | 0 | 0 | ['Go'] | 0 |
delete-duplicate-folders-in-system | C++ Clean & Fast Code with Explanation | Faster than 90% | c-clean-fast-code-with-explanation-faste-7jjz | Firstly, construct a tree which denotes the file system. The value of every tree node is the corresponding folder name.\n\nThen, we compress every node\'s subtr | vampireweekend | NORMAL | 2022-07-12T07:58:17.421783+00:00 | 2022-07-12T07:58:17.421822+00:00 | 77 | false | Firstly, construct a tree which denotes the file system. The value of every tree node is the corresponding folder name.\n\nThen, we compress every node\'s subtree architecture into a string. Note that in order to judge two identical tree nodes, the children of every node should be visited according to the lexigraphical... | 0 | 0 | [] | 0 |
delete-duplicate-folders-in-system | Weird Expected output | weird-expected-output-by-jainish-2ydy | Input\n[["a"],["a","x"],["a","x","y"],["a","z"],["b"],["b","x"],["b","x","y"],["b","z"],["b","w"]]\nOutput\n[["b"],["b","w"]]\nExpected\n[["a"],["b"],["a","z"], | jainish | NORMAL | 2022-07-08T14:00:47.573752+00:00 | 2022-07-08T14:00:47.573792+00:00 | 21 | false | Input\n[["a"],["a","x"],["a","x","y"],["a","z"],["b"],["b","x"],["b","x","y"],["b","z"],["b","w"]]\nOutput\n[["b"],["b","w"]]\nExpected\n[["a"],["b"],["a","z"],["b","z"],["b","w"]]\n\nSeems according to me that expected sol is wrong .\n\nhere is the sub directory structure \n *\n\t/ \\ \na b\n| \\ ... | 0 | 0 | [] | 0 |
delete-duplicate-folders-in-system | Python Clean Solution | python-clean-solution-by-user6397p-oe6e | \nclass Trie:\n def __init__(self):\n self.children = defaultdict(Trie)\n self.delete = False\n\nclass Solution:\n def deleteDuplicateFolder | user6397p | NORMAL | 2022-07-06T09:01:45.193272+00:00 | 2022-07-06T09:01:45.193300+00:00 | 175 | false | ```\nclass Trie:\n def __init__(self):\n self.children = defaultdict(Trie)\n self.delete = False\n\nclass Solution:\n def deleteDuplicateFolder(self, paths: List[List[str]]) -> List[List[str]]:\n root = Trie()\n patterns = defaultdict(list)\n ans = []\n\n self.populate_tr... | 0 | 0 | ['Python3'] | 0 |
delete-duplicate-folders-in-system | C++ || Trie || DFS || Map || (TLE/MLE) | c-trie-dfs-map-tlemle-by-igloo11-5vnf | \nclass Solution {\nprivate:\n map<string, int> cnt;\n \n struct Node {\n string data;\n string directory;\n map<string, Node*> ne | Igloo11 | NORMAL | 2022-05-06T18:17:26.633131+00:00 | 2022-05-06T18:18:27.082333+00:00 | 149 | false | ```\nclass Solution {\nprivate:\n map<string, int> cnt;\n \n struct Node {\n string data;\n string directory;\n map<string, Node*> next;\n Node(string value) {\n data = value;\n directory = "";\n }\n };\n \n void make_directory(Node* root, vecto... | 0 | 0 | ['Depth-First Search', 'Trie', 'C'] | 0 |
delete-duplicate-folders-in-system | [C++] Trie + DFS hashing | c-trie-dfs-hashing-by-kaminyou-frkx | \nclass TrieNode {\npublic:\n map<string, TrieNode*> children;\n bool isEnd;\n bool isExcluded;\n string name;\n TrieNode(string folder) {\n | kaminyou | NORMAL | 2022-04-28T15:22:20.354868+00:00 | 2022-04-28T15:22:20.354897+00:00 | 178 | false | ```\nclass TrieNode {\npublic:\n map<string, TrieNode*> children;\n bool isEnd;\n bool isExcluded;\n string name;\n TrieNode(string folder) {\n name = folder;\n isEnd = false;\n isExcluded = false;\n }\n};\nclass Trie {\npublic:\n TrieNode* root;\n Trie() {\n root = n... | 0 | 0 | ['Depth-First Search', 'Trie', 'C'] | 0 |
delete-duplicate-folders-in-system | Swift Solution | swift-solution-by-maksimilliano-v6z6 | \nclass Solution {\n private var subdirsTotal: [String: Bool] = [:]\n \n func deleteDuplicateFolder(_ paths: [[String]]) -> [[String]] {\n var root = Node | maksimilliano | NORMAL | 2022-04-08T16:55:35.661148+00:00 | 2022-04-08T16:55:35.661192+00:00 | 50 | false | ```\nclass Solution {\n private var subdirsTotal: [String: Bool] = [:]\n \n func deleteDuplicateFolder(_ paths: [[String]]) -> [[String]] {\n var root = Node(value: "/")\n for path in paths {\n let oldRoot = root\n createTree(path, &root)\n root = oldRoot\n }\n \n marker(root)\n \n ... | 0 | 0 | [] | 0 |
delete-duplicate-folders-in-system | Short but slow js solution | short-but-slow-js-solution-by-837951602-1yii | Runtime: 1446 ms\nMemory Usage: 96.1 MB\n\nvar deleteDuplicateFolder = function(paths) {\n var root = {}, socks = {};\n paths.sort().forEach(x=>x.reduce(( | 837951602 | NORMAL | 2022-02-27T17:52:19.993208+00:00 | 2022-02-27T17:52:19.993243+00:00 | 101 | false | Runtime: 1446 ms\nMemory Usage: 96.1 MB\n```\nvar deleteDuplicateFolder = function(paths) {\n var root = {}, socks = {};\n paths.sort().forEach(x=>x.reduce((dir,v)=>dir[v]||(dir[v]={}),root));\n function f(folder) {\n var n = JSON.stringify(folder);\n socks[n] = -~socks[n];\n for (var i in... | 0 | 0 | [] | 0 |
delete-duplicate-folders-in-system | Clean Python Solution (Self-Explained, Easy to Understand) | clean-python-solution-self-explained-eas-i0ot | python\nclass Node(object):\n def __init__(self, val):\n self.val = val\n self.key = None\n self.children = {}\n\nclass Solution(object) | christopherwu0529 | NORMAL | 2022-01-21T03:00:44.649034+00:00 | 2022-01-21T03:37:58.939815+00:00 | 261 | false | ```python\nclass Node(object):\n def __init__(self, val):\n self.val = val\n self.key = None\n self.children = {}\n\nclass Solution(object):\n def deleteDuplicateFolder(self, paths):\n def setKey(node):\n node.key = \'\'\n for c in sorted(node.children.keys()): #n... | 0 | 0 | [] | 0 |
delete-duplicate-folders-in-system | Ugly unoptimised Ruby solution, still beats 100%/100% | ugly-unoptimised-ruby-solution-still-bea-22ww | \n# @param {String[][]} paths\n# @return {String[][]}\ndef delete_duplicate_folder(paths)\n root = {}\n paths.sort.each do |path|\n cur = root\n path.ea | dnnx | NORMAL | 2021-12-11T22:07:31.104038+00:00 | 2021-12-11T22:07:31.104061+00:00 | 87 | false | ```\n# @param {String[][]} paths\n# @return {String[][]}\ndef delete_duplicate_folder(paths)\n root = {}\n paths.sort.each do |path|\n cur = root\n path.each do |name|\n cur = (cur[name] ||= {})\n end\n end\n \n @mem = Hash.new { |h, k| h[k] = [] }\n f([], root) \n @mem.keep_if { |k, v| v.size... | 0 | 0 | [] | 0 |
delete-duplicate-folders-in-system | [python] dfs with trie | python-dfs-with-trie-by-vl4deee11-18ni | \nclass Solution:\n def deleteDuplicateFolder(self, paths: List[List[str]]) -> List[List[str]]:\n tr={"1":"*"}\n for p in paths:\n c | vl4deee11 | NORMAL | 2021-09-27T06:15:59.010629+00:00 | 2021-09-27T06:15:59.010658+00:00 | 244 | false | ```\nclass Solution:\n def deleteDuplicateFolder(self, paths: List[List[str]]) -> List[List[str]]:\n tr={"1":"*"}\n for p in paths:\n ctr=tr\n for f in p:\n if f not in ctr:\n ctr[f]={"1":f} \n ctr=ctr[f]\n hm=defaultdict(li... | 0 | 0 | [] | 0 |
delete-duplicate-folders-in-system | [Simple & Efficient][C++] Trie + Hashing | simple-efficientc-trie-hashing-by-outvoi-w85b | \nclass Solution {\n\tusing ull = unsigned long long;\n\n\tstruct hash_t {\n\t\tstatic constexpr ull base = 31, begin = 27, end = 28;\n\t\tull value, coeff;\n\t | outvoider | NORMAL | 2021-08-15T23:45:14.362042+00:00 | 2021-08-20T07:14:26.539541+00:00 | 309 | false | ```\nclass Solution {\n\tusing ull = unsigned long long;\n\n\tstruct hash_t {\n\t\tstatic constexpr ull base = 31, begin = 27, end = 28;\n\t\tull value, coeff;\n\t\thash_t() : value(0), coeff(1) {}\n\t\thash_t(ull value, ull coeff) : value(value), coeff(coeff) {}\n\t\thash_t(string_view s) : hash_t() {\n\t\t\tfor (char... | 0 | 0 | [] | 0 |
delete-duplicate-folders-in-system | C++ Solution O(nlogn) solution by assigning ids to serializations | c-solution-onlogn-solution-by-assigning-q39fe | \nclass Solution {\n struct Node {\n string val;\n bool skip = false;\n unordered_map<string, Node*> next;\n Node() = default;\n | yeetcode_t | NORMAL | 2021-08-05T11:51:05.481303+00:00 | 2021-08-05T11:51:05.481337+00:00 | 262 | false | ```\nclass Solution {\n struct Node {\n string val;\n bool skip = false;\n unordered_map<string, Node*> next;\n Node() = default;\n Node(string v) : val{move(v)} {};\n }; \n unordered_map<string, pair<int, Node*>> tree_map;\n unordered_map<string, Node*> content_map;\n ... | 0 | 0 | [] | 0 |
maximum-number-of-k-divisible-components | Why greedy dfs will work? Easiest Intution explanation! | why-greedy-dfs-will-work-easiest-intutio-g26f | Intuition\n Describe your first thoughts on how to solve this problem. \n#### Let\'s understand the problem first:\n- Nodes represent undirected (tree)\n- To re | IndominouS | NORMAL | 2023-10-02T13:59:38.167351+00:00 | 2024-08-31T14:20:18.581333+00:00 | 1,412 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n#### Let\'s understand the problem first:\n- Nodes represent undirected (tree)\n- To return max no of components divisible by k\n\n###### Now how can we form a component whose sum is divisible by k?\n> Isn\'t this similar to finding a sub... | 28 | 0 | ['Math', 'Dynamic Programming', 'Greedy', 'C++', 'Java', 'Python3'] | 5 |
maximum-number-of-k-divisible-components | ✅[Python] cut leaf or merge with parent (explained) | python-cut-leaf-or-merge-with-parent-exp-zv41 | If a leaf\'s value is divisible by k, we can safely separate it from the tree, thus, increasing the number of components. If not, it will be a part of its paren | stanislav-iablokov | NORMAL | 2023-09-30T16:01:30.498995+00:00 | 2023-09-30T16:15:30.612988+00:00 | 1,766 | false | If a leaf\'s value is divisible by `k`, we can safely separate it from the tree, thus, increasing the number of components. If not, it will be a part of its parent\'s component. To account for the latter, it is sufficient to just increase the parent\'s value by the leaf\'s value. \n\nThe algorithm proceeds by cutting l... | 28 | 0 | ['Python3'] | 6 |
maximum-number-of-k-divisible-components | DFS/topo sort finds components||79 ms Beats 98.73% | dfs-finds-components-by-anwendeng-x39e | Intuition1st approach is DFS which is quite standard.Later try other approach. BFS is doable, but it's a modification of Kahn's algorithm (topo sort) applied to | anwendeng | NORMAL | 2024-12-21T00:56:48.225173+00:00 | 2024-12-21T10:05:21.879236+00:00 | 7,632 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
1st approach is DFS which is quite standard.
Later try other approach. BFS is doable, but it's a modification of Kahn's algorithm (topo sort) applied to undirected tree.
# DFS Approach
<!-- Describe your approach to solving the problem. --... | 26 | 1 | ['Depth-First Search', 'Breadth-First Search', 'Topological Sort', 'C++'] | 6 |
maximum-number-of-k-divisible-components | BFS | bfs-by-votrubac-hz1u | As usual, we frist generate the adjacency list from the edges.\n\nThen, we count edges for each node, and we do BFS starting from leaves.\n\nAs we process the n | votrubac | NORMAL | 2023-09-30T16:01:51.297454+00:00 | 2023-09-30T16:12:31.224789+00:00 | 1,951 | false | As usual, we frist generate the adjacency list from the edges.\n\nThen, we count edges for each node, and we do BFS starting from leaves.\n\nAs we process the node `i`, we check if the value is divisible by `k`.\n\nIf so, we can make a cut here, and we increment the result.\n\nThen, for the parent node `j`:\n- we add t... | 26 | 0 | [] | 10 |
maximum-number-of-k-divisible-components | DFS| Prefix Sum| Easy to understand | dfs-prefix-sum-easy-to-understand-by-md_-m0b8 | Intuition\ngo till bottom and then compe up calculating sum of value of nodes\n\n\n\n# Complexity\n- Time complexity:O(n)\n Add your time complexity here, e.g. | Md_Rafiq | NORMAL | 2023-09-30T16:12:14.877874+00:00 | 2023-09-30T16:12:14.877894+00:00 | 3,649 | false | # Intuition\ngo till bottom and then compe up calculating sum of value of nodes\n\n\n\n# Complexity\n- Time complexity:O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n \n // dp ... | 25 | 2 | ['Depth-First Search', 'Graph', 'C++'] | 5 |
maximum-number-of-k-divisible-components | Topological Sort Approach ✅| C++ | Java | Python | JavaScript | topological-sort-approach-c-java-python-s98ti | ⬆️Upvote if it helps ⬆️Connect with me on Linkedin [Bijoy Sing]Solution in C++, Python, Java, and JavaScriptIntuitionThe problem can be approached by considerin | BijoySingh7 | NORMAL | 2024-12-21T02:32:30.864398+00:00 | 2024-12-21T02:32:30.864398+00:00 | 8,577 | false | # ⬆️Upvote if it helps ⬆️
---
## Connect with me on Linkedin [Bijoy Sing]
---
###### *Solution in C++, Python, Java, and JavaScript*
```cpp []
class Solution {
public:
int maxKDivisibleComponents(int n, vector<vector<int>>& edges, vector<int>& vals, int k) {
vector<vector<int>> graph(n);
vecto... | 22 | 1 | ['Topological Sort', 'Python', 'C++', 'Java', 'Python3', 'JavaScript'] | 9 |
maximum-number-of-k-divisible-components | Simple dfs ! | simple-dfs-by-yadivyanshu-sctr | \n\n# Code\n\nclass Solution {\npublic:\n int count = 0, k;\n \n int dfs(int i, vector<bool>& vis, vector<int> adj[], vector<int>& values) {\n v | yadivyanshu | NORMAL | 2023-09-30T17:22:54.272200+00:00 | 2023-09-30T17:22:54.272218+00:00 | 692 | false | \n\n# Code\n```\nclass Solution {\npublic:\n int count = 0, k;\n \n int dfs(int i, vector<bool>& vis, vector<int> adj[], vector<int>& values) {\n vis[i] = true;\n int sum = 0;\n for(auto &j : adj[i]) {\n if(vis[j] == true)continue;\n sum += dfs(j, vis, adj, values);\n... | 14 | 1 | ['Depth-First Search', 'C++'] | 4 |
maximum-number-of-k-divisible-components | PostOrder Traversal || C++ || Easy to understand | postorder-traversal-c-easy-to-understand-d4en | 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 | baibhavkr143 | NORMAL | 2023-09-30T16:30:47.618087+00:00 | 2023-09-30T16:31:51.318268+00:00 | 940 | 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:\n<!-- Add your space complexity here, e.g. $$O(n)$... | 12 | 0 | ['C++'] | 3 |
maximum-number-of-k-divisible-components | 2872. Maximum Number of K-Divisible Com..., Time complexity: O(N*M), Space complexity: O(N*M) | 2872-maximum-number-of-k-divisible-com-t-ansb | IntuitionApproachComplexity
Time complexity: O(N*M)
Space complexity: O(N*M)
Code | richardmantikwang | NORMAL | 2024-12-21T02:07:10.644277+00:00 | 2024-12-21T02:10:48.097925+00:00 | 153 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity: O(N*M)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: O(N*M)
<!-- Add your space complexity here, e.g. $$O(n)$$... | 11 | 0 | ['Python3'] | 0 |
maximum-number-of-k-divisible-components | Dfs Sum Finding 98% Beats | dfs-sum-finding-98-beats-by-sumeet_sharm-fr5f | 🧠 IntuitionThe problem is based on breaking a tree into components such that the sum of node values in each component is divisible by ( k ). The main observatio | Sumeet_Sharma-1 | NORMAL | 2024-12-21T01:28:50.199812+00:00 | 2024-12-21T01:28:50.199812+00:00 | 1,891 | false | # 🧠 Intuition
The problem is based on breaking a tree into components such that the sum of node values in each component is divisible by \( k \). The main observation is that during a tree traversal, if the cumulative sum of a subtree is divisible by \( k \), we can "cut" the subtree into a separate component. The res... | 10 | 0 | ['C++'] | 0 |
maximum-number-of-k-divisible-components | Python | Depth-First Search (DFS) | python-depth-first-search-dfs-by-khosiya-6zz2 | see the Successfully Accepted SubmissionCodeAlgorithm Overview
Represent the tree using an adjacency list.
Use DFS to traverse the tree and calculate the sum of | Khosiyat | NORMAL | 2024-12-21T00:04:21.879645+00:00 | 2024-12-21T00:04:21.879645+00:00 | 1,797 | false | [see the Successfully Accepted Submission](https://leetcode.com/problems/maximum-number-of-k-divisible-components/submissions/1484125649/?envType=daily-question&envId=2024-12-21)
# Code
```python3 []
class Solution:
def maxKDivisibleComponents(self, n: int, edges: List[List[int]], values: List[int], k: int) -> int... | 10 | 0 | ['Python3'] | 1 |
maximum-number-of-k-divisible-components | Easy to understand DFS✅✅ || O(n) Solution | easy-to-understand-dfs-on-solution-by-ar-kcbl | Approach
First, calculate the subtree size(value of subtree) for every node in the tree using the getSubtree function. This function recursively computes the su | arunk_leetcode | NORMAL | 2024-12-21T05:26:10.737874+00:00 | 2024-12-21T05:29:48.529512+00:00 | 3,047 | false | # Approach
<!-- Describe your approach to solving the problem. -->
1. First, calculate the `subtree size(value of subtree)` for every node in the tree using the getSubtree function. This function recursively computes the sum of the values in the subtree rooted at each node.
2. The while doing `dfs` just check if that p... | 8 | 0 | ['Tree', 'Depth-First Search', 'Python', 'C++', 'Java', 'Python3'] | 3 |
maximum-number-of-k-divisible-components | Easy Video Solution(C++,JAVA,Python)🔥 || DFS + Indegree 🔥 | easy-video-solutioncjavapython-dfs-indeg-8k5o | Intuition\n Describe your first thoughts on how to solve this problem. \nStart thinking from leaf Nodes\n\n# Detailed and easy Video Solution\n\nhttps://youtu.b | ayushnemmaniwar12 | NORMAL | 2023-09-30T19:06:42.201061+00:00 | 2023-09-30T19:06:42.201088+00:00 | 674 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nStart thinking from leaf Nodes\n\n# ***Detailed and easy Video Solution***\n\nhttps://youtu.be/JvgvAhKPBcI\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\nThe problem is to determine the maximum number of connect... | 8 | 0 | ['Breadth-First Search', 'Graph', 'C++', 'Java', 'Python3'] | 0 |
maximum-number-of-k-divisible-components | Simple ✅ || Easy to Understand ✅ || beat 100% ✅ | simple-easy-to-understand-beat-100-by-th-0xgc | Intuition:\nWe are given a tree with nodes having integer values. We need to find the number of subtrees in the tree such that the sum of values in each subtree | thesaurabhmhaske | NORMAL | 2023-09-30T18:35:25.420159+00:00 | 2023-09-30T18:35:25.420179+00:00 | 320 | false | # Intuition:\nWe are given a tree with nodes having integer values. We need to find the number of subtrees in the tree such that the sum of values in each subtree is divisible by \'k\'. We can solve this problem using Depth-First Search (DFS) to traverse the tree.\n\n# Approach:\n\n- Initialize a count \'cnt\' to keep ... | 8 | 0 | ['Tree', 'Depth-First Search', 'Graph', 'C++'] | 2 |
maximum-number-of-k-divisible-components | DFS for Tree Traversal😎|| Written in JavaScript | dfs-for-tree-traversal-written-in-javasc-f90b | Complexity
Time complexity: O(n)
Space complexity: O(n)
Code | yenhuynh02 | NORMAL | 2024-12-21T05:17:36.325004+00:00 | 2024-12-21T05:17:36.325004+00:00 | 420 | false | # Complexity
- Time complexity: O(n)
- Space complexity: O(n)
# Code
```javascript []
/**
* @param {number} n
* @param {number[][]} edges
* @param {number[]} values
* @param {number} k
* @return {number}
*/
var maxKDivisibleComponents = function(n, edges, values, k) {
// Step 1: Build the adjacency list rep... | 7 | 0 | ['Tree', 'Depth-First Search', 'JavaScript'] | 0 |
maximum-number-of-k-divisible-components | Easy Java Solution using DFS in O(n) | easy-java-solution-using-dfs-in-on-by-wh-8abf | Approach\n Describe your approach to solving the problem. \nThe code aims to find the number of components in a graph such that the sum of values in each compon | whopiyushanand | NORMAL | 2023-10-03T08:49:12.818985+00:00 | 2023-10-03T08:49:12.819007+00:00 | 180 | false | # Approach\n<!-- Describe your approach to solving the problem. -->\nThe code aims to find the number of components in a graph such that the sum of values in each component is divisible by k. It uses a Depth-First Search (DFS) approach to traverse the graph.\n\n- Create an adjacency list to represent the graph where ea... | 7 | 0 | ['Tree', 'Depth-First Search', 'Java'] | 3 |
maximum-number-of-k-divisible-components | ✅100% Faster | DFS | Easy Intuitive approach | Java | C++ | Python | Detailed Video Explanation 🔥 | 100-faster-dfs-easy-intuitive-approach-j-9d0t | IntuitionThe problem can be thought of as dividing the graph into subtrees where the sum of the node values is divisible by k. By using Depth First Search (DFS) | sahilpcs | NORMAL | 2024-12-21T12:32:33.184043+00:00 | 2024-12-21T12:32:33.184043+00:00 | 766 | false | # Intuition
The problem can be thought of as dividing the graph into subtrees where the sum of the node values is divisible by `k`. By using Depth First Search (DFS), we can traverse the graph and calculate the sum of values in each subtree while keeping track of these divisible components.
# Approach
1. **Graph Repre... | 6 | 0 | ['Tree', 'Depth-First Search', 'Python', 'C++', 'Java'] | 2 |
maximum-number-of-k-divisible-components | 💢Faster✅💯 Lesser C++✅Python3🐍✅Java✅C✅Python🐍✅C#✅💥🔥💫Explained☠💥🔥 Beats 💯 | faster-lesser-cpython3javacpythoncexplai-19r4 | IntuitionApproach
JavaScript Code --> https://leetcode.com/problems/maximum-number-of-k-divisible-components/submissions/1484229155
C++ Code --> https://leetcod | Edwards310 | NORMAL | 2024-12-21T05:18:26.267934+00:00 | 2024-12-21T05:18:26.267934+00:00 | 734 | false | 
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your first thoughts on how to solve this problem. -->
- ***JavaScript Code -->*** https:... | 5 | 0 | ['Tree', 'Depth-First Search', 'C', 'Python', 'C++', 'Java', 'Go', 'Python3', 'JavaScript', 'C#'] | 0 |
maximum-number-of-k-divisible-components | Easy | DFS | easy-dfs-by-kamlesh012-x6rq | \nclass Solution {\npublic:\n int maxKDivisibleComponents(int n, vector<vector<int>>& edges, vector<int>& values, int k) {\n \n vector<vector<i | kamlesh012 | NORMAL | 2023-10-01T13:56:33.480264+00:00 | 2023-10-01T13:56:33.480291+00:00 | 169 | false | ```\nclass Solution {\npublic:\n int maxKDivisibleComponents(int n, vector<vector<int>>& edges, vector<int>& values, int k) {\n \n vector<vector<int>> adjl(n);\n for(auto i:edges){\n adjl[i[0]].push_back(i[1]);\n adjl[i[1]].push_back(i[0]);\n }\n int cnt=0ll;\... | 5 | 0 | ['Depth-First Search'] | 1 |
maximum-number-of-k-divisible-components | Java O(n) visit each subtree (DFS) | java-on-visit-each-subtree-dfs-by-ricola-i8wv | We start from the bottom of the tree.\u2028For each subtree we visit, we compute the sum of all its nodes (excluding the ones already split).\u2028If it\u2019s | ricola | NORMAL | 2023-09-30T16:01:32.424431+00:00 | 2023-10-01T09:43:29.057606+00:00 | 433 | false | We start from the bottom of the tree.\u2028For each subtree we visit, we compute the sum of all its nodes (excluding the ones already split).\u2028If it\u2019s divisible by k, it means that this subtree can be split from its parent.\n\n\nYou could ask : \u201CHow do we know that we should only consider entire subtrees?... | 5 | 1 | ['Breadth-First Search', 'Java'] | 3 |
maximum-number-of-k-divisible-components | DFS in python split if sum % k == 0 | dfs-in-python-split-if-sum-k-0-by-dinesh-572k | The sum of all nodes in the tree is always divisible by k.\nThus if a subtree with sum \'s\' is divisible by k then the rest of subtree sum \'temp\' will always | dinesh55 | NORMAL | 2023-09-30T16:01:01.598355+00:00 | 2023-09-30T16:01:54.647913+00:00 | 356 | false | The sum of all nodes in the tree is always divisible by k.\nThus if a subtree with sum \'s\' is divisible by k then the rest of subtree sum \'temp\' will always be divisible by k.\nHence all we have to do is <b>find the subtrees with sum % k == 0 and cut them </b>\n\nTo find the subtrees we will use DFS\n\npython code... | 5 | 0 | ['Depth-First Search', 'Graph', 'Python3'] | 0 |
maximum-number-of-k-divisible-components | Video Explanation (Solving problem step by step from scratch) | video-explanation-solving-problem-step-b-gdqh | Explanation\n\nClick here for the video\n\n# Code\n\ntypedef long long int ll;\nconst int N = 3e4+5;\n\nvector<vector<int>> g(N);\nvector<ll> val(N);\nint resul | codingmohan | NORMAL | 2023-09-30T16:33:08.015630+00:00 | 2023-09-30T16:33:08.015652+00:00 | 108 | false | # Explanation\n\n[Click here for the video](https://youtu.be/PZQLUgYwfJA)\n\n# Code\n```\ntypedef long long int ll;\nconst int N = 3e4+5;\n\nvector<vector<int>> g(N);\nvector<ll> val(N);\nint result;\nint K;\n\nint MaxComponents (int src, int par) {\n ll leftOver = val[src];\n \n for (auto i : g[src]) {\n ... | 4 | 0 | ['C++'] | 0 |
maximum-number-of-k-divisible-components | [Python3/C++/Java/Go] DFS -Detailed Explanation | python3cjavago-dfs-detailed-explanation-4y0s3 | Intuition
Finding the maximum components -> go up from leaves whenever we first meet group that divisible by k -> cut it immediately. Because the total values i | dolong2110 | NORMAL | 2024-12-21T11:58:59.220402+00:00 | 2024-12-21T19:08:13.677218+00:00 | 173 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
- Finding the maximum components -> go up from leaves whenever we first meet group that divisible by `k` -> cut it immediately. Because the total values is divisible for `k`, so cut soon is a greedy way to achieve it.
# Approach
<!-- Descr... | 3 | 0 | ['Tree', 'Depth-First Search', 'C++', 'Java', 'Go', 'Python3'] | 0 |
maximum-number-of-k-divisible-components | an intuitive? "top down" approach using euler tour and prefix sum | an-intuitive-top-down-approach-using-eul-ebbe | Intuition and ApproachFirstly, we observe that the sum of all nodes in the tree must be nk for some n∈N.Consider a simple case, a tree with root 1 and leaves 2 | ui_beam | NORMAL | 2024-12-21T08:54:46.764742+00:00 | 2024-12-21T08:54:46.764742+00:00 | 83 | false | # Intuition and Approach
Firstly, we observe that the sum of all nodes in the tree must be $$nk$$ for some $$n \in \N $$.
Consider a simple case, a tree with root 1 and leaves 2 and 3, with values 1,2,3 respectively, with $$k = 3$$.
We have two edges we can possibly "cut" and remove from our graph (i.e 1-2 and 1-3). ... | 3 | 0 | ['Tree', 'Depth-First Search', 'Prefix Sum', 'Eulerian Circuit', 'Python3'] | 2 |
maximum-number-of-k-divisible-components | DFS || C++ solution | dfs-c-solution-by-ashishcoder2002-yove | IntuitionApproachComplexity
Time complexity:
O(N)
Space complexity:
Code | ashishcoder2002 | NORMAL | 2024-12-21T08:38:29.521640+00:00 | 2024-12-21T08:38:29.521640+00:00 | 198 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
O(N)
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```cpp []
class Solution {
public:
int ans = 0... | 3 | 0 | ['Depth-First Search', 'C++'] | 0 |
maximum-number-of-k-divisible-components | 💡 Easy simple || DFS || Prefix Sum 🎯 | easy-simple-dfs-prefix-sum-by-cs_balotiy-acwn | Let n be the number of nodes in the graph, and let m be the number of edges in the graph.
Time complexity: O(n+m)
Space complexity: O(n+m)
Code | cs_balotiya | NORMAL | 2024-12-21T08:24:33.218549+00:00 | 2024-12-21T08:24:33.218549+00:00 | 17 | false | Let n be the number of nodes in the graph, and let m be the number of edges in the graph.
- Time complexity: O(n+m)
- Space complexity: O(n+m)
# Code
```cpp []
class Solution {
public:
long long dfs(int node, int parent, const vector<vector<int>>& adj, vector<int>& values, int& res, int k) {
long long pre... | 3 | 0 | ['Tree', 'Depth-First Search', 'C++'] | 0 |
maximum-number-of-k-divisible-components | Efficient DFS-Based Solution to Count K-Divisible Components in a Tree (O(n) Time Complexity) | efficient-dfs-based-solution-to-count-k-k1mt4 | IntuitionThe problem involves finding the number of connected components in a tree where the sum of node values in the component is divisible by k.
The intuitio | Hexagon_6_ | NORMAL | 2024-12-21T07:25:50.226043+00:00 | 2024-12-21T07:25:50.226043+00:00 | 24 | false | # Intuition
The problem involves finding the number of connected components in a tree where the sum of node values in the component is divisible by `k`.
The intuition is to use Depth First Search (DFS) to traverse the graph and calculate the sum of values for each subtree. Whenever a subtree's sum is divisible by `k`... | 3 | 0 | ['Tree', 'Depth-First Search', 'C++'] | 0 |
maximum-number-of-k-divisible-components | Easy Solution || C++ || DFS | easy-solution-dfs-by-shishirrsiam-3wjz | if it's help, please up ⬆ vote! ❤️Code | shishirRsiam | NORMAL | 2024-12-21T04:21:51.799832+00:00 | 2024-12-21T04:22:20.566356+00:00 | 558 | false | # if it's help, please up ⬆ vote! ❤️
# Code
```cpp []
class Solution {
public:
int ans = 0;
unordered_map<int, vector<int>> adj;
long dfs(int node, int parent, int &k, vector<int>& values)
{
long subTreeSum = values[node];
for(auto &child : adj[node])
{
if(child !=... | 3 | 0 | ['Tree', 'Depth-First Search', 'C++'] | 4 |
maximum-number-of-k-divisible-components | EASY and Simple TREE | easy-and-simple-tree-by-codewithsparsh-hmh9 | IntuitionThe problem requires splitting a graph into the maximum number of connected components where the sum of node values in each component is divisible by ( | CodeWithSparsh | NORMAL | 2024-12-21T02:26:05.122015+00:00 | 2024-12-21T02:26:32.227306+00:00 | 533 | false |
# Intuition
The problem requires splitting a graph into the maximum number of connected components where the sum of node values in each component is divisible by \(k\). This suggests a **tree traversal** approach to accumulate the values of nodes and check divisibility conditions. Depth-first search (DFS) is particul... | 3 | 0 | ['Tree', 'Depth-First Search', 'C', 'C++', 'Java', 'Go', 'Python3', 'Kotlin', 'JavaScript', 'Dart'] | 0 |
maximum-number-of-k-divisible-components | Rust solution - DFS | rust-solution-dfs-by-sunjesse-wryc | Code | sunjesse | NORMAL | 2024-12-21T00:38:52.665782+00:00 | 2024-12-21T00:38:52.665782+00:00 | 113 | false | # Code
```rust []
use std::collections::HashMap;
impl Solution {
fn dfs(seen: &mut Vec<bool>, G: &HashMap<usize, Vec<usize>>, values: &mut Vec<i32>, k: &i32, u: usize, ans: &mut i32) -> i32 {
seen[u] = true;
if !G.contains_key(&u) {
*ans += 1;
return 0;
}
fo... | 3 | 0 | ['Rust'] | 2 |
maximum-number-of-k-divisible-components | [Python3] Cut Leaf - BFS + Topological Sort - Simple Solution | python3-cut-leaf-bfs-topological-sort-si-p96b | 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 | dolong2110 | NORMAL | 2024-03-13T15:42:33.565518+00:00 | 2024-03-13T16:46:06.568275+00:00 | 78 | 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)$$ --... | 3 | 0 | ['Hash Table', 'Tree', 'Breadth-First Search', 'Topological Sort', 'Python3'] | 0 |
maximum-number-of-k-divisible-components | [C++|Java|Python3] DFS | cjavapython3-dfs-by-ye15-r4bo | C++\n\nclass Solution {\npublic:\n int maxKDivisibleComponents(int n, vector<vector<int>>& edges, vector<int>& values, int k) {\n vector<vector<int>> | ye15 | NORMAL | 2023-10-03T01:49:27.045794+00:00 | 2023-10-03T01:49:27.045815+00:00 | 64 | false | C++\n```\nclass Solution {\npublic:\n int maxKDivisibleComponents(int n, vector<vector<int>>& edges, vector<int>& values, int k) {\n vector<vector<int>> tree(n); \n for (auto& e : edges) {\n tree[e[0]].push_back(e[1]); \n tree[e[1]].push_back(e[0]); \n }\n \n ... | 3 | 0 | ['C', 'Java', 'Python3'] | 1 |
maximum-number-of-k-divisible-components | Easy Implementation | easy-implementation-by-talib5-t43b | 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 | talib5 | NORMAL | 2023-09-30T17:36:24.589581+00:00 | 2023-09-30T17:36:24.589602+00:00 | 36 | 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)$$ --... | 3 | 0 | ['Backtracking', 'Tree', 'Depth-First Search', 'Graph', 'Recursion', 'C++'] | 1 |
maximum-number-of-k-divisible-components | ✅☑[C++] ||Beats 95% || EXPLAINED🔥 | c-beats-95-explained-by-marksphilip31-vqgo | \n# PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approach\n(Also explained in the code)\n\n1. The function maxKDivisibleComponents takes several inputs: the number | MarkSPhilip31 | NORMAL | 2023-09-30T16:07:36.625677+00:00 | 2023-09-30T16:15:53.017523+00:00 | 364 | false | \n# *PLEASE UPVOTE IF IT HELPED*\n\n---\n\n\n# Approach\n***(Also explained in the code)***\n\n1. The function `maxKDivisibleComponents` takes several inputs: the number of nodes `n`, a list of edges, a list of values associated with each node, and an integer `k`.\n\n1. It uses dynamic programming to calculate `dp` val... | 3 | 0 | ['Dynamic Programming', 'C++'] | 0 |
maximum-number-of-k-divisible-components | Building a Tree and using DFS to prune the tree childs | building-a-tree-and-using-dfs-to-prune-t-31ro | IntuitionBuilding a Tree and using DFS to prune the tree childs.ApproachOnce we have our tree, we start buttom up if we have a child tree that is divisble by k | youssefsaad3000 | NORMAL | 2024-12-30T19:42:12.021758+00:00 | 2024-12-30T19:42:12.021758+00:00 | 11 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Building a Tree and using DFS to prune the tree childs.
# Approach
<!-- Describe your approach to solving the problem. -->
Once we have our tree, we start buttom up if we have a child tree that is divisble by k we prune it (remove it and c... | 2 | 0 | ['Python3'] | 0 |
maximum-number-of-k-divisible-components | most optimised java solution || java DFS || faster code | most-optimised-java-solution-java-dfs-fa-xd8m | import java.util.*;\n\nclass Solution {\n private int dfs(List> adj, int[] values, int k, int[] count, int curr, int parent) {\n long sum = values[cur | Seema_Kumari1 | NORMAL | 2024-12-21T13:31:17.402269+00:00 | 2024-12-21T13:31:17.402311+00:00 | 16 | false | import java.util.*;\n\nclass Solution {\n private int dfs(List<List<Integer>> adj, int[] values, int k, int[] count, int curr, int parent) {\n long sum = values[curr];\n for (int nbr : adj.get(curr)) {\n if (nbr != parent) {\n sum += dfs(adj, values, k, count, nbr, curr);\n ... | 2 | 0 | ['Tree', 'Depth-First Search', 'Binary Tree', 'Java'] | 0 |
maximum-number-of-k-divisible-components | Not that easy but at least the code is clean | not-that-easy-but-at-least-the-code-is-c-q5ca | Code | Nade4n | NORMAL | 2024-12-21T13:34:27.705670+00:00 | 2024-12-21T13:34:27.705670+00:00 | 50 | false |
# Code
```csharp []
public class Solution {
public int MaxKDivisibleComponents(int n, int[][] edges, int[] values, int k)
{
//build adjacent list
List<int>[] graph = new List<int>[n];
foreach(var edge in edges)
{
int node1=edge[0];
int node2=edge[1... | 2 | 0 | ['C#'] | 0 |
maximum-number-of-k-divisible-components | DFS | dfs-by-ahmedsayed1-iabl | ExplainingLets starts with the root 0 and make dfs and for every summation of values subtree of current node if its divisable by k increase my ans and make the | AhmedSayed1 | NORMAL | 2024-12-21T09:01:08.565519+00:00 | 2024-12-21T09:03:44.858585+00:00 | 44 | false | # Explaining
<h3>Lets starts with the root 0 and make dfs and for every summation of values subtree of current node if its divisable by k increase my ans and make the ret equal to 0 (that means i removed the edge between node and its parent) and when node is the root (node = 0) if the final summation is not divisable... | 2 | 0 | ['Tree', 'Depth-First Search', 'C++'] | 0 |
maximum-number-of-k-divisible-components | Go Solution (bahasa indonesia) | go-solution-by-anastasyawlf-4tr3 | Approach
DFS (Depth-First Search):
Hitung total nilai di subtree setiap node.
Pastikan subtree bisa menjadi komponen yang habis dibagi 𝑘.
Validasi Komponen: | anastasyawlf | NORMAL | 2024-12-21T08:45:50.264299+00:00 | 2024-12-21T08:49:33.004584+00:00 | 79 | false | # Approach
<!-- Describe your approach to solving the problem. -->
1) DFS (Depth-First Search):
- Hitung total nilai di subtree setiap node.
- Pastikan subtree bisa menjadi komponen yang habis dibagi 𝑘.
2) Validasi Komponen:
- Cek apakah dengan memotong suatu edge, subtree yang dihasilkan valid.
3) Optimasi:
- Lakukan... | 2 | 0 | ['Go'] | 0 |
maximum-number-of-k-divisible-components | 🌳 Breaking the Tree into Divisible Components 🌟|| Beats 100% ✅ || JAVA ✅ | breaking-the-tree-into-divisible-compone-o5ex | IntuitionThe problem is to split a tree into the maximum number of connected components such that the sum of the node values in each component is divisible by ( | code-shinobi | NORMAL | 2024-12-21T07:20:46.559030+00:00 | 2024-12-21T07:20:46.559030+00:00 | 133 | false | # Intuition
The problem is to split a tree into the maximum number of connected components such that the sum of the node values in each component is divisible by \(k\).
The key observation is that we can use a **DFS traversal** to compute the sum of the values in each subtree. By checking the divisibility of these su... | 2 | 0 | ['Java'] | 0 |
maximum-number-of-k-divisible-components | Java code | java-code-by-ayeshakhan7-uofd | Code | ayeshakhan7 | NORMAL | 2024-12-21T07:17:34.459544+00:00 | 2024-12-21T07:17:34.459544+00:00 | 184 | false |
# Code
```java []
class Solution {
public int maxKDivisibleComponents(int n, int[][] edges, int[] values, int k) {
// adjacency list from edges
List<Integer>[] adjList = new ArrayList[n];
for (int i = 0; i < n; i++) {
adjList[i] = new ArrayList<>();
}
for (int[... | 2 | 0 | ['Java'] | 0 |
maximum-number-of-k-divisible-components | [Javascript] DFS O(n) / O(n) | javascript-dfs-on-on-by-_savely-bz2g | Intuitionevery subtree with the sum of nodes values divisible by K will represent a separate componentCode | _savely | NORMAL | 2024-12-21T06:13:22.429707+00:00 | 2024-12-21T06:13:22.429707+00:00 | 73 | false | # Intuition
every subtree with the sum of nodes values divisible by K will represent a separate component
# Code
```javascript []
/**
* @param {number} n
* @param {number[][]} edges
* @param {number[]} values
* @param {number} k
* @return {number}
*/
var maxKDivisibleComponents = function(n, edges, values, k) {... | 2 | 0 | ['Tree', 'Depth-First Search', 'JavaScript'] | 0 |
maximum-number-of-k-divisible-components | Follow for more easy solution | follow-for-more-easy-solution-by-dev_bha-ndni | Code | dev_bhatt202 | NORMAL | 2024-12-21T05:51:48.730595+00:00 | 2024-12-21T05:51:48.730595+00:00 | 29 | false |
# Code
```java []
import java.util.*;
class Solution {
private long cnt = 0;
private void getSubtree(int node, List<List<Integer>> adj, int parent, long[] subtree) {
if (adj.get(node).size() == 1 && adj.get(node).get(0) == parent) return;
for (int neighbor : adj.get(node)) {
if (... | 2 | 0 | ['Tree', 'Depth-First Search', 'Java'] | 1 |
maximum-number-of-k-divisible-components | Here Comes Easiest Solution 🥳🥳 | here-comes-easiest-solution-by-sarangkal-m2ym | IntuitionThe goal is to maximize the number of connected components in the tree such that the sum of node values in each component is divisible by k. To achieve | Sarangkale66 | NORMAL | 2024-12-21T05:33:34.867348+00:00 | 2024-12-21T05:33:34.867348+00:00 | 260 | false | # Intuition
The goal is to maximize the number of connected components in the tree such that the sum of node values in each component is divisible by **k**. To achieve this, we explore the tree using Depth-First Search **(DFS)** and leverage modular arithmetic to determine divisibility conditions.
# Approach
Tree Repr... | 2 | 0 | ['Python', 'C++', 'Java', 'TypeScript', 'Python3', 'JavaScript'] | 0 |
maximum-number-of-k-divisible-components | Simple code with detailed explanation. (I bet you will understand this :)) | simple-code-with-detailed-explanation-i-3mggd | IntuitionThe problem involves finding the maximum number of connected components in a tree such that the sum of the values in each component is divisible by k. | nishita_shah1 | NORMAL | 2024-12-21T04:17:44.956798+00:00 | 2024-12-21T04:17:44.956798+00:00 | 46 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem involves finding the maximum number of connected components in a tree such that the sum of the values in each component is divisible by k. A connected component can be formed by removing edges in the tree. To solve this, the key... | 2 | 0 | ['Depth-First Search', 'C++'] | 0 |
maximum-number-of-k-divisible-components | DFS || Hard to get Intuition || ---- | dfs-hard-to-get-intuition-by-jayavenkate-fnck | IntuitionApproachComplexity
Time complexity: O(N)
Space complexity: O(N)
Code | jayavenkatesh | NORMAL | 2024-12-21T03:42:20.038776+00:00 | 2024-12-21T03:42:20.038776+00:00 | 11 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity: O(N)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: O(N)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->... | 2 | 0 | ['Depth-First Search', 'Java'] | 0 |
maximum-number-of-k-divisible-components | Swift | DFS | swift-dfs-by-noahfeld-j62h | Complexity
Time complexity: O(n)
Space complexity: O(n)
Code | noahfeld | NORMAL | 2024-12-21T03:30:18.392465+00:00 | 2024-12-21T03:30:18.392465+00:00 | 49 | false | # Complexity
- Time complexity: $$O(n)$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: $$O(n)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```swift []
class Solution {
func maxKDivisibleComponents(_ n: Int, _ edges: [[Int]], _ values: [Int], _ k: Int) -> Int {
... | 2 | 0 | ['Swift'] | 0 |
maximum-number-of-k-divisible-components | Beats 53%, Enough for interviews...Maybe! | beats-53-enough-for-interviewsmaybe-by-v-u4xp | IntuitionEven though this seems like a tree, let's consider this a Graph and a simple dfs with a carry forward would workApproachdfs starting from any node woul | vtkp | NORMAL | 2024-12-21T02:58:36.696065+00:00 | 2024-12-21T02:58:36.696065+00:00 | 119 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Even though this seems like a tree, let's consider this a Graph and a simple dfs with a carry forward would work
# Approach
<!-- Describe your approach to solving the problem. -->
dfs starting from any node would do the same job since we a... | 2 | 0 | ['C++'] | 0 |
maximum-number-of-k-divisible-components | DFS Technique || O(V+E) Time Complexity|| Beats 96%Users || Beginner Friendly | dfs-technique-ove-time-complexity-beats-3sa3u | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | Heisenberg_wc | NORMAL | 2024-12-21T02:32:56.783434+00:00 | 2024-12-21T02:32:56.783434+00:00 | 186 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
... | 2 | 0 | ['C++'] | 1 |
maximum-number-of-k-divisible-components | C++ Solution using DFS , Easy Approach and Explain the code | c-solution-using-dfs-easy-approach-and-e-e2ni | IntuitionThe problem requires us to maximize the number of components in a tree such that the sum of the values in each component is divisible by a given intege | Dhanu07 | NORMAL | 2024-12-21T02:26:04.655123+00:00 | 2024-12-21T02:29:22.026262+00:00 | 65 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem requires us to maximize the number of components in a tree such that the sum of the values in each component is divisible by a given integer ( k ). A tree is a connected acyclic graph, and removing edges can create multiple comp... | 2 | 0 | ['Tree', 'Depth-First Search', 'C++'] | 1 |
maximum-number-of-k-divisible-components | C++ DFS | c-dfs-by-pb_matrix-5bf0 | \n#define ll long long\nclass Solution {\npublic:\n unordered_map<int,vector<int>>adj;\n vector<ll>sub;\n vector<int>val;\n int k;\n ll dfs(int n | pb_matrix | NORMAL | 2024-06-01T11:18:14.459784+00:00 | 2024-06-01T11:18:14.459808+00:00 | 78 | false | ```\n#define ll long long\nclass Solution {\npublic:\n unordered_map<int,vector<int>>adj;\n vector<ll>sub;\n vector<int>val;\n int k;\n ll dfs(int node,int par){\n ll sum=val[node];\n for(auto it:adj[node]){\n if(it!=par){\n sum+=dfs(it,node);\n }\n }\n ... | 2 | 0 | ['Depth-First Search', 'C++'] | 0 |
maximum-number-of-k-divisible-components | Easy Solution | With Recursive Tree | Simple | Cpp | Java | Python | 🐍 | 🔥 | easy-solution-with-recursive-tree-simple-6pxz | Approach\n\n- We go to the depth of tree and and if we find it divisible by k than , we split that edge and increment our componenets\n- else we merge(add the v | TIME2LIVE | NORMAL | 2023-10-01T13:27:44.241144+00:00 | 2023-10-01T13:27:44.241174+00:00 | 78 | false | # Approach\n\n- We go to the depth of tree and and if we find it divisible by k than , we split that edge and increment our componenets\n- else we merge(add the value) that componenet to its parent \n- we repeat this until our dfs calls end \n\n {\n\tvis[ | Harsh__005 | NORMAL | 2024-12-21T13:45:03.237864+00:00 | 2024-12-21T13:45:03.237884+00:00 | 62 | false | ## **CODE**\n```Java []\n// 0 - sum, 1 - number of components\nprivate long[] dfs(int curr, Map<Integer, List<Integer>> map, int[] values, int k, boolean[] vis) {\n\tvis[curr] = true;\n\tlong[] ans = new long[2];\n\tans[0] += values[curr];\n\tfor(int child : map.getOrDefault(curr, new ArrayList<>())) {\n\t\tif(vis[chil... | 1 | 0 | ['Depth-First Search', 'Java'] | 1 |
maximum-number-of-k-divisible-components | Topological sort + DFS solution | topological-sort-dfs-solution-by-andrewj-qd9p | IntuitionConsider any set of vertex-disjoint k-divisible connected components. Then, any connected component that contains exactly all the edges in those connec | andrewjyuan | NORMAL | 2024-12-21T23:55:32.692786+00:00 | 2024-12-21T23:57:45.134968+00:00 | 9 | false | # Intuition
Consider any set of vertex-disjoint k-divisible connected components. Then, any connected component that contains exactly all the edges in those connected components must also be k-divisible.
# Approach
We proceed with a greedy approach to this solution. Whenever we encounter a smallest possible subtree th... | 1 | 0 | ['Depth-First Search', 'Topological Sort', 'Python3'] | 0 |
maximum-number-of-k-divisible-components | C# Solution for Maximum Number of K-Divisible Components Problem | c-solution-for-maximum-number-of-k-divis-7qx9 | IntuitionThe problem requires splitting the tree into connected components such that the sum of node values in each component is divisible by k . Since the tre | Aman_Raj_Sinha | NORMAL | 2024-12-21T20:44:44.903892+00:00 | 2024-12-21T20:44:44.903892+00:00 | 13 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem requires splitting the tree into connected components such that the sum of node values in each component is divisible by k . Since the tree has no cycles, removing an edge always results in two separate components. The goal is ... | 1 | 0 | ['C#'] | 0 |
maximum-number-of-k-divisible-components | O(N) DFS Python Solution with DP | on-python-solution-by-avendramini-bqr6 | IntuitionThe problem requires splitting a tree into components such that the sum of values in each component is divisible by k. Since a tree is connected and a | avendramini | NORMAL | 2024-12-21T20:42:45.094520+00:00 | 2024-12-21T22:02:18.428568+00:00 | 7 | false | # Intuition
The problem requires splitting a tree into components such that the sum of values in each component is divisible by *k*. Since a tree is connected and acyclic, we can use a DFS traversal to calculate the sum of values for each subtree and determine the maximum number of valid components. By leveraging dyna... | 1 | 0 | ['Tree', 'Depth-First Search', 'Python3'] | 0 |
maximum-number-of-k-divisible-components | Java Solution for Maximum Number of K-Divisible Components Problem | java-solution-for-maximum-number-of-k-di-pf7u | IntuitionThe problem asks us to find the maximum number of components in a tree such that the sum of node values in each component is divisible by k. The approa | Aman_Raj_Sinha | NORMAL | 2024-12-21T20:37:47.589360+00:00 | 2024-12-21T20:37:47.589360+00:00 | 12 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem asks us to find the maximum number of components in a tree such that the sum of node values in each component is divisible by k. The approach leverages the properties of trees and their structure:
1. Leaf Nodes and Tree Traversa... | 1 | 0 | ['Java'] | 0 |
maximum-number-of-k-divisible-components | Java 17ms runtime, 100% time and 100% space! Detailed Explanation of Maximum Optimizations | java-17ms-runtime-100-time-and-100-space-x3ms | IntuitionThe core idea to the approach is to understand that adding or subtracting multiples of k to some number, will never change the results divisibility by | mshoosterman | NORMAL | 2024-12-21T20:20:35.208855+00:00 | 2024-12-21T20:20:35.208855+00:00 | 7 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The core idea to the approach is to understand that adding or subtracting multiples of k to some number, will never change the results divisibility by k. This means you can always prune segments which are divisible by k.
This means you sh... | 1 | 0 | ['Java'] | 0 |
maximum-number-of-k-divisible-components | DFS Approach || O(n) | dfs-approach-on-by-satwikprem-fifd | IntuitionTo maximize the number of connected components in a tree where each component's sum is divisible by k, we use DFS. By calculating subtree sums, we spli | satwikprem | NORMAL | 2024-12-21T18:40:15.924526+00:00 | 2024-12-21T18:40:15.924526+00:00 | 17 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
To maximize the number of connected components in a tree where each component's sum is divisible by `k`, we use DFS. By calculating subtree sums, we split the tree whenever a subtree sum is divisible by `k`.
# Approach
<!-- Describe your ap... | 1 | 0 | ['Tree', 'Depth-First Search', 'Java'] | 0 |
maximum-number-of-k-divisible-components | 2872. Maximum Number of K-Divisible Components | 2872-maximum-number-of-k-divisible-compo-ea3a | IntuitionThe problem involves finding the maximum number of components in a tree such that the sum of the node values in each component is divisible by (k). Thi | ayushrawat220804 | NORMAL | 2024-12-21T18:31:15.013836+00:00 | 2024-12-21T18:31:37.713948+00:00 | 23 | false | # Intuition
The problem involves finding the maximum number of components in a tree such that the sum of the node values in each component is divisible by \(k\). This can be achieved using Depth First Search (DFS) to calculate the sum of subtree values and splitting the components whenever a divisible sum is encounte... | 1 | 0 | ['C++'] | 0 |
maximum-number-of-k-divisible-components | Best approch to solve step by step with explaination using python | best-approch-to-solve-step-by-step-with-iuqv3 | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | abhishekai01 | NORMAL | 2024-12-21T18:10:08.077789+00:00 | 2024-12-21T18:10:08.077789+00:00 | 7 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 1 | 0 | ['Python'] | 0 |
maximum-number-of-k-divisible-components | DFS. Simple and clear solution. Runtime 100% | dfs-simple-and-clear-solution-runtime-10-wgxn | null | xxxxkav | NORMAL | 2024-12-21T15:56:59.568559+00:00 | 2024-12-21T15:58:55.841533+00:00 | 23 | false | ```
class Solution:
def maxKDivisibleComponents(self, n: int, edges: List[List[int]], values: List[int], k: int) -> int:
graph = [[] for _ in range(n)]
for u, v in edges:
graph[u].append(v)
graph[v].append(u)
self.ans = 0
def dfs(root, parent):
s ... | 1 | 0 | ['Depth-First Search', 'Python3'] | 0 |
maximum-number-of-k-divisible-components | Solution in || Python || | solution-in-python-by-rajatcoder2255-bq4i | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | rajatcoder2255 | NORMAL | 2024-12-21T15:10:04.677659+00:00 | 2024-12-21T15:10:04.677659+00:00 | 17 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 1 | 0 | ['Python3'] | 0 |
maximum-number-of-k-divisible-components | C++ Greedy Solution | c-greedy-solution-by-retire-wa1e | Code | Retire | NORMAL | 2024-12-21T14:42:54.878304+00:00 | 2024-12-21T14:42:54.878304+00:00 | 58 | false | # Code
```cpp []
class Solution {
public:
vector<vector<int>> g;
pair<int, int> dfs(int pre, int cur, vector<int> &values, int k) {
pair<int, int> res = {values[cur] % k, 0};
for (auto &x: g[cur]) {
if (x == pre) continue;
auto [cnt, pairs] = dfs(cur, x, values, k);
... | 1 | 0 | ['Greedy', 'C++'] | 0 |
maximum-number-of-k-divisible-components | Easy Solution With C++ | easy-solution-with-c-by-ashraf-abu3lrai-3p95 | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | Al-Khwarizmi_ | NORMAL | 2024-12-21T14:11:53.511193+00:00 | 2024-12-21T14:11:53.511193+00:00 | 23 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 1 | 0 | ['C++'] | 0 |
maximum-number-of-k-divisible-components | C++ 🖥️ | Beats 80% 🏆 | Easy Solution Explained 🧠 | O(n) ⏱️ | Greedy Tree Traversal with Queue 🌳 | c-beats-80-easy-solution-explained-on-gr-v1kx | IntuitionThe task involves splitting a tree into the maximum number of components such that each has a sum divisible by 𝑘. By focusing on leaf nodes first and p | Raza-Jaun | NORMAL | 2024-12-21T13:58:35.538637+00:00 | 2024-12-21T13:59:35.915290+00:00 | 36 | false | # Intuition
The task involves splitting a tree into the maximum number of components such that each has a sum divisible by 𝑘. By focusing on leaf nodes first and propagating their sums to their parent nodes, we simplify the problem iteratively, ensuring an efficient and structured solution.
---
# Approach
Build a g... | 1 | 0 | ['Math', 'Tree', 'Depth-First Search', 'Breadth-First Search', 'Graph', 'Topological Sort', 'Queue', 'Binary Tree', 'Iterator', 'C++'] | 0 |
maximum-number-of-k-divisible-components | A simple solution using DFS | a-simple-solution-using-dfs-by-kumar_van-a8q3 | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | Kumar_Vanshaj | NORMAL | 2024-12-21T13:39:57.661894+00:00 | 2024-12-21T13:39:57.661894+00:00 | 15 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 1 | 0 | ['Linked List', 'Dynamic Programming', 'Greedy', 'Depth-First Search', 'JavaScript'] | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.