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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
find-duplicate-subtrees | Solution in C++ | solution-in-c-by-ashish_madhup-7j2n | 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 | ashish_madhup | NORMAL | 2023-02-28T12:55:51.246357+00:00 | 2023-02-28T12:55:51.246405+00:00 | 251 | 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)$$ --... | 5 | 0 | ['C++'] | 1 |
find-duplicate-subtrees | JAVA Solution || Easy | java-solution-easy-by-sanjay1305-prwc | \nclass Solution {\n Map<String,Integer> map=new HashMap<>();\n List<TreeNode> res=new ArrayList<>();\n public List<TreeNode> findDuplicateSubtrees(Tre | sanjay1305 | NORMAL | 2023-02-28T03:55:18.571656+00:00 | 2023-02-28T08:59:31.790543+00:00 | 774 | false | ```\nclass Solution {\n Map<String,Integer> map=new HashMap<>();\n List<TreeNode> res=new ArrayList<>();\n public List<TreeNode> findDuplicateSubtrees(TreeNode root) {\n helper(root);\n return res;\n }\n public String helper(TreeNode root){\n if(root==null) return "";\n String... | 5 | 0 | ['Depth-First Search', 'Binary Tree', 'Java'] | 0 |
find-duplicate-subtrees | Python DFS with Hashing | python-dfs-with-hashing-by-hong_zhao-ma5m | python []\nclass Solution:\n def findDuplicateSubtrees(self, root: Optional[TreeNode]) -> List[Optional[TreeNode]]:\n\n hmap = defaultdict(int)\n | hong_zhao | NORMAL | 2023-02-28T00:35:32.444707+00:00 | 2023-02-28T00:35:32.444751+00:00 | 1,665 | false | ```python []\nclass Solution:\n def findDuplicateSubtrees(self, root: Optional[TreeNode]) -> List[Optional[TreeNode]]:\n\n hmap = defaultdict(int)\n res = []\n def dfs(node):\n if not node: return \'\'\n l, r = dfs(node.left), dfs(node.right)\n struct = f\'l{l}{n... | 5 | 0 | ['Python3'] | 1 |
find-duplicate-subtrees | 🥳【Python3】🔥 Easy Solution - Compare Serialisations of TreeNodes | python3-easy-solution-compare-serialisat-v44k | Approach\nSerialize each TreeNode and compare with memorized TreeNode serialization. Please see comments in code.\n\nExample of serialization. For the tree in e | yimingdai | NORMAL | 2023-02-28T00:15:20.289554+00:00 | 2023-02-28T00:23:17.853289+00:00 | 1,450 | false | ## Approach\nSerialize each TreeNode and compare with memorized TreeNode serialization. Please see comments in code.\n\nExample of serialization. For the tree in example one, each tree is serialized into the following form:\n```\n4,#,#,\n2,4,#,#,#,\n4,#,#,\n2,4,#,#,#,\n4,#,#,\n3,2,4,#,#,#,4,#,#,\n1,2,4,#,#,#,3,2,4,#,#,... | 5 | 0 | ['Tree', 'Depth-First Search', 'Recursion', 'Python3'] | 0 |
find-duplicate-subtrees | Golang O(n) solution with map | golang-on-solution-with-map-by-tuanbiebe-lnb5 | \nfunc PrettyPrint(data interface{}) {\n\tvar p []byte\n\t// var err := error\n\tp, err := json.MarshalIndent(data, "", "\\t")\n\tif err != nil {\n\t\tfmt.Pr | tuanbieber | NORMAL | 2022-03-14T10:36:25.633733+00:00 | 2022-03-14T10:36:25.633762+00:00 | 356 | false | ```\nfunc PrettyPrint(data interface{}) {\n\tvar p []byte\n\t// var err := error\n\tp, err := json.MarshalIndent(data, "", "\\t")\n\tif err != nil {\n\t\tfmt.Println(err)\n\t\treturn\n\t}\n\tfmt.Printf("%s \\n", p)\n}\n\nfunc findDuplicateSubtrees(root *TreeNode) []*TreeNode {\n var res []*TreeNode\n \n rec... | 5 | 1 | ['Go'] | 3 |
find-duplicate-subtrees | Python O(n) simple . beats 97% | python-on-simple-beats-97-by-ketansomvan-o8bt | \n\tdef dfs(self,root):\n if not root:\n return \' \'\n n=str(root.val)+","+self.dfs(root.left)+","+self.dfs(root.right)\n if n | ketansomvanshi007 | NORMAL | 2022-01-04T13:23:13.069928+00:00 | 2022-01-04T13:24:52.867569+00:00 | 653 | false | ```\n\tdef dfs(self,root):\n if not root:\n return \' \'\n n=str(root.val)+","+self.dfs(root.left)+","+self.dfs(root.right)\n if n in self.s and n not in self.counted:\n self.counted.add(n)\n self.ans.append(root)\n return n\n self.s.add(n)\n ... | 5 | 0 | ['Depth-First Search', 'Python'] | 0 |
find-duplicate-subtrees | Clean Javascript Post-Order solution | clean-javascript-post-order-solution-by-4y6y9 | \nvar findDuplicateSubtrees = function(root) {\n let seen = {};\n let output = [];\n \n traverse(root);\n \n return output; \n \n functi | Adetomiwa | NORMAL | 2021-11-15T23:24:09.564188+00:00 | 2021-11-15T23:24:09.564223+00:00 | 642 | false | ```\nvar findDuplicateSubtrees = function(root) {\n let seen = {};\n let output = [];\n \n traverse(root);\n \n return output; \n \n function traverse(node){\n if(!node) return "null";\n \n let left = traverse(node.left);\n let right = traverse(node.right);\n \... | 5 | 0 | ['Depth-First Search', 'JavaScript'] | 2 |
find-duplicate-subtrees | C# HashCode | c-hashcode-by-dana-n-kt86 | I ended up using hash codes here and it worked well enough to get accepted. It is not guaranteed to be unique, in order for that you need to do deep comparision | dana-n | NORMAL | 2021-11-12T18:52:37.808198+00:00 | 2023-02-28T20:52:06.135799+00:00 | 314 | false | I ended up using hash codes here and it worked well enough to get accepted. It is not guaranteed to be unique, in order for that you need to do deep comparision. Some of the solutions serialize the tree into a string which seems to work well. In terms of performance, this solution is pretty dang fast!\n\n```cs\npublic ... | 5 | 0 | [] | 2 |
find-duplicate-subtrees | Easy O(N) Java solution | easy-on-java-solution-by-anweban-7c9n | \nclass Solution {\n public List<TreeNode> findDuplicateSubtrees(TreeNode root) {\n Map<String,TreeNode> map = new HashMap<>();\n postOrderTrav | anweban | NORMAL | 2021-05-25T17:04:16.733067+00:00 | 2021-05-25T17:04:16.733109+00:00 | 455 | false | ```\nclass Solution {\n public List<TreeNode> findDuplicateSubtrees(TreeNode root) {\n Map<String,TreeNode> map = new HashMap<>();\n postOrderTraversal(root, map, new StringBuilder());\n \n List<TreeNode> res = new ArrayList<>();\n for(TreeNode tree : map.values()){\n if... | 5 | 0 | [] | 0 |
find-duplicate-subtrees | Python O(N) Hashmap Solution | python-on-hashmap-solution-by-wei_lun-yfd4 | We ask the question : FOR EACH SUBTREE, WHAT ARE ITS ROOTS?\n\n2. In other words, we want to keep track of {subtree1: [root1, root2, ...], ...}, where root1 and | wei_lun | NORMAL | 2020-10-07T13:19:05.736433+00:00 | 2020-10-07T13:19:05.736484+00:00 | 523 | false | 1. We ask the question : **FOR EACH SUBTREE, WHAT ARE ITS ROOTS?**\n\n2. In other words, we want to keep track of **{subtree1: [root1, root2, ...], ...}**, where root1 and root2 are the roots of subtree1, in `sub_roots_map`\n\n3. During the `preorder` traversal, we shall return the **string representation of the subtre... | 5 | 0 | ['Python'] | 0 |
find-duplicate-subtrees | c# DFS | c-dfs-by-finefurrydove-nqw1 | \n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * public int val;\n * public TreeNode left;\n * public TreeNode right;\n | FineFurryDove | NORMAL | 2020-09-29T17:25:16.056152+00:00 | 2020-09-29T17:25:16.056201+00:00 | 334 | false | ```\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * public int val;\n * public TreeNode left;\n * public TreeNode right;\n * public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {\n * this.val = val;\n * this.left = left;\n * this.right ... | 5 | 0 | ['Depth-First Search'] | 1 |
find-duplicate-subtrees | Java Simple DFS | java-simple-dfs-by-arunvt1983-l6ia | \n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * int val;\n * TreeNode left;\n * TreeNode right;\n * TreeNode(int x) | arunvt1983 | NORMAL | 2019-10-31T14:00:02.311963+00:00 | 2019-10-31T14:00:02.312016+00:00 | 719 | false | ```\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * int val;\n * TreeNode left;\n * TreeNode right;\n * TreeNode(int x) { val = x; }\n * }\n */\nclass Solution {\n Map<String,Integer> subTrees = new HashMap<>();\n List<TreeNode> duplicates = new ArrayList<>();\n\n pu... | 5 | 0 | [] | 0 |
find-duplicate-subtrees | 652: Solution with step by step explanation | 652-solution-with-step-by-step-explanati-oszh | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n1. Create an empty dictionary freq to store the frequency of each subtree | Marlen09 | NORMAL | 2023-03-19T06:00:13.245502+00:00 | 2023-03-19T06:00:13.245536+00:00 | 151 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Create an empty dictionary freq to store the frequency of each subtree.\n2. Define a helper function traverse(node) to traverse the binary tree recursively and get the string representation of each subtree:\n - If the ... | 4 | 0 | ['Python3'] | 0 |
find-duplicate-subtrees | 📌📌Python3 || ⚡42 ms, faster than 97.10% of Python3 | python3-42-ms-faster-than-9710-of-python-xq38 | \n\nclass Solution:\n def findDuplicateSubtrees(self, root: Optional[TreeNode]) -> List[Optional[TreeNode]]:\n map = {}\n res = set()\n | harshithdshetty | NORMAL | 2023-02-28T14:19:00.764021+00:00 | 2023-02-28T14:19:00.764053+00:00 | 1,824 | false | \n```\nclass Solution:\n def findDuplicateSubtrees(self, root: Optional[TreeNode]) -> List[Optional[TreeNode]]:\n map = {}\n res = set()\n def check(node):\n if not node:\n ... | 4 | 0 | ['Tree', 'Depth-First Search', 'Python', 'Python3'] | 1 |
find-duplicate-subtrees | O(n) Solution | Very Easy to Understand | on-solution-very-easy-to-understand-by-c-gh82 | 1.Perform a depth-first search (DFS) traversal of the binary tree.\n2.For each node visited during the traversal, generate a unique string representation of the | cOde_Ranvir25 | NORMAL | 2023-02-28T12:45:23.151848+00:00 | 2023-02-28T12:45:23.151889+00:00 | 512 | false | **1.Perform a depth-first search (DFS) traversal of the binary tree.\n2.For each node visited during the traversal, generate a unique string representation of the subtree rooted at that node using a subtree serialization technique.**\n**3.Store the string representation in a hash map along with the node that is the roo... | 4 | 0 | ['Java'] | 1 |
find-duplicate-subtrees | Easy Solution Java|| 80% faster Solution | easy-solution-java-80-faster-solution-by-3is6 | Intuition\n Describe your first thoughts on how to solve this problem. Using HashMap\n\n# Approach\n Describe your approach to solving the problem. \n\n# Comple | Soumyabehera123 | NORMAL | 2023-02-28T04:26:17.948094+00:00 | 2023-02-28T04:26:17.948125+00:00 | 991 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->Using HashMap\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:18ms || Beats 80% of Solution\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add yo... | 4 | 0 | ['Java'] | 1 |
find-duplicate-subtrees | PHP - easy understanding / Beats 100% | php-easy-understanding-beats-100-by-ting-nfsa | Explain\n\nSave the structure as string while travel the nodes.\n\n# Code\n\nclass Solution {\n\n /**\n * @param TreeNode $root\n * @return TreeNode[ | tinghaolai | NORMAL | 2023-02-28T03:09:11.146228+00:00 | 2023-02-28T03:09:11.146275+00:00 | 290 | false | # Explain\n\nSave the structure as string while travel the nodes.\n\n# Code\n```\nclass Solution {\n\n /**\n * @param TreeNode $root\n * @return TreeNode[]\n */\n function findDuplicateSubtrees($root) {\n $this->subTrees = [];\n $this->result = [];\n $this->runNode($root);\n\n ... | 4 | 0 | ['PHP'] | 1 |
find-duplicate-subtrees | [C++] || Postorder Traversal + Map | c-postorder-traversal-map-by-rahul921-02se | \nclass Solution {\npublic:\n unordered_map<string,int> dp ;\n vector<TreeNode*> ans ;\n \n string solve(TreeNode * root){\n if(!root) return | rahul921 | NORMAL | 2022-08-07T13:08:26.850322+00:00 | 2022-08-07T13:08:26.850357+00:00 | 481 | false | ```\nclass Solution {\npublic:\n unordered_map<string,int> dp ;\n vector<TreeNode*> ans ;\n \n string solve(TreeNode * root){\n if(!root) return "" ;\n \n string left = solve(root->left) ;\n string right = solve(root->right) ;\n \n string code = to_string(root->val)... | 4 | 0 | ['C'] | 0 |
find-duplicate-subtrees | C++ | Easy to understand | c-easy-to-understand-by-dhakad_g-2jjo | \nclass Solution {\npublic:\n unordered_map<string, int> mp;\n vector<TreeNode*> ans;\n string dfs(TreeNode *p)\n {\n if(!p) return "";\n | dhakad_g | NORMAL | 2022-06-28T14:27:36.482545+00:00 | 2022-06-28T14:27:36.482590+00:00 | 317 | false | ```\nclass Solution {\npublic:\n unordered_map<string, int> mp;\n vector<TreeNode*> ans;\n string dfs(TreeNode *p)\n {\n if(!p) return "";\n string res = to_string(p->val) + "|" + dfs(p->left) + "|" + dfs(p->right);\n mp[res] ++;\n if(mp[res] == 2) ans.push_back(p);\n retu... | 4 | 0 | ['Depth-First Search', 'C'] | 0 |
find-duplicate-subtrees | Java | HashMap | java-hashmap-by-meerasuthar1122-bd36 | \n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * int val;\n * TreeNode left;\n * TreeNode right;\n * TreeNode() {}\n | MeeraSuthar1122 | NORMAL | 2022-04-06T15:09:39.039097+00:00 | 2022-04-06T15:09:39.039132+00:00 | 700 | false | ```\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * int val;\n * TreeNode left;\n * TreeNode right;\n * TreeNode() {}\n * TreeNode(int val) { this.val = val; }\n * TreeNode(int val, TreeNode left, TreeNode right) {\n * this.val = val;\n * this.left = l... | 4 | 0 | ['Java'] | 0 |
find-duplicate-subtrees | Python, simple | python-simple-by-sadomtsevvs-wmeb | \ndef findDuplicateSubtrees(self, root: Optional[TreeNode]) -> list[Optional[TreeNode]]:\n\n result = []\n paths = defaultdict(int)\n\t\n def get_path( | sadomtsevvs | NORMAL | 2022-02-25T23:58:24.849885+00:00 | 2022-02-25T23:58:24.849912+00:00 | 435 | false | ```\ndef findDuplicateSubtrees(self, root: Optional[TreeNode]) -> list[Optional[TreeNode]]:\n\n result = []\n paths = defaultdict(int)\n\t\n def get_path(node):\n if not node:\n return "None"\n else:\n path = str(node.val)\n path += \'.\' + get_path(node.left)\n ... | 4 | 0 | ['Python'] | 0 |
find-duplicate-subtrees | Post Order Traversal | Unordered_Map | C++ | post-order-traversal-unordered_map-c-by-njegq | \nclass Solution {\npublic:\n unordered_map<string, int> mp;\n vector<TreeNode*> result;\n string calc(TreeNode *root) {\n if (root == nullptr) | ajay_5097 | NORMAL | 2021-11-14T10:47:46.122468+00:00 | 2021-11-14T10:47:46.122509+00:00 | 214 | false | ```\nclass Solution {\npublic:\n unordered_map<string, int> mp;\n vector<TreeNode*> result;\n string calc(TreeNode *root) {\n if (root == nullptr) return to_string(\'#\');\n string node = "";\n string left = calc(root->left);\n string right = calc(root->right);\n node += left... | 4 | 0 | [] | 1 |
find-duplicate-subtrees | C++ || Great Question || Conceptual || | c-great-question-conceptual-by-kundankum-urne | \tclass Solution {\n\tpublic:\n vector res;\n vector findDuplicateSubtrees(TreeNode root) {\n unordered_map m;\n solve(root,m);\n ret | kundankumar4348 | NORMAL | 2021-10-21T05:25:47.696798+00:00 | 2021-10-21T05:25:47.696846+00:00 | 250 | false | \tclass Solution {\n\tpublic:\n vector<TreeNode*> res;\n vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {\n unordered_map<string,int> m;\n solve(root,m);\n return res;\n }\n \n string solve(TreeNode* root,unordered_map<string,int>& m){\n if(!root) return "x";\n ... | 4 | 0 | [] | 0 |
find-duplicate-subtrees | [Java] [O(N) time][beats 98.52%] Clean Code with Explanation + Merkle Tree | java-on-timebeats-9852-clean-code-with-e-qjy1 | To find if the duplicate exists or not in the subtree we need a good way of converting a sub-tree to some canonical format.\n\nThis canonical representation (i. | stb_satybald | NORMAL | 2021-09-26T12:52:01.494954+00:00 | 2021-09-26T15:04:28.894970+00:00 | 378 | false | To find if the duplicate exists or not in the subtree we need a good way of converting a sub-tree to some `canonical format`.\n\nThis canonical representation (i.e. which allows it to be identified in a unique way) can be a string representation of the \npost order traversal of sub-tree. For example, our unique represe... | 4 | 0 | [] | 0 |
find-duplicate-subtrees | [Python3] DFS with comments | python3-dfs-with-comments-by-ssshukla26-y2zf | \n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.lef | ssshukla26 | NORMAL | 2021-08-24T21:32:58.908469+00:00 | 2021-08-24T21:32:58.908500+00:00 | 673 | false | ```\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\n\nfrom collections import defaultdict\n\nclass Solution:\n \n def traverse(self, node: TreeNode, dups: Dict, su... | 4 | 0 | ['Depth-First Search', 'Python', 'Python3'] | 0 |
find-duplicate-subtrees | Java solution | Simple Logic using map | java-solution-simple-logic-using-map-by-vy2f8 | Approach- make a string of subtree element ,chekh if that string is already present in map,if not return a default value 0 ,it tells that the given subtree is n | kuldeepmishra007k | NORMAL | 2021-07-16T13:10:11.125483+00:00 | 2022-02-27T15:39:31.293298+00:00 | 469 | false | Approach- make a string of subtree element ,chekh if that string is already present in map,if not return a default value 0 ,it tells that the given subtree is not present in map.And further we insert that subtree in map with an increased value by 1.If we get a value equal to 1 , then the subtree has duplicate already a... | 4 | 0 | ['Java'] | 2 |
find-duplicate-subtrees | Clean and simple C++ | clean-and-simple-c-by-arpit-satnalika-dapy | \nunordered_map<string,int> hashmap;\nstring dfs(TreeNode *root,vector<TreeNode*> &v)\n{\n\tif(root==NULL)\n\t\treturn "#";\n\tstring left=dfs(root->left,v);\n\ | arpit-satnalika | NORMAL | 2020-12-21T14:04:45.398400+00:00 | 2020-12-21T14:04:45.398441+00:00 | 163 | false | ```\nunordered_map<string,int> hashmap;\nstring dfs(TreeNode *root,vector<TreeNode*> &v)\n{\n\tif(root==NULL)\n\t\treturn "#";\n\tstring left=dfs(root->left,v);\n\tstring right=dfs(root->right,v);\n\tstring str;\n\tstr=to_string(root->val)+","+left+ ","+right;\n\thashmap[str]++;\n\tif(hashmap[str]==2)\n\t\tv.push_back(... | 4 | 0 | [] | 0 |
find-duplicate-subtrees | 8 lines python solution beats 85% | 8-lines-python-solution-beats-85-by-luol-zdb2 | \'\'\'\nclass Solution:\n\n def findDuplicateSubtrees(self, root):\n dic=collections.defaultdict(list)\n def inorder(root):\n if not | luolingwei | NORMAL | 2019-06-27T12:48:15.359664+00:00 | 2019-06-27T12:48:15.359694+00:00 | 407 | false | \'\'\'\nclass Solution:\n\n def findDuplicateSubtrees(self, root):\n dic=collections.defaultdict(list)\n def inorder(root):\n if not root:return \'#\'\n strings=str(root.val)+inorder(root.left)+inorder(root.right)\n dic[strings].append(root)\n return strings\... | 4 | 0 | [] | 3 |
find-duplicate-subtrees | JavaScript Merkle tree - Based in Subtree of Another Tree | javascript-merkle-tree-based-in-subtree-tinbp | Based in https://leetcode.com/problems/subtree-of-another-tree\n\nSimilarly to Subtree of Another Tree (and @awice \'s answer), we generate the hash of every su | grs | NORMAL | 2018-11-13T11:45:58.804342+00:00 | 2018-11-13T11:45:58.804381+00:00 | 573 | false | Based in https://leetcode.com/problems/subtree-of-another-tree\n\nSimilarly to Subtree of Another Tree (and [@awice \'s answer](https://leetcode.com/problems/subtree-of-another-tree/discuss/102741/Python-Straightforward-with-Explanation-(O(ST)-and-O(S+T)-approaches))), we generate the hash of every subtree (known as Me... | 4 | 0 | [] | 0 |
find-duplicate-subtrees | Java | Post order traversal | 10 lines | java-post-order-traversal-10-lines-by-ju-whvh | 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 | judgementdey | NORMAL | 2023-02-28T21:50:53.101330+00:00 | 2023-03-22T21:36:01.974848+00:00 | 61 | 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 | ['Depth-First Search', 'Java'] | 0 |
find-duplicate-subtrees | 📌📌 C++ || DFS || Hashmap || Faster || Easy To Understand 🤷♂️🤷♂️ | c-dfs-hashmap-faster-easy-to-understand-8ol13 | Using DFS && Hashmap\n\n Time Complexity :- O(N)\n\n Space Complexity :- O(N)\n\n\nclass Solution {\npublic:\n \n // declare an unordered map\n \n u | __KR_SHANU_IITG | NORMAL | 2023-02-28T08:05:51.114345+00:00 | 2023-02-28T08:05:51.114390+00:00 | 532 | false | * ***Using DFS && Hashmap***\n\n* ***Time Complexity :- O(N)***\n\n* ***Space Complexity :- O(N)***\n\n```\nclass Solution {\npublic:\n \n // declare an unordered map\n \n unordered_map<string, int> mp;\n \n // declare a res array\n \n vector<TreeNode*> res;\n \n // function for finding du... | 3 | 0 | ['Tree', 'Depth-First Search', 'C', 'C++'] | 0 |
find-duplicate-subtrees | C# and TypeScript Solution . ❇️✅ | c-and-typescript-solution-by-arafatsabbi-nzfo | \u2B06\uFE0FLike|\uD83C\uDFAFShare|\u2B50Favourite\n# Approach\n1. Traverse the tree in postorder and get a unique identifier for each subtree.\n2. Use a dictio | arafatsabbir | NORMAL | 2023-02-28T06:12:38.239666+00:00 | 2023-02-28T06:12:38.239718+00:00 | 239 | false | # \u2B06\uFE0FLike|\uD83C\uDFAFShare|\u2B50Favourite\n# Approach\n1. Traverse the tree in postorder and get a unique identifier for each subtree.\n2. Use a dictionary to count the number of times each subtree occurs in the tree.\n3. Return all subtrees with more than one occurrence.\n\n# Runtime & Memory\n -> List[Optional[TreeNode]]:\n # create a dictionary to store subtrees\n subTrees = defaultdict(list)\n \n # define ... | 3 | 0 | ['Depth-First Search', 'Python', 'Python3'] | 0 |
find-duplicate-subtrees | python3 , HashMap + store duplicates with same height | python3-hashmap-store-duplicates-with-sa-kj9i | Intuition\nThe idea is to for duplicates subTrees to exist there will be nodes in tress with duplicates values. For two subtrees to be duplicate at two nodes wi | Code-IQ7 | NORMAL | 2023-02-28T04:37:40.326469+00:00 | 2023-02-28T04:37:40.326499+00:00 | 205 | false | # Intuition\nThe idea is to for duplicates subTrees to exist there will be nodes in tress with duplicates values. For two subtrees to be duplicate at two nodes will have -\n1. same heights\n2. same Tree structure\n\nWe need to take care of edge cases where two nodes might be at same heights but they might have differe... | 3 | 0 | ['Python3'] | 0 |
find-duplicate-subtrees | Find Duplicate Subtree || Easy to understand || beginner's Friendly😉🤗 | find-duplicate-subtree-easy-to-understan-e3yp | Intuition\nBy reading this qston first things which came to my mind was mapping.So, here is its implementation :\n\n# Approach\n- make a helper function ans pas | abhix2910 | NORMAL | 2023-02-28T04:09:08.689022+00:00 | 2023-02-28T04:09:08.689060+00:00 | 850 | false | # Intuition\nBy reading this qston first things which came to my mind was mapping.So, here is its implementation :\n\n# Approach\n- make a helper function ans pass the root and an answer vector.\n- first check the base case if root is null or not if yes then return a "#",\n- declare a string and inside that string keep... | 3 | 0 | ['Tree', 'Binary Tree', 'C++'] | 1 |
find-duplicate-subtrees | Java | java-by-akash2023-v05e | 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 | Akash2023 | NORMAL | 2023-02-28T03:54:35.995060+00:00 | 2023-02-28T03:54:35.995111+00:00 | 796 | 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 | ['Java'] | 0 |
find-duplicate-subtrees | ✅✅ C++|| Simplest Solution | c-simplest-solution-by-ashvani_gurjar-emb0 | \n# Complexity\n- Time complexity: O(N2)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(N)\n Add your space complexity here, e.g. O(n) \n\ | Ashvani_Gurjar | NORMAL | 2023-02-28T03:53:02.912843+00:00 | 2023-02-28T03:53:02.912898+00:00 | 453 | false | \n# Complexity\n- Time complexity: O(N2)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *... | 3 | 0 | ['C++'] | 1 |
find-duplicate-subtrees | Simple solution using postorder traversal. | simple-solution-using-postorder-traversa-tifu | \n\n# Complexity\n- Time complexity:O(N^2)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity:O(N)\n Add your space complexity here, e.g. O(n) \n | ShivamDubey55555 | NORMAL | 2023-02-28T03:39:52.229691+00:00 | 2023-02-28T03:39:52.229780+00:00 | 301 | false | \n\n# Complexity\n- Time complexity:$$O(N^2)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(N)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * T... | 3 | 0 | ['C++'] | 1 |
find-duplicate-subtrees | C++ Solution || >90% Runtime and Memory | c-solution-90-runtime-and-memory-by-0nea-xuy2 | \n# Code\n\nclass Solution {\npublic:\n unordered_map<string, pair<int, int>> mp;\n int id;\n vector<TreeNode*> res;\n vector<TreeNode*> findDuplica | 0neAnd0nlyBOI | NORMAL | 2023-02-28T02:28:02.003778+00:00 | 2023-02-28T02:28:02.003812+00:00 | 645 | false | \n# Code\n```\nclass Solution {\npublic:\n unordered_map<string, pair<int, int>> mp;\n int id;\n vector<TreeNode*> res;\n vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {\n id = 1;\n postOrder(root);\n return res;\n }\n\n string postOrder(TreeNode* root)\n {\n i... | 3 | 0 | ['C++'] | 1 |
find-duplicate-subtrees | HashMap & PostorderTraversal | hashmap-postordertraversal-by-brijesh20m-zgko | \n# Code\n\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * int val;\n * TreeNode left;\n * TreeNode right;\n * TreeN | brijesh20M | NORMAL | 2023-02-28T02:10:32.526608+00:00 | 2023-02-28T02:10:32.526652+00:00 | 132 | false | \n# Code\n```\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * int val;\n * TreeNode left;\n * TreeNode right;\n * TreeNode() {}\n * TreeNode(int val) { this.val = val; }\n * TreeNode(int val, TreeNode left, TreeNode right) {\n * this.val = val;\n * thi... | 3 | 0 | ['Java'] | 1 |
find-duplicate-subtrees | Swift | Set | swift-set-by-upvotethispls-nxzi | Set (accepted answer)\n\nclass Solution {\n func findDuplicateSubtrees(_ root: TreeNode?) -> [TreeNode?] {\n var set = Set<Int>()\n var result | UpvoteThisPls | NORMAL | 2023-02-28T00:45:59.961833+00:00 | 2024-04-16T18:45:00.051095+00:00 | 107 | false | **Set (accepted answer)**\n```\nclass Solution {\n func findDuplicateSubtrees(_ root: TreeNode?) -> [TreeNode?] {\n var set = Set<Int>()\n var result = [TreeNode?]()\n \n func dfs(_ node: TreeNode?) -> Int {\n guard let node else { return 0 }\n let hashValue = [node.... | 3 | 0 | ['Swift'] | 1 |
find-duplicate-subtrees | The simplest C# solution with Stack and Postorder traversal | the-simplest-c-solution-with-stack-and-p-zqgv | Approach\nPostorder traversal.\n\n# Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(n)\n\n# Code\n\npublic class Solution {\n public IList<TreeN | dyfilatov | NORMAL | 2022-12-12T15:09:22.686226+00:00 | 2022-12-12T15:10:30.456483+00:00 | 212 | false | # Approach\nPostorder traversal.\n\n# Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(n)\n\n# Code\n```\npublic class Solution {\n public IList<TreeNode> FindDuplicateSubtrees(TreeNode root) {\n var stack = new Stack<TreeNode>();\n var visited = new Dictionary<TreeNode, string>();\n ... | 3 | 0 | ['Stack', 'C#'] | 0 |
find-duplicate-subtrees | Python O(N) using hashmap simply and easy | python-on-using-hashmap-simply-and-easy-u9ebv | Similar to LC297 preorder serialization binary tree, recursively serialize every node, using a hashmap to record every serialized string, if there is duplicat | JessicaWestside | NORMAL | 2022-07-04T01:39:00.184459+00:00 | 2022-07-04T01:39:00.184502+00:00 | 355 | false | Similar to LC297 `preorder serialization binary tree`, recursively serialize every node, using a hashmap to record every serialized string, if there is duplicate, record it.\nTime Complexity O(n)\n```\nclass Solution:\n def findDuplicateSubtrees(self, root: TreeNode) -> List[TreeNode]:\n res = []\n m... | 3 | 0 | [] | 0 |
find-duplicate-subtrees | C++ | Clean And Easy Solution | c-clean-and-easy-solution-by-jayesh2604-2o18 | \nclass Solution\n{\nprivate:\n map<string, vector<TreeNode *>> mp;\n string postOrder(TreeNode *root)\n {\n if (!root)\n {\n | jayesh2604 | NORMAL | 2022-05-28T15:26:31.123177+00:00 | 2022-05-28T15:26:31.123216+00:00 | 432 | false | ```\nclass Solution\n{\nprivate:\n map<string, vector<TreeNode *>> mp;\n string postOrder(TreeNode *root)\n {\n if (!root)\n {\n return "";\n }\n string leftAns = postOrder(root->left);\n string rightAns = postOrder(root->right);\n string currentString = "L"... | 3 | 0 | ['C++'] | 0 |
find-duplicate-subtrees | Python | Hashmap | DFS | Simple Solution | python-hashmap-dfs-simple-solution-by-ak-578t | \n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.lef | akash3anup | NORMAL | 2022-05-21T20:43:39.203635+00:00 | 2022-05-21T20:43:39.203676+00:00 | 516 | false | ```\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nclass Solution:\n def dfs(self, root, lookup, duplicates):\n if not root:\n return \'.\'\n ... | 3 | 0 | ['Depth-First Search', 'Python'] | 0 |
find-duplicate-subtrees | c++ easy solution with Hashmap. | c-easy-solution-with-hashmap-by-prashant-95sa | \nclass Solution {\npublic:\n vector<TreeNode*> d;\n unordered_map<string, int> mp;\n \n string postorder(TreeNode* root) {\n if(!root) retur | Prashant830 | NORMAL | 2022-04-25T07:52:42.675337+00:00 | 2022-04-25T07:52:42.675366+00:00 | 182 | false | ```\nclass Solution {\npublic:\n vector<TreeNode*> d;\n unordered_map<string, int> mp;\n \n string postorder(TreeNode* root) {\n if(!root) return "";\n \n string left = postorder(root->left);\n string right = postorder(root->right);\n \n string current = to_string(r... | 3 | 0 | [] | 0 |
find-duplicate-subtrees | C++ DFS string and hashmap | c-dfs-string-and-hashmap-by-adbnemesis-9imt | The main idea is to store the tree as a string and keep incrementing it.\n\n```\nclass Solution {\n \npublic:\n \n string help(TreeNode root, unordered | adbnemesis | NORMAL | 2022-02-22T10:48:51.106515+00:00 | 2022-02-22T10:48:51.106546+00:00 | 182 | false | The main idea is to store the tree as a string and keep incrementing it.\n\n```\nclass Solution {\n \npublic:\n \n string help(TreeNode* root, unordered_map<string, int>& um,vector<TreeNode*>& ans){\n if(root == NULL){\n return " ";\n };\n string s = to_string(root->val) + " "+ ... | 3 | 0 | [] | 1 |
find-duplicate-subtrees | Easy HashMap based solution | easy-hashmap-based-solution-by-pratosh-1r95 | \nclass Solution {\n List<TreeNode> res = new ArrayList<>();\n HashMap<String, Integer> map = new HashMap<>();\n \n public List<TreeNode> findDuplic | pratosh | NORMAL | 2022-02-10T15:02:50.327787+00:00 | 2022-02-10T15:10:28.870207+00:00 | 491 | false | ```\nclass Solution {\n List<TreeNode> res = new ArrayList<>();\n HashMap<String, Integer> map = new HashMap<>();\n \n public List<TreeNode> findDuplicateSubtrees(TreeNode root) {\n find(root);\n return res;\n }\n \n String find(TreeNode root){\n if(root==null) return "";\n ... | 3 | 0 | ['Java'] | 0 |
find-duplicate-subtrees | JAVA || EASY SOLUTION || HASHMAP || FAST | java-easy-solution-hashmap-fast-by-anike-3xhg | \n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * int val;\n * TreeNode left;\n * TreeNode right;\n * TreeNode() {}\n | aniket7419 | NORMAL | 2022-02-03T18:09:46.426265+00:00 | 2022-02-03T18:09:46.426408+00:00 | 107 | false | ```\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * int val;\n * TreeNode left;\n * TreeNode right;\n * TreeNode() {}\n * TreeNode(int val) { this.val = val; }\n * TreeNode(int val, TreeNode left, TreeNode right) {\n * this.val = val;\n * this.left = l... | 3 | 1 | [] | 0 |
find-duplicate-subtrees | Simpe postorder recursive Java solution | simpe-postorder-recursive-java-solution-25sho | \nclass Solution {\n public List<TreeNode> findDuplicateSubtrees(TreeNode root) {\n Map<String, Integer> map = new HashMap<>();\n List<TreeNode | KorabeL | NORMAL | 2021-12-08T13:15:39.780869+00:00 | 2021-12-08T13:15:39.780909+00:00 | 230 | false | ```\nclass Solution {\n public List<TreeNode> findDuplicateSubtrees(TreeNode root) {\n Map<String, Integer> map = new HashMap<>();\n List<TreeNode> list = new LinkedList<>();\n helper(root, map, list);\n return list;\n }\n\n private String helper(TreeNode root, Map<String, Integer> ... | 3 | 0 | [] | 0 |
find-duplicate-subtrees | python dfs | python-dfs-by-gasohel336-xjga | ```\nfrom typing import List\nfrom collections import defaultdict\nclass TreeNode:\n def init(self, val=0, left=None, right=None):\n self.val = val\n | gasohel336 | NORMAL | 2021-07-30T14:10:20.489891+00:00 | 2021-07-30T14:10:20.489944+00:00 | 228 | false | ```\nfrom typing import List\nfrom collections import defaultdict\nclass TreeNode:\n def __init__(self, val=0, left=None, right=None):\n self.val = val\n self.left = left\n self.right = right\n\nclass Solution:\n def findDuplicateSubtrees(self, root: TreeNode) -> List[TreeNode]:\n\n se... | 3 | 0 | [] | 0 |
find-duplicate-subtrees | Simple C++ solution using unordered_map | simple-c-solution-using-unordered_map-by-tqju | \nclass Solution {\npublic:\n unordered_map<string,int> mp;\n vector<TreeNode*> ans;\n \n string dfs(TreeNode* root)\n {\n if(root==NULL)\ | Amey_Joshi | NORMAL | 2021-07-16T04:10:10.173088+00:00 | 2021-07-16T04:10:10.173148+00:00 | 165 | false | ```\nclass Solution {\npublic:\n unordered_map<string,int> mp;\n vector<TreeNode*> ans;\n \n string dfs(TreeNode* root)\n {\n if(root==NULL)\n return "#";\n \n string left=dfs(root->left);\n string right=dfs(root->right);\n \n string key=left+" "+right... | 3 | 0 | [] | 0 |
find-duplicate-subtrees | C++ solution using string and hashmap | c-solution-using-string-and-hashmap-by-m-7l67 | Create a hashmap storing vector of TreeNodes with a string as a key. The string can uniquely identify the subtree because we are using "(" and ")" values to mar | maitreya47 | NORMAL | 2021-06-22T00:02:35.407797+00:00 | 2021-06-22T00:17:43.802202+00:00 | 508 | false | Create a hashmap storing vector of TreeNodes with a string as a key. The string can uniquely identify the subtree because we are using "(" and ")" values to mark each left and right subtree recursively , so even if the node values are same, the structure is checked if and only if the parans too are same.\nFor example: ... | 3 | 0 | ['String', 'C', 'C++'] | 1 |
find-duplicate-subtrees | Sumple C++ soln | Hashing | Serialization | sumple-c-soln-hashing-serialization-by-s-r5u0 | \n string helper(TreeNode* root,vector<TreeNode*> &ans, unordered_map<string, int>&m ){\n if(!root)\n return "# ";\n string node=to_ | sneha_pandey_ | NORMAL | 2021-05-17T13:54:30.594042+00:00 | 2021-05-17T13:54:30.594081+00:00 | 346 | false | ```\n string helper(TreeNode* root,vector<TreeNode*> &ans, unordered_map<string, int>&m ){\n if(!root)\n return "# ";\n string node=to_string(root->val)+" ";\n node+=helper(root->left, ans,m);\n node+=helper(root->right, ans,m);\n if(m.count(node) && m[node]==1){\n ... | 3 | 0 | ['C'] | 1 |
find-duplicate-subtrees | Java || Tree + Hashing || 2 solutions || 4ms || beats 99% in both time and memory | java-tree-hashing-2-solutions-4ms-beats-70maw | \n\t// O(n^2) O(n)\n\tpublic List findDuplicateSubtrees1(TreeNode root) {\n\n\t\tList ans = new ArrayList();\n\t\tHashMap map = new HashMap<>();\n\t\tfindDuplic | LegendaryCoder | NORMAL | 2021-05-16T14:31:27.119777+00:00 | 2021-05-16T14:31:27.119810+00:00 | 332 | false | \n\t// O(n^2) O(n)\n\tpublic List<TreeNode> findDuplicateSubtrees1(TreeNode root) {\n\n\t\tList<TreeNode> ans = new ArrayList<TreeNode>();\n\t\tHashMap<String, Integer> map = new HashMap<>();\n\t\tfindDuplicateSubtrees1(root, ans, map);\n\t\treturn ans;\n\t}\n\n\t// O(n^2) O(n)\n\tpublic StringBuilder findDuplicateSubt... | 3 | 0 | [] | 0 |
find-duplicate-subtrees | Java, dfs with serializing and stringbuilder beats 98.7% | java-dfs-with-serializing-and-stringbuil-h3j5 | ```\npublic List findDuplicateSubtrees(TreeNode root) {\n List ans = new ArrayList<>();\n if (root == null) {\n return ans;\n }\ | amethystave | NORMAL | 2021-02-24T21:59:57.692169+00:00 | 2021-02-24T21:59:57.692223+00:00 | 444 | false | ```\npublic List<TreeNode> findDuplicateSubtrees(TreeNode root) {\n List<TreeNode> ans = new ArrayList<>();\n if (root == null) {\n return ans;\n }\n \n // serial to id map\n Map<String, Integer> serialToId = new HashMap<>();\n // id to count map\n Map<... | 3 | 0 | ['Java'] | 0 |
find-duplicate-subtrees | Time & Space Beating 99%. The Reasoning behind Hashing: Rabin Karp | time-space-beating-99-the-reasoning-behi-1rql | This problem is quite simple and straightforward indeed. We are to find the "equal" binary trees among all the subtrees.\n\nFirst, how do we define an "equality | maristie | NORMAL | 2021-01-13T13:16:30.281154+00:00 | 2021-01-13T15:23:18.836325+00:00 | 207 | false | This problem is quite simple and straightforward indeed. We are to find the "equal" binary trees among all the subtrees.\n\nFirst, how do we define an "equality" between two binary trees? It can be defined recursively: two trees are equal if and only if their root node has the same value and their respective left and r... | 3 | 0 | [] | 1 |
find-duplicate-subtrees | Java / HashMap / recursive | java-hashmap-recursive-by-zc-3618e4cb-aj8g | java\nclass Solution {\n Map<String, Integer> map = new HashMap<>();\n List<TreeNode> res = new ArrayList<>();\n public List<TreeNode> findDuplicateSub | zc-3618e4cb | NORMAL | 2020-10-17T07:50:59.187849+00:00 | 2020-10-17T07:50:59.187884+00:00 | 265 | false | ```java\nclass Solution {\n Map<String, Integer> map = new HashMap<>();\n List<TreeNode> res = new ArrayList<>();\n public List<TreeNode> findDuplicateSubtrees(TreeNode root) {\n traverse(root);\n return res;\n }\n \n private String traverse(TreeNode node) {\n if (node == null) {\... | 3 | 0 | ['Recursion'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | O(n logn) | Detailed Explanation | on-logn-detailed-explanation-by-khaufnak-50xq | A hard question = Lots of observation + data structures \n\nLet\'s figure out observations and then find out data structure to implement them.\n\n>Observation 0 | khaufnak | NORMAL | 2020-07-05T07:21:49.295543+00:00 | 2020-08-18T17:09:11.550068+00:00 | 10,670 | false | A hard question = Lots of observation + data structures \n\nLet\'s figure out observations and then find out data structure to implement them.\n\n>Observation 0: Well, we know that if we could get smallest digit to the left, then we will be able to make number smaller than we currently have. In that sense, a sorted num... | 307 | 3 | ['Java'] | 20 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | The constraint was not very helpful... [C++/Python] Clean 56ms O(n2) solution | the-constraint-was-not-very-helpful-cpyt-yays | 1 <= num.length <= 30000\n1 <= k <= 10^9\n\nGiven these two constraints, how could leetcode expect someone to know that O(n2) and O(n2logn) would pass the OJ?\n | lichuan199010 | NORMAL | 2020-07-05T04:15:42.175031+00:00 | 2020-07-07T22:24:42.355787+00:00 | 5,774 | false | 1 <= num.length <= 30000\n1 <= k <= 10^9\n\nGiven these two constraints, how could leetcode expect someone to know that O(n2) and O(n2logn) would pass the OJ?\nIt was not hard to figure out the bubble sort as a naive solution (pick the smallest possible number and move to the front) and think about fast return with sor... | 97 | 0 | [] | 12 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | [Python] 17 lines O(nlogn) solution | python-17-lines-onlogn-solution-by-qqwqe-uf7i | The idea is quite straightforward: \nIn each round, pick the smallest number within k distance and move it to the front.\nFor each number, we save its indexes i | qqwqert007-2 | NORMAL | 2020-07-05T08:13:14.048747+00:00 | 2020-07-05T08:15:34.291457+00:00 | 3,192 | false | The idea is quite straightforward: \n`In each round, pick the smallest number within k distance and move it to the front.`\nFor each number, we save its indexes in a deque.\nIn each round, we check from 0 to 9 to see if the nearest index is within k distance.\nBut here comes the tricky part:\n`The index of a number may... | 38 | 0 | [] | 8 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | [Python] bytedance interview question | python-bytedance-interview-question-by-y-ftql | Idea\nGreedily select the smallest number we can reach, and push it all the way to the front.\n\nComplexity\n- time: O(n^2)\n- space: O(n)\n\nPython\n\nclass So | yanrucheng | NORMAL | 2020-07-05T04:03:28.080097+00:00 | 2020-07-05T04:03:28.080165+00:00 | 5,500 | false | **Idea**\nGreedily select the smallest number we can reach, and push it all the way to the front.\n\n**Complexity**\n- time: O(n^2)\n- space: O(n)\n\n**Python**\n```\nclass Solution:\n def minInteger(self, num: str, k: int) -> str:\n num = [*map(int, num)]\n if k >= (len(num) ** 2) // 2:\n r... | 37 | 18 | [] | 8 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Java Bubblesort 16 lines with detailed explanation easy to understand | java-bubblesort-16-lines-with-detailed-e-sk5n | \n\n\nclass Solution {\n public String minInteger(String num, int k) {\n char[] ca = num.toCharArray();\n helper(ca, 0, k);\n return new | funbam | NORMAL | 2020-07-05T04:01:11.746599+00:00 | 2020-07-05T05:37:43.624050+00:00 | 3,177 | false | \n\n```\nclass Solution {\n public String minInteger(String num, int k) {\n char[] ca = num.toCharArray();\n helper(ca, 0, k);\n return new String(ca);\n }\n \n public void help... | 30 | 5 | [] | 9 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | [C++] O(N log N) Fenwick/BIT solution 196ms (with explanation) | c-on-log-n-fenwickbit-solution-196ms-wit-wcj1 | \nclass Solution {\n vector<pair<int, int>> resort;\n \n priority_queue<int, vector<int>, greater<int>> nums[10];\n int used[30001];\n int n;\n | jt3698 | NORMAL | 2020-07-05T04:20:59.986562+00:00 | 2020-07-05T04:58:41.747875+00:00 | 3,176 | false | ```\nclass Solution {\n vector<pair<int, int>> resort;\n \n priority_queue<int, vector<int>, greater<int>> nums[10];\n int used[30001];\n int n;\n int getSum(int index) {\n int sum = 0;\n while (index > 0) { \n sum += used[index];\n index -= index & (-index); \n ... | 23 | 4 | [] | 4 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Largest Number Possible After At Most K Swaps | largest-number-possible-after-at-most-k-j0mum | Hi guys, a solution to this problem is provided here in the link.\nSolution Link:-https://www.pepcoding.com/resources/data-structures-and-algorithms-in-java-lev | sanjeevpep1coding | NORMAL | 2020-09-04T11:24:28.001160+00:00 | 2020-09-04T11:24:28.001194+00:00 | 12,925 | false | Hi guys, a solution to this problem is provided here in the link.\nSolution Link:-https://www.pepcoding.com/resources/data-structures-and-algorithms-in-java-levelup/recursion-and-backtracking/largest-number-at-most-k-swaps-official/ojquestion\nFree resources Link:-https://www.pepcoding.com/resources/online-java-foundat... | 16 | 24 | [] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | A simple C++ O(n) solution - 100ms | a-simple-c-on-solution-100ms-by-user2009-dy4h | \nclass Solution {\npublic:\n string minInteger(string num, int k) {\n const int n = num.size();\n string res;\n res.reserve(n);\n | user2009j | NORMAL | 2020-07-07T01:30:11.199031+00:00 | 2020-07-07T01:30:11.199140+00:00 | 2,416 | false | ```\nclass Solution {\npublic:\n string minInteger(string num, int k) {\n const int n = num.size();\n string res;\n res.reserve(n);\n vector<int> q(10, n);\n for (int i = 0; i < n; ++i) {\n const int d = num[i] - \'0\';\n if (q[d] == n)\n q[d] =... | 16 | 2 | [] | 4 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Detailed C++ Segment Tree Solution (88ms, might not be optimal) | detailed-c-segment-tree-solution-88ms-mi-uzin | Here is my first post on leetcode. I would like to share my segment tree solution to this problem. My time complexity is O(nlognlogn) so it might not be the opt | yao_yin | NORMAL | 2020-07-05T06:45:17.421302+00:00 | 2020-07-05T18:39:32.619430+00:00 | 1,643 | false | Here is my first post on leetcode. I would like to share my segment tree solution to this problem. My time complexity is **O(nlognlogn)** so it might not be the optimal solution.\n\nWell, let us analyze this problem. If we want to construct the mininal number, we need to force the leftmost digit as small as possible. S... | 15 | 0 | [] | 4 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | [Java] 27ms O(NlogN) Fenwick/BIT solution | java-27ms-onlogn-fenwickbit-solution-by-4wszg | Had the rough idea during the contest but got stuck at how to update INDEX using BIT. Checked @tian-tang-6\'s answer and this tutorial: https://www.hackerearth. | caohuicn | NORMAL | 2020-07-05T07:25:44.599779+00:00 | 2020-07-05T07:25:44.599826+00:00 | 957 | false | Had the rough idea during the contest but got stuck at how to update INDEX using BIT. Checked @tian-tang-6\'s answer and this tutorial: https://www.hackerearth.com/practice/data-structures/advanced-data-structures/fenwick-binary-indexed-trees/tutorial/, realized that instead of updating INDEX, we should: \n1. Initializ... | 10 | 0 | ['Binary Indexed Tree'] | 1 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | My screencast | my-screencast-by-cuiaoxiang-gyt8 | https://www.youtube.com/watch?v=1wjYXKeGtOc | cuiaoxiang | NORMAL | 2020-07-05T04:37:36.984462+00:00 | 2020-07-05T04:37:36.984516+00:00 | 925 | false | https://www.youtube.com/watch?v=1wjYXKeGtOc | 10 | 1 | [] | 2 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | C++ Preprocess (120 ms) | c-preprocess-120-ms-by-votrubac-f4ig | The first function is just a greedy brute-force algorithm that checks for the minimal character within K, and bubbles it up.\n\nIt\'s O(n * n) so you will get T | votrubac | NORMAL | 2020-07-05T05:23:52.961720+00:00 | 2020-07-05T12:00:31.027165+00:00 | 2,010 | false | The first function is just a greedy brute-force algorithm that checks for the minimal character within K, and bubbles it up.\n\nIt\'s O(n * n) so you will get TLE. I saw folks solved it using logarithmic structures, but all I got is this silly idea.\n\nI think that we first need to reduce K. Obviously, if K is large, w... | 9 | 2 | [] | 3 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | C++ | O(NlogN) | Binary Search with logic | c-onlogn-binary-search-with-logic-by-agr-t0q6 | \n```\n/ \nWe take 10 queues for digits 0 to 9 and store their occurence(index in the original string) in the queues (clearly all the queues are sorted by defau | agrims | NORMAL | 2020-09-03T12:00:08.086061+00:00 | 2020-09-03T12:01:45.392230+00:00 | 1,466 | false | \n```\n/** <Greedy Approach>\nWe take 10 queues for digits 0 to 9 and store their occurence(index in the original string) in the queues (clearly all the queues are sorted by default) \nNow we want the most significant digit to be the smallest so we try all the 0-9 digits at position i .\nSuppose we want to place any di... | 6 | 0 | [] | 2 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Beats 100% on memory [EXPLAINED] | beats-100-on-memory-explained-by-r9n-hozw | Intuition\nThe problem involves rearranging digits of a number to form the smallest possible integer by making at most k adjacent swaps.\n\n# Approach\nThe appr | r9n | NORMAL | 2024-09-26T18:38:04.455133+00:00 | 2024-09-26T18:38:04.455162+00:00 | 35 | false | # Intuition\nThe problem involves rearranging digits of a number to form the smallest possible integer by making at most k adjacent swaps.\n\n# Approach\nThe approach is to iteratively find the smallest digit within the range allowed by k, bring it to the current position, and repeat this process until k is exhausted o... | 5 | 0 | ['C#'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Python. Straight Forward Short Greedy Algorithm. | python-straight-forward-short-greedy-alg-0sq3 | A greedy approach will do the job.\nIterate the index i from the left to right. Each iteration do a swap in a greedy way, i.e. swap num[i] with the smallest num | caitao | NORMAL | 2020-07-05T19:05:24.560186+00:00 | 2020-07-05T19:06:36.165209+00:00 | 664 | false | A greedy approach will do the job.\nIterate the index i from the left to right. Each iteration do a swap in a greedy way, i.e. swap num[i] with the smallest number within num[i+1:]\n\n```\nclass Solution:\n \'\'\' greedy approach. start from the left side, \n\t and everytime pick the smallest number on the right ... | 5 | 0 | [] | 2 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | [Java][NlogN][Binary Tree] | javanlognbinary-tree-by-mayank12559-afl0 | Store indexes of all digits in a list.\n Start putting from 0 to 9 whichever satisfy the condition first.\n Condition is satisfied if number of swaps remaining | mayank12559 | NORMAL | 2020-07-05T04:18:37.992710+00:00 | 2020-07-05T04:34:21.390867+00:00 | 1,023 | false | * Store indexes of all digits in a list.\n* Start putting from 0 to 9 whichever satisfy the condition first.\n* Condition is satisfied if number of swaps remaining (k) >= number of swaps required (val - count) => `k >= val - count`\n* `val = index of the digit`\n* ` count = number of elements used before that index`\n*... | 5 | 1 | [] | 1 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Java TreeMap headMap().size() Time complexity is not O(1) | java-treemap-headmapsize-time-complexity-18q2 | I was surprised that no one consider using treemap at first, but found out I didn\'t quite understand the time complexity of treemap api.\nImplemente the idea o | wooohs | NORMAL | 2020-10-22T23:25:33.822721+00:00 | 2020-10-22T23:27:24.326711+00:00 | 906 | false | I was surprised that no one consider using treemap at first, but found out I didn\'t quite understand the time complexity of treemap api.\nImplemente the idea of this [post](https://leetcode.com/problems/minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits/discuss/720548/O(n-logn)-or-Detailed-Explanation) ... | 4 | 0 | [] | 1 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | EASY CPP || SLIDING WINDOW | easy-cpp-sliding-window-by-beenbuggy-7k7x | \n\n# Code\n\nclass Solution {\npublic:\n string minInteger(string a, int k) {\n int n=a.length();\n int req=(n+1)*n/2; // upper limit fo | SprihaAnand | NORMAL | 2023-08-06T08:59:10.582913+00:00 | 2023-08-06T08:59:10.582937+00:00 | 603 | false | \n\n# Code\n```\nclass Solution {\npublic:\n string minInteger(string a, int k) {\n int n=a.length();\n int req=(n+1)*n/2; // upper limit for the number of swaps that is also less than 10^9 lowering our time complexity\n if(k>req){ // that means we can try all swaps so r... | 3 | 0 | ['C++'] | 2 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | [python] 3 solutions with thought process when being asked during interviews | python-3-solutions-with-thought-process-ax73f | Binary Search\nIn order to construct the minimum number, we want to find the minimum digit within the window of size of k, which determine the range of digits t | jandk | NORMAL | 2021-08-07T16:34:28.434083+00:00 | 2021-08-08T02:04:32.251058+00:00 | 471 | false | ### Binary Search\nIn order to construct the minimum number, we want to find the minimum digit within the window of size of *k*, which determine the range of digits that can be moved.\nThe naive solution I came up first is to find out the minimum digit adhoc, which takes many unnecessary comparision. Because the digit ... | 3 | 0 | [] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Java super easy Insertion Sort O(N^2) w/ explanation | java-super-easy-insertion-sort-on2-w-exp-aupf | In the window of length k, the optimal solution is when the smallest integer in the window is occupying the MSB. So, for each position, find the minimum element | varkey98 | NORMAL | 2020-08-27T18:38:36.370419+00:00 | 2020-08-27T19:01:32.121652+00:00 | 635 | false | In the window of length k, the optimal solution is when the smallest integer in the window is occupying the MSB. So, for each position, find the minimum element in that window, perform the swaps and update the MSB.\n```\npublic String minInteger(String num, int k) \n{\n\tchar[] arr=num.toCharArray();\n\tint n=arr.lengt... | 3 | 0 | ['Java'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | [Python] O(n) 10 deques with 1 stack | python-on-10-deques-with-1-stack-by-yupi-n14f | Idea\n\nThe first digit of the output should be the minimum of the first k + 1 digits in num, i.e., min(num[:k+1]). Similarly, the i-th digit of the output shou | yupingso | NORMAL | 2020-07-14T09:00:57.395980+00:00 | 2020-07-15T10:02:21.394370+00:00 | 427 | false | # Idea\n\nThe first digit of the output should be the minimum of the first `k + 1` digits in `num`, i.e., `min(num[:k+1])`. Similarly, the `i`-th digit of the output should be `min(num[i:i+k+1])`, where we assume\n* The digits of `num` are swapped in-place. That is, `num[:i]` are already the first `i` digits of the out... | 3 | 0 | [] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | [Python] O(N*log(N)) BinarySearch solution | python-onlogn-binarysearch-solution-by-k-vg8s | In this post I will focus on stepping through my thought process in solving this question and only post the full solution at the end. So a word of warning: long | k0dl3 | NORMAL | 2020-07-11T08:20:07.677510+00:00 | 2020-07-11T08:20:07.677556+00:00 | 277 | false | In this post I will focus on stepping through my thought process in solving this question and only post the full solution at the end. So a word of warning: **long post**. But hopefully some may find this useful :) You can also practice implementing after reading analysis instead of refering to the full solution.\n\n**I... | 3 | 1 | [] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | C++ easy solution | c-easy-solution-by-klimkina-h4iu | \nclass Solution {\npublic:\n void helper(char *array, int k, int start, int n){\n if (k == 0 or start >= n) // no moves left, done modifying the stri | klimkina | NORMAL | 2020-07-05T15:28:12.449918+00:00 | 2020-07-05T15:28:12.449943+00:00 | 398 | false | ```\nclass Solution {\npublic:\n void helper(char *array, int k, int start, int n){\n if (k == 0 or start >= n) // no moves left, done modifying the string\n return; \n int min_idx = start; // position of the closest min digit within k\n for(int i = start+1; i < min(n, start + k... | 3 | 1 | [] | 1 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | [Java] O(n^2) Easy to understand | java-on2-easy-to-understand-by-marvinbai-6g6k | \nclass Solution {\n public String minInteger(String num, int k) {\n int n = num.length();\n int[] nums = new int[n];\n for(int i = 0; i | marvinbai | NORMAL | 2020-07-05T09:31:34.273150+00:00 | 2020-07-05T11:26:39.729900+00:00 | 208 | false | ```\nclass Solution {\n public String minInteger(String num, int k) {\n int n = num.length();\n int[] nums = new int[n];\n for(int i = 0; i < n; i++) nums[i] = Character.getNumericValue(num.charAt(i));\n helper(nums, k);\n StringBuilder sb = new StringBuilder();\n for(int i ... | 3 | 0 | [] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | [C++] O(nlogn) solution using segment tree | c-onlogn-solution-using-segment-tree-by-60l9g | I applied the greedy approach where I will move the smallest possible digit to the leftmost unfixed position. Instead of updating all the ununsed indices to the | svashish | NORMAL | 2020-07-05T07:36:15.991854+00:00 | 2020-07-05T07:36:15.991888+00:00 | 212 | false | I applied the greedy approach where I will move the smallest possible digit to the leftmost unfixed position. Instead of updating all the ununsed indices to the left, I used a segement tree to find the number of digits on the right side of each digit which crossed over the current digit and go left. This brought down t... | 3 | 0 | [] | 1 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | [Javascript] specific bubble sorting problem with limited steps k | javascript-specific-bubble-sorting-probl-e7cq | \n/**\n * @param {string} num\n * @param {number} k\n * @return {string}\n \n Idea is based on bubble sorting with limited steps k\n \n */\n\nvar minInteger = f | alanchanghsnu | NORMAL | 2020-07-05T04:06:27.364744+00:00 | 2020-07-05T04:06:27.364793+00:00 | 295 | false | ```\n/**\n * @param {string} num\n * @param {number} k\n * @return {string}\n \n Idea is based on bubble sorting with limited steps k\n \n */\n\nvar minInteger = function(num, k) {\n if (num.length == 1)\n return num;\n \n let nums = num.split(\'\');\n let i = 0, j = 0;\n \n while (k && i < num... | 3 | 0 | ['JavaScript'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Concise JavaScript solution | concise-javascript-solution-by-seriously-40dv | \nvar minInteger = function(num, k) {\n var arr = num.split("");\n var n = arr.length;\n\t\tfor (var i = 0; i < n-1 && k > 0; ++i) \n {\n | seriously_ridhi | NORMAL | 2020-07-05T04:05:33.344264+00:00 | 2020-07-05T04:11:59.038490+00:00 | 297 | false | ```\nvar minInteger = function(num, k) {\n var arr = num.split("");\n var n = arr.length;\n\t\tfor (var i = 0; i < n-1 && k > 0; ++i) \n {\n var pos = i; \n for (var j = i+1; j < n ; ++j) \n {\n if (j - i > k) \n break; \n \n ... | 3 | 0 | ['JavaScript'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | [Python3] brute force | python3-brute-force-by-ye15-juve | Algo\nScan through the string. At each index, look for the smallest element behind it within k swaps. Upon finding such minimum, swap it to replece the current | ye15 | NORMAL | 2020-07-05T04:03:11.567689+00:00 | 2020-07-05T04:10:57.467902+00:00 | 688 | false | Algo\nScan through the string. At each index, look for the smallest element behind it within k swaps. Upon finding such minimum, swap it to replece the current element, and reduce k to reflect the swaps. Do this for all element until k becomes 0. \n\n`O(N^2)` time & `O(N)` space \n```\nclass Solution:\n def minInteg... | 3 | 1 | ['Python3'] | 2 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | c++ | easy | segment tree | c-easy-segment-tree-by-venomhighs7-cuki | \n# Code\n\ntypedef long long ll;\nconst int N =30010;\nll w[N];\nll st[N];\n\nstruct Node {\n ll l,r;\n ll minv, idx, sum;\n} tree[4*N];\n\nvoid push_up( | venomhighs7 | NORMAL | 2022-10-11T04:11:08.079523+00:00 | 2022-10-11T04:11:08.079564+00:00 | 1,273 | false | \n# Code\n```\ntypedef long long ll;\nconst int N =30010;\nll w[N];\nll st[N];\n\nstruct Node {\n ll l,r;\n ll minv, idx, sum;\n} tree[4*N];\n\nvoid push_up(Node& a, Node& b, Node & c) {\n if(b.minv <= c.minv) {\n a.minv = b.minv;\n a.idx = b.idx;\n } else {\n a.minv = c.minv;\n ... | 2 | 0 | ['C++'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Java | Simple implementation | Detailed explanation | java-simple-implementation-detailed-expl-7ux4 | \nclass Solution {\n public static String minInteger(String num, int k) {\n char[] array = num.toCharArray(); // returns a newly allocated character a | BoiMax | NORMAL | 2022-08-17T11:27:31.990498+00:00 | 2022-08-17T11:27:31.990527+00:00 | 713 | false | ```\nclass Solution {\n public static String minInteger(String num, int k) {\n char[] array = num.toCharArray(); // returns a newly allocated character array.\n int length = array.length;\n int pointer = 0; // the index replaced at most K times by the smallest possible digit\n while (k > ... | 2 | 0 | ['Java'] | 1 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Segment tree and Queue | segment-tree-and-queue-by-shiraj2802-udns | \tclass SegmentTree {\n\t\tpublic:\n\t\tvector arr;\n\t\tint n;\n\t\tSegmentTree(int n) {\n\t\t\tthis->n=n;\n\t\t\tarr = vector(4n);\n\t\t}\n\t\tvoid add(int i) | shiraj2802 | NORMAL | 2022-08-08T17:38:15.247497+00:00 | 2022-08-08T17:38:15.247539+00:00 | 378 | false | \tclass SegmentTree {\n\t\tpublic:\n\t\tvector<int> arr;\n\t\tint n;\n\t\tSegmentTree(int n) {\n\t\t\tthis->n=n;\n\t\t\tarr = vector<int>(4*n);\n\t\t}\n\t\tvoid add(int i) {\n\t\t\taddUtil(0,n-1,i,0);\n\t\t}\n\n\t\tint addUtil(int l, int r, int ind, int node) {\n\t\t\tif(l == r && l == ind){\n\t\t\t\tarr[node]++;\n\t\t... | 2 | 0 | ['Tree'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | C++ Time O(n) , Space O(n) | c-time-on-space-on-by-aholtzman-f6mr | // The solution is base on:\n// 1) position queue for each digit\n// 2) The entries that removed before the first psition for each digit\n// 3) The next digit t | aholtzman | NORMAL | 2021-12-21T15:43:25.686737+00:00 | 2021-12-21T15:49:28.914818+00:00 | 682 | false | // The solution is base on:\n// 1) position queue for each digit\n// 2) The entries that removed before the first psition for each digit\n// 3) The next digit to add to the return string is the lowest digit which the position minus removed entries not greater than k;\n// 4) Update return string; ( time n)\n// 5) Update... | 2 | 0 | [] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | [Python] O(n) time and O(1) space complexity | python-on-time-and-o1-space-complexity-b-mg0n | \nclass Solution:\n def minInteger(self, num, k):\n ln=len(num)\n optimizedNums=0\n YJSP=1145141919810\n a={\'0\':YJSP,\'1\':YJSP | tztanjunjie | NORMAL | 2020-10-03T17:06:42.428347+00:00 | 2020-10-03T17:06:42.428377+00:00 | 689 | false | ```\nclass Solution:\n def minInteger(self, num, k):\n ln=len(num)\n optimizedNums=0\n YJSP=1145141919810\n a={\'0\':YJSP,\'1\':YJSP,\'2\':YJSP,\'3\':YJSP,\'4\':YJSP,\'5\':YJSP,\'6\':YJSP,\'7\':YJSP,\'8\':YJSP,\'9\':YJSP}\n for i in range(ln):\n if i<a[num[i]]:a[num[i]]=... | 2 | 2 | [] | 1 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | [Python] Detailed comments with explanation, using bsearch only | python-detailed-comments-with-explanatio-tisc | The O(N*logN) solution requires a bit of leap of faith and view the problem from a different angle. I\'ve also left in my first attempt that TLEs which would gi | algomelon | NORMAL | 2020-07-12T18:11:43.135360+00:00 | 2020-07-12T18:18:43.734131+00:00 | 229 | false | The `O(N*logN)` solution requires a bit of leap of faith and view the problem from a different angle. I\'ve also left in my first attempt that TLEs which would give some motivation to the `O(N*logN)`\nsolution.\n\nThe key to understanding the solution is viewing it as a selection problem instead of a swapping problem. ... | 2 | 0 | [] | 1 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | [Java][中文视频] Fenwick Tree O(nlogn) Solution | javazhong-wen-shi-pin-fenwick-tree-onlog-hj2w | Video:\nhttps://www.youtube.com/watch?v=GYin0E1ENWM\n\nclass Solution {\n public String minInteger(String num, int k) {\n int n = num.length();\n | zzzyq | NORMAL | 2020-07-09T15:55:06.848785+00:00 | 2020-07-09T15:55:06.848828+00:00 | 314 | false | Video:\nhttps://www.youtube.com/watch?v=GYin0E1ENWM\n```\nclass Solution {\n public String minInteger(String num, int k) {\n int n = num.length();\n FenwickTree ft = new FenwickTree(n);\n for(int i = 0; i < n; i++) {\n ft.update(i, 1);\n }\n Queue[] arr = new Queue[10];\... | 2 | 1 | [] | 2 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Python NlogN Fenwick/BIT solution | python-nlogn-fenwickbit-solution-by-nate-dysn | python\nclass Solution:\n def minInteger(self, num: str, k: int) -> str:\n n = len(num)\n mp = defaultdict(deque)\n for i,v in enumerate | nate17 | NORMAL | 2020-07-05T09:41:45.701339+00:00 | 2020-07-06T03:26:31.889417+00:00 | 168 | false | ```python\nclass Solution:\n def minInteger(self, num: str, k: int) -> str:\n n = len(num)\n mp = defaultdict(deque)\n for i,v in enumerate(num):\n mp[v].append(i)\n res = ""\n def lowbit(x):\n return x & (-x)\n def query(x):\n sums = 0\n ... | 2 | 0 | [] | 1 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Python Bisect O(nlgn) Pass! [SO HARD] | python-bisect-onlgn-pass-so-hard-by-wsy1-zco1 | \tclass Solution:\n\t\tdef minInteger(self, num: str, k: int) -> str:\n\n\t\t\tnum = list(num)\n\t\t\tsmall = sorted(list(num))\n\n\t\t\tif k > len(num) ** 2:\n | wsy19970125 | NORMAL | 2020-07-05T05:15:18.710672+00:00 | 2020-07-05T05:18:15.728148+00:00 | 196 | false | \tclass Solution:\n\t\tdef minInteger(self, num: str, k: int) -> str:\n\n\t\t\tnum = list(num)\n\t\t\tsmall = sorted(list(num))\n\n\t\t\tif k > len(num) ** 2:\n\t\t\t\treturn \'\'.join(small)\n\n\t\t\td = defaultdict(list)\n\t\t\tfor i in range(len(num)):\n\t\t\t\td[num[i]].append(i)\n\n\t\t\tindexs = []\n\t\t\tfor cha... | 2 | 1 | [] | 2 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Efficient JS Solution - Greedy + No Segment tree - O(10n) | efficient-js-solution-greedy-no-segment-5hfvk | Pro tip: love Lil Xie! \uD83D\uDC9C\n\n# Brief explanation\n- We will generate the result from left to right. For each position, we will also greedily choose th | CuteTN | NORMAL | 2024-01-10T11:59:37.274912+00:00 | 2024-01-10T12:44:23.624387+00:00 | 244 | false | Pro tip: love Lil Xie! \uD83D\uDC9C\n\n# Brief explanation\n- We will generate the result from left to right. For each position, we will also greedily choose the smallest digit possible.\n- Suppose we want to move digit `d` from the position `j` of num to the position `i` of the result. We will calculate how many swaps... | 1 | 0 | ['Greedy', 'Counting', 'JavaScript'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | C++ | Most Intuitive | c-most-intuitive-by-mukulgupta1357-06ud | \n# Code\n\nclass Solution {\npublic:\n string minInteger(string num, int k) {\n int n = num.size();\n if(k > n*(n+1)/2)\n {\n | mukulgupta1357 | NORMAL | 2023-08-13T11:07:43.984531+00:00 | 2023-08-13T11:07:43.984556+00:00 | 195 | false | \n# Code\n```\nclass Solution {\npublic:\n string minInteger(string num, int k) {\n int n = num.size();\n if(k > n*(n+1)/2)\n {\n sort(num.begin(), num.end());\n }\n\n for(int i=0;i<n && k >0;i++)\n {\n int pos = i;\n for(int j=i+1;j<n;j++)\n... | 1 | 0 | ['Sliding Window', 'C++'] | 2 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Python Sliding Window + AVL Tree Solution | Easy to Understand | python-sliding-window-avl-tree-solution-a73tk | Approach\n Describe your approach to solving the problem. \nMaintain a window of size k in a AVL Tree, pop out the minimum element in the index in O(log(n)) tim | hemantdhamija | NORMAL | 2023-01-06T05:08:50.933493+00:00 | 2023-01-06T05:08:50.933534+00:00 | 168 | false | # Approach\n<!-- Describe your approach to solving the problem. -->\nMaintain a window of size ```k``` in a AVL Tree, pop out the minimum element in the index in $$O(log(n))$$ time, find its index, decrement ```k``` accordingly until ```k``` becomes ```0```.\n\n# Complexity\n- Time complexity: $$O(n * log(n))$$\n<!-- A... | 1 | 0 | ['String', 'Greedy', 'Binary Search Tree', 'Sliding Window', 'Python3'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | C++ || Segment tree || Explained solution | c-segment-tree-explained-solution-by-raj-c1ap | ```\n/\n\nswap any two adjacent digits atmost k times -> minimum integer formed after this??\n\nwill greedy approach works here ?\n\nbringing all smaller no\' a | Rajdeep_Nagar | NORMAL | 2022-10-06T16:29:34.867131+00:00 | 2022-10-06T16:29:34.867173+00:00 | 115 | false | ```\n/*\n\nswap any two adjacent digits atmost k times -> minimum integer formed after this??\n\nwill greedy approach works here ?\n\nbringing all smaller no\' ahead as possible as\n \n \n(bring that smallest no. here which is towards right of it and can be brought here within k steps) () () ()...\n\n\nValue of k decre... | 1 | 0 | ['Tree', 'C'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | O(nlogn): Segment tree + binary search | onlogn-segment-tree-binary-search-by-sha-2ip5 | For a given swap count k, if we are currently at index i of our array nums, we wish to find the minimum element in nums that is <= k swaps away from index i, an | shaunakdas88 | NORMAL | 2022-06-21T13:12:33.660780+00:00 | 2022-06-21T20:23:41.390422+00:00 | 231 | false | For a given swap count `k`, if we are currently at index `i` of our array `nums`, we wish to find the minimum element in `nums` that is `<= k` swaps away from index `i`, and bubble it up to `i`\'s position, eating up some number of swaps along the way. We repeatedly do this until we have either:\n- exhausted all swaps\... | 1 | 0 | [] | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.