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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
unique-binary-search-trees-ii | ✅Unique Binary Search Trees II || Recursion W/ Approach || C++ | Python | unique-binary-search-trees-ii-recursion-gik9v | Idea:\n We will use a recursive helper function that recieves a range (within n) and returns all subtrees in that range.\n We have a few cases:\n\t if start > e | Maango16 | NORMAL | 2021-09-02T18:22:13.210500+00:00 | 2021-09-02T18:22:13.210541+00:00 | 1,163 | false | **Idea**:\n* We will use a recursive helper function that recieves a range `(within n)` and returns all subtrees in that range.\n* We have a few cases:\n\t* if `start > end`, which is not supposed to happen, we return a list that contains only a `null`.\n\t* if `start == end` it means we reached a `leaf` and we will re... | 23 | 8 | [] | 5 |
unique-binary-search-trees-ii | [Python] divide and conquer with dp, explained | python-divide-and-conquer-with-dp-explai-qua3 | We can use dp technique here, where in dp[i][j] we keep all possible trees from numbers i ... j. We can also keep only one dimensional dp, but then we need to c | dbabichev | NORMAL | 2021-09-02T07:10:07.427399+00:00 | 2021-09-02T07:10:07.427461+00:00 | 1,172 | false | We can use `dp` technique here, where in `dp[i][j]` we keep all possible trees from numbers `i ... j`. We can also keep only one dimensional `dp`, but then we need to `clone` trees, adding the same values to all elements. However, number of trees is exponential, and I think it is not worth to make this optimization fro... | 23 | 2 | ['Divide and Conquer', 'Dynamic Programming'] | 3 |
unique-binary-search-trees-ii | C++ very easy to understand Recursive Solution | c-very-easy-to-understand-recursive-solu-vag1 | \nclass Solution {\npublic:\n \n vector<TreeNode*> generateBST(int begin, int end)\n {\n vector<TreeNode*>V;\n \n if(begin>end)\n | chirags_30 | NORMAL | 2020-07-28T03:06:18.855904+00:00 | 2020-07-28T03:06:18.855938+00:00 | 2,882 | false | ```\nclass Solution {\npublic:\n \n vector<TreeNode*> generateBST(int begin, int end)\n {\n vector<TreeNode*>V;\n \n if(begin>end)\n {\n V.push_back(NULL);\n return V;\n }\n \n for(int i=begin; i<=end; i++)\n {\n vector<Tr... | 23 | 1 | ['Recursion', 'C', 'C++'] | 4 |
unique-binary-search-trees-ii | Video Solution | 2 Approaches (Recursion + Memoization) | Intuition explained in detail | video-solution-2-approaches-recursion-me-nx8l | \n# Video\n Describe your first thoughts on how to solve this problem. \nHey everyone, i have created a video solution for this problem (it\'s in hindi), wher | _code_concepts_ | NORMAL | 2024-08-17T06:43:20.637873+00:00 | 2024-08-17T06:43:20.637910+00:00 | 1,721 | false | \n# Video\n<!-- Describe your first thoughts on how to solve this problem. -->\nHey everyone, i have created a video solution for this problem (it\'s in hindi), where i have explained the video from very basic intuition to optimal approach, which means there are in total 2 approches:\n- Recursion\n- Memoization (DP)\... | 21 | 0 | ['C++'] | 2 |
unique-binary-search-trees-ii | [Python] Recursive solution explained (beats 99.56%) | python-recursive-solution-explained-beat-rd0o | The basic idea is to recursively split the conceptual array [1, ..., n] in half. During every split, make a new root node for every different combination of lef | m-just | NORMAL | 2020-03-11T08:02:41.666231+00:00 | 2020-03-23T07:15:26.564507+00:00 | 3,329 | false | The basic idea is to recursively split the conceptual array `[1, ..., n]` in half. During every split, make a new root node for every different combination of left/right subtree structures. For the time complexity of this algorithm, please check the comment below.\n```python\ndef generateTrees(self, n: int) -> List[Tre... | 21 | 2 | ['Recursion', 'Python', 'Python3'] | 3 |
unique-binary-search-trees-ii | 30 ms c++ solution | 30-ms-c-solution-by-pankit-fl9v | /**\n * Definition for binary tree\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeN | pankit | NORMAL | 2015-02-26T02:19:51+00:00 | 2018-09-14T20:15:36.868602+00:00 | 4,193 | false | /**\n * Definition for binary tree\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\n class Solution {\n public:\n vector<TreeNode *> generateTrees(int n) {\n ... | 21 | 1 | [] | 7 |
unique-binary-search-trees-ii | 20ms C++ top-down DP solution | 20ms-c-top-down-dp-solution-by-ragepyre-rvvq | a bottom up solution looks much better, but I find it's also a little bit harder to understand. Top-down solution is straight forward,\n\n \n\n vector gene | ragepyre | NORMAL | 2015-06-26T19:14:46+00:00 | 2015-06-26T19:14:46+00:00 | 4,776 | false | a bottom up solution looks much better, but I find it's also a little bit harder to understand. Top-down solution is straight forward,\n\n \n\n vector<TreeNode*> generateTrees(int n) {\n vector<TreeNode*> ret;\n vector<vector<vector<TreeNode*>>> dp(n,vector<vector<TreeNode*>>(n));\n helper(1,n... | 18 | 0 | ['Dynamic Programming', 'Memoization'] | 3 |
unique-binary-search-trees-ii | Beats 100% + Video | Java C++ Python | beats-100-video-java-c-python-by-jeevank-umq8 | \n\n\nclass Solution {\n Map<Pair<Integer, Integer>, List<TreeNode>> dp; \n public List<TreeNode> generateTrees(int n) {\n dp = new HashMap<>();\n | jeevankumar159 | NORMAL | 2023-08-05T02:44:29.846398+00:00 | 2023-08-05T02:44:29.846432+00:00 | 3,123 | false | <iframe width="560" height="315" src="https://www.youtube.com/embed/4Ca9t6LYRDI" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>\n\n```\nclass Solution {\n Map<Pair<Integer, Integer>, Li... | 16 | 3 | ['C', 'Python', 'Java'] | 0 |
unique-binary-search-trees-ii | 6 Liner C++ Recursive | 6-liner-c-recursive-by-amitpandey31-v7l1 | \nclass Solution {\npublic:\n vector<TreeNode*> generateTrees(int n, int s = 1) {\n vector<TreeNode*> ans;\n if(n < s) return {nullptr}; | AmitPandey31 | NORMAL | 2022-08-30T07:26:29.241226+00:00 | 2022-08-31T17:31:11.373891+00:00 | 2,184 | false | ```\nclass Solution {\npublic:\n vector<TreeNode*> generateTrees(int n, int s = 1) {\n vector<TreeNode*> ans;\n if(n < s) return {nullptr}; \n for(int i=s; i<=n; i++) { \t // Consider every number in range [s,n] as root \n ... | 15 | 0 | ['Recursion', 'C', 'C++'] | 0 |
unique-binary-search-trees-ii | [Java] Recursion & Iteration Solutions (DP, Clone, Memoization) | java-recursion-iteration-solutions-dp-cl-k2x7 | Reference: LeetCode\nDifficulty: Medium\n\n## Problem\n\n> Given an integer n, generate all structurally unique BST\'s (binary search trees) that store values 1 | junhaowanggg | NORMAL | 2019-10-21T06:58:45.484098+00:00 | 2019-10-21T06:59:37.499329+00:00 | 1,368 | false | Reference: [LeetCode](https://leetcode.com/problems/unique-binary-search-trees-ii/)\nDifficulty: <span class="orange">Medium</span>\n\n## Problem\n\n> Given an integer `n`, generate all structurally unique BST\'s (binary search trees) that store values `1 ... n`.\n\n**Example:**\n\n```java\nInput: 3\nOutput:\n[\n [1,n... | 15 | 1 | ['Dynamic Programming', 'Recursion', 'Memoization', 'Java'] | 0 |
unique-binary-search-trees-ii | An Intuitive Explanation with Diagrams | an-intuitive-explanation-with-diagrams-b-e9ei | BASICS\nBST? A node x has node z to right with value z>x and node y to the left with y<x.\n\n\n\nBUILDING THE INTUITION: OBSERVATIONS\n- n = 1 case is trivial\n | chaudhary1337 | NORMAL | 2021-09-02T09:20:16.709976+00:00 | 2021-09-02T09:26:15.079863+00:00 | 614 | false | ***BASICS***\nBST? A node `x` has node `z` to right with value `z>x` and node `y` to the left with `y<x`.\n\n\n\n***BUILDING THE INTUITION: OBSERVATIONS***\n- n = 1 case is trivial\n- n = 2 case suggests\n\t- e... | 14 | 1 | [] | 1 |
unique-binary-search-trees-ii | C++ || Recursion || Day 5 | c-recursion-day-5-by-chiikuu-gljb | Code\n\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : | CHIIKUU | NORMAL | 2023-08-05T09:12:32.615217+00:00 | 2023-08-05T09:13:17.681987+00:00 | 3,027 | false | # Code\n```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod... | 13 | 0 | ['Tree', 'Binary Search Tree', 'Recursion', 'C++'] | 4 |
unique-binary-search-trees-ii | C++ Easy Recursive Solution | c-easy-recursive-solution-by-mythri_kaul-rmzj | Let us start by understanding how many unique BSTs are possible for keys 1-n.\n\nTo form a structurally unique BST of n nodes, we can take any of the 1-n keys a | Mythri_Kaulwar | NORMAL | 2021-11-08T07:32:10.699930+00:00 | 2021-11-08T07:34:31.543216+00:00 | 1,048 | false | Let us start by understanding how many unique BSTs are possible for keys 1-n.\n\nTo form a structurally unique BST of n nodes, we can take any of the 1-n keys as the root. Let us take key i (1<=i<=n) as the root. Now the left subtrees can contain nodes with keys 1 to i-1 (Since all the values in the left subtree must b... | 13 | 0 | ['Recursion', 'C', 'C++'] | 1 |
unique-binary-search-trees-ii | Sharing top-down DP Python solution, beats 93.33% | sharing-top-down-dp-python-solution-beat-my2r | def generateTrees(self, n):\n \n def gen_trees(s, e, memo):\n if e < s:\n return [None]\n ret_list = []\n | agave | NORMAL | 2016-04-06T02:30:47+00:00 | 2018-10-13T00:32:38.631220+00:00 | 1,856 | false | def generateTrees(self, n):\n \n def gen_trees(s, e, memo):\n if e < s:\n return [None]\n ret_list = []\n if (s, e) in memo:\n return memo[s, e]\n for i in range(s, e + 1):\n list_left = gen_trees(s, i - 1, memo)\... | 13 | 0 | ['Dynamic Programming', 'Memoization'] | 3 |
unique-binary-search-trees-ii | C++ || Recursion | c-recursion-by-abhay5349singh-62yd | Connect with me on LinkedIn: https://www.linkedin.com/in/abhay5349singh/\n\n\n\n\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int v | abhay5349singh | NORMAL | 2023-08-05T01:57:28.169455+00:00 | 2023-08-05T02:06:30.411720+00:00 | 1,775 | false | **Connect with me on LinkedIn**: https://www.linkedin.com/in/abhay5349singh/\n\n\n\n```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNod... | 12 | 2 | ['Recursion', 'C++'] | 1 |
unique-binary-search-trees-ii | ✔️ 100% Fastest Swift Solution | 100-fastest-swift-solution-by-sergeylesc-tnlh | \n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * public var val: Int\n * public var left: TreeNode?\n * public var right | sergeyleschev | NORMAL | 2022-04-08T13:30:55.221356+00:00 | 2022-04-08T13:30:55.221406+00:00 | 694 | false | ```\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * public var val: Int\n * public var left: TreeNode?\n * public var right: TreeNode?\n * public init() { self.val = 0; self.left = nil; self.right = nil; }\n * public init(_ val: Int) { self.val = val; self.left = nil; sel... | 12 | 0 | ['Swift'] | 2 |
unique-binary-search-trees-ii | Explanation with diagrams,DP solution. | explanation-with-diagramsdp-solution-by-6w02n | Again I am going to explain a DP solution using diagrams.\nFirst I define a two-dimension list res[0..n][0..n].(res[0][0] is of no use,just for readability,so w | planegoose | NORMAL | 2019-07-11T14:33:42.723872+00:00 | 2019-07-11T14:33:42.723907+00:00 | 656 | false | Again I am going to explain a DP solution using diagrams.\nFirst I define a two-dimension list res[0..n][0..n].(res[0][0] is of no use,just for readability,so we will ignore them later.)\nres[i][j] stores the root TreeNode of all possible BST formed by integers from i to j.Then our goal is res[1][n].\nTo calculate res[... | 12 | 0 | ['Dynamic Programming'] | 0 |
unique-binary-search-trees-ii | JavaScript DFS with Memo | javascript-dfs-with-memo-by-linfongi-2twb | js\nfunction generateTrees(n) {\n if (n < 1) return [];\n const dp = [...Array(n+1)].map(r => Array(n+1));\n return generate(1, n);\n \n function generate( | linfongi | NORMAL | 2018-07-19T03:11:20.463064+00:00 | 2018-07-19T03:11:20.463064+00:00 | 1,091 | false | ```js\nfunction generateTrees(n) {\n if (n < 1) return [];\n const dp = [...Array(n+1)].map(r => Array(n+1));\n return generate(1, n);\n \n function generate(s, e) {\n if (s > e) return [null];\n if (dp[s][e]) return dp[s][e];\n \n const res = [];\n for (let root = s; root <= e; root++) {\n for... | 12 | 0 | [] | 2 |
unique-binary-search-trees-ii | A simple bottom-up DP solution | a-simple-bottom-up-dp-solution-by-albja1-epj3 | The optimal substructure is that for any BST with nodes 1 to n, pick i-th node as root, then the left subtree will contain nodes from 1 to (i-1), and the right | albja16 | NORMAL | 2015-04-09T10:01:27+00:00 | 2015-04-09T10:01:27+00:00 | 3,881 | false | The optimal substructure is that for any BST with nodes 1 to n, pick i-th node as root, then the left subtree will contain nodes from 1 to (i-1), and the right subtree will contain nodes from (i+1) to n. I use a 3-d vector to store all possible trees for subtrees with nodes from i to j (0 <= i <= j <=n+1 ), if i==j, th... | 12 | 0 | ['Binary Search', 'Tree', 'C++'] | 1 |
unique-binary-search-trees-ii | Easy to Understand || DP solution || Iterative || Space Optimised || Better Runtime | easy-to-understand-dp-solution-iterative-zd22 | \nclass Solution {\npublic:\n \n TreeNode* newTreeRight(TreeNode* root, int j){\n if(root==NULL){\n return root;\n }\n Tre | arunit | NORMAL | 2022-01-06T06:32:25.911780+00:00 | 2022-01-06T06:32:52.695109+00:00 | 497 | false | ```\nclass Solution {\npublic:\n \n TreeNode* newTreeRight(TreeNode* root, int j){\n if(root==NULL){\n return root;\n }\n TreeNode* newRoot = new TreeNode(root->val + j);\n newRoot->left = newTreeRight(root->left, j);\n newRoot->right = newTreeRight(root->right, j);\n... | 11 | 0 | ['Dynamic Programming', 'C'] | 2 |
unique-binary-search-trees-ii | Unique Binary Search Trees II [C++] | unique-binary-search-trees-ii-c-by-movee-wia8 | IntuitionThe task is to generate all unique binary search trees (BSTs) that can be formed with values from 1 to n. The key observation is that for each number i | moveeeax | NORMAL | 2025-01-24T12:58:32.762451+00:00 | 2025-01-24T12:58:32.762451+00:00 | 980 | false | # Intuition
The task is to generate all unique binary search trees (BSTs) that can be formed with values from 1 to `n`. The key observation is that for each number `i` from `1` to `n`, we can consider `i` as the root of the tree. Once the root is fixed, the problem boils down to constructing the left and right subtrees... | 10 | 1 | ['C++'] | 0 |
unique-binary-search-trees-ii | java easy solution (recursion) | java-easy-solution-recursion-by-rmanish0-ta0h | \n\nclass Solution {\n public List<TreeNode> generateTrees(int n) {\n return recursion(1,n);\n }\n List<TreeNode> recursion(int start ,int end)\ | rmanish0308 | NORMAL | 2021-07-07T05:25:43.698401+00:00 | 2021-07-07T05:25:43.698449+00:00 | 789 | false | ```\n\nclass Solution {\n public List<TreeNode> generateTrees(int n) {\n return recursion(1,n);\n }\n List<TreeNode> recursion(int start ,int end)\n {\n List<TreeNode> list = new ArrayList<>();\n if(start > end)\n {\n list.add(null);\n return list;\n ... | 10 | 0 | ['Recursion', 'Java'] | 1 |
unique-binary-search-trees-ii | Easy Java solution | Faster than 96 % | easy-java-solution-faster-than-96-by-jyo-dgbw | \nclass Solution {\n public List<TreeNode> generateTrees(int n) {\n if(n < 1)return Collections.EMPTY_LIST;\n return helper(1,n);\n }\n \ | jyotiprakashrout434 | NORMAL | 2020-07-11T18:15:46.155141+00:00 | 2020-07-11T18:15:46.155218+00:00 | 958 | false | ```\nclass Solution {\n public List<TreeNode> generateTrees(int n) {\n if(n < 1)return Collections.EMPTY_LIST;\n return helper(1,n);\n }\n \n private List<TreeNode> helper(int start , int end){\n List<TreeNode> ans = new ArrayList<>();\n if(start > end){\n ans.add(null... | 10 | 2 | ['Recursion', 'Java'] | 3 |
unique-binary-search-trees-ii | [C++] Pure Recursion to DP (only with addition of 2 Lines) | c-pure-recursion-to-dp-only-with-additio-5867 | There is a significant amount of improvement in the 2nd solution compared to the effort made to code that one over the inital recursive un-memoized solution.\n\ | Rxnjeet | NORMAL | 2022-07-14T07:47:03.200756+00:00 | 2022-07-19T06:29:31.895205+00:00 | 868 | false | There is a significant amount of improvement in the 2nd solution compared to the effort made to code that one over the inital recursive un-memoized solution.\n\n# 1. Pure Recursion (43 ms)\n```\nclass Solution {\npublic:\n vector<TreeNode*> buildTrees(int l, int r) {\n \n if(l > r) return {nullptr};\n ... | 9 | 0 | ['Dynamic Programming', 'Recursion', 'Memoization', 'C', 'C++'] | 0 |
unique-binary-search-trees-ii | Recursive C++ solution : faster than 97% c++ submissions | recursive-c-solution-faster-than-97-c-su-24n2 | INTUTION:\nFor every element in range 1 to n lets say (temp), we try to find its corresponding left and right subtrees by helper(start,i-1) && helper(i+1,end) i | Sanath404 | NORMAL | 2021-07-14T13:09:10.312598+00:00 | 2021-07-14T13:09:10.312636+00:00 | 882 | false | **INTUTION:**\nFor every element in range 1 to n lets say (temp), we try to find its corresponding left and right subtrees by helper(start,i-1) && helper(i+1,end) iterate through all possibilities of them and set temp\'s left and right sub trees and push back to some vector and return.\n```\nvector<TreeNode*> generateT... | 9 | 0 | ['Recursion', 'C'] | 1 |
unique-binary-search-trees-ii | Approach+code | approachcode-by-geeks_12-4m2r | Honestly I was not able to solve the problem in one go. I was able to get the idea to solve the problem but wasn\'t able to implement it. \nFirstly try to solve | geeks_12 | NORMAL | 2021-04-23T06:33:58.903706+00:00 | 2021-04-23T06:33:58.903731+00:00 | 241 | false | Honestly I was not able to solve the problem in one go. I was able to get the idea to solve the problem but wasn\'t able to implement it. \nFirstly try to solve the problem Unique Binary Search Tree\nSo here is my approach:\n1) We need to generate a ***BST*** which means that left child will contain the node value less... | 9 | 0 | [] | 2 |
unique-binary-search-trees-ii | JavaScript Solution - Top Down & Bottom Up Approach | javascript-solution-top-down-bottom-up-a-fbf6 | Top-Down Approach:\n\n\nvar generateTrees = function(n) {\n if (n == 0) return [];\n \n return findAllUniqueTrees(1, n);\n\n function findAllUniqueT | Deadication | NORMAL | 2020-06-15T17:34:47.550928+00:00 | 2020-06-23T23:11:05.380129+00:00 | 1,336 | false | **Top-Down Approach:**\n\n```\nvar generateTrees = function(n) {\n if (n == 0) return [];\n \n return findAllUniqueTrees(1, n);\n\n function findAllUniqueTrees(start, end) {\n const ans = [];\n \n // base case\n if (start > end) {\n ans.push(null);\n return ... | 9 | 0 | ['Dynamic Programming', 'JavaScript'] | 1 |
unique-binary-search-trees-ii | Python solution | python-solution-by-zitaowang-gkcx | \nclass Solution(object):\n def generateTrees(self, n):\n """\n :type n: int\n :rtype: List[TreeNode]\n """\n def generate | zitaowang | NORMAL | 2018-08-30T09:04:54.958421+00:00 | 2018-10-15T00:19:53.781156+00:00 | 1,461 | false | ```\nclass Solution(object):\n def generateTrees(self, n):\n """\n :type n: int\n :rtype: List[TreeNode]\n """\n def generate(i,j):\n if j-i < 0:\n return [None]\n elif j-i == 0:\n return [TreeNode(i)]\n else:\n ... | 9 | 0 | [] | 3 |
unique-binary-search-trees-ii | Help simplify my code (the second one) | help-simplify-my-code-the-second-one-by-6twsr | class Solution {\n private:\n \tvector<TreeNode*> generateTreesRec(int start, int end){\n \t\tvector<TreeNode*> v;\n \t\tif(start > end){\n \t\t\ | lzl124631x | NORMAL | 2014-03-09T12:02:23+00:00 | 2014-03-09T12:02:23+00:00 | 4,705 | false | class Solution {\n private:\n \tvector<TreeNode*> generateTreesRec(int start, int end){\n \t\tvector<TreeNode*> v;\n \t\tif(start > end){\n \t\t\tv.push_back(NULL);\n \t\t\treturn v;\n \t\t}\n \t\tfor(int i = start; i <= end; ++i){\n \t\t\tvector<TreeNode*> left = generateTreesRec(start, ... | 9 | 0 | [] | 7 |
unique-binary-search-trees-ii | DP solution in Python | dp-solution-in-python-by-heliu023-uhla | ----------\n\nclass Solution:\n # @return a list of tree node\n\n def generateTrees(self, n):\n if n == 0:\n return [None]\n tree | heliu023 | NORMAL | 2015-03-26T22:41:17+00:00 | 2018-10-09T21:10:33.024397+00:00 | 2,524 | false | ----------\n\nclass Solution:\n # @return a list of tree node\n\n def generateTrees(self, n):\n if n == 0:\n return [None]\n tree_list = [[[None]] * (n + 2) for i in range(n + 2)]\n for i in range(1, n + 1):\n tree_list[i][i] = [TreeNode(i)]\n for j in reverse... | 9 | 0 | [] | 1 |
unique-binary-search-trees-ii | 94.15% Unique Binary Search Trees II with step by step explanation | 9415-unique-binary-search-trees-ii-with-x8tc7 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nTo generate all structurally unique BST\'s with n nodes, we can use a rec | Marlen09 | NORMAL | 2023-02-15T05:27:56.554445+00:00 | 2023-02-15T05:27:56.554485+00:00 | 3,523 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nTo generate all structurally unique BST\'s with n nodes, we can use a recursive approach. The idea is to fix each number i (1 <= i <= n) as the root of the tree and then recursively generate all the left and right subtrees t... | 8 | 0 | ['Python', 'Python3'] | 3 |
unique-binary-search-trees-ii | DP solution in C++ and Java | dp-solution-in-c-and-java-by-savan07-ahh3 | Code in C++\n\nclass Solution {\npublic:\n vector<TreeNode*> generateTrees(int n) {\n return subTrees(1, n);\n }\nprivate:\n vector<TreeNode*> s | savan07 | NORMAL | 2022-06-29T08:45:04.689985+00:00 | 2022-06-29T08:45:04.690043+00:00 | 1,251 | false | **Code in C++**\n```\nclass Solution {\npublic:\n vector<TreeNode*> generateTrees(int n) {\n return subTrees(1, n);\n }\nprivate:\n vector<TreeNode*> subTrees(int start, int end){\n vector<TreeNode*> res;\n if(start>end){\n res.push_back(NULL);\n return res;\n ... | 8 | 0 | ['Dynamic Programming', 'C', 'C++', 'Java'] | 1 |
unique-binary-search-trees-ii | Python Recursive Explanation | python-recursive-explanation-by-wei_lun-3kvq | For every node with value val\n\t Left subtree\'s root\'s value must be in [1, val - 1]\n\n\t Right subtree\'s root\'s value must be in [val + 1, n] \n\n2. Now, | wei_lun | NORMAL | 2020-07-05T04:42:45.706126+00:00 | 2020-07-05T04:42:59.779286+00:00 | 215 | false | 1. For every node with value `val`\n\t* **Left subtree\'s root**\'s value must be in `[1, val - 1]`\n\n\t* **Right subtree\'s root**\'s value must be in `[val + 1, n]` \n\n2. Now, what **root value** do we get to try in each recursive call? We try root values in `[start, end]`\n\n3. This means for each root, there is a... | 8 | 0 | [] | 0 |
unique-binary-search-trees-ii | Quite clean Java solution with explanation | quite-clean-java-solution-with-explanati-dux5 | For all possible root of the trees (i.e. 1, 2, ..., n), get the list of left subtrees and list of right subtrees, recursively. Now, for every left and right sub | whitehat | NORMAL | 2015-12-30T19:30:22+00:00 | 2015-12-30T19:30:22+00:00 | 1,709 | false | For all possible root of the trees (i.e. 1, 2, ..., n), get the list of left subtrees and list of right subtrees, recursively. Now, for every left and right subtree combination, create a new tree and add to resultant list.\n\nHere, "start > end" becomes the base case for recursion, for which I add "null" as the only el... | 8 | 0 | ['Java'] | 0 |
unique-binary-search-trees-ii | 🦀 Unique Binary Search Trees II - A Rusty Approach | unique-binary-search-trees-ii-a-rusty-ap-4388 | Have you ever tried to generate all unique binary search trees (BSTs) with a given number of nodes? It\'s a crab-tastic challenge, but don\'t worry; Rust is her | phistellar | NORMAL | 2023-08-05T01:47:42.736731+00:00 | 2023-08-05T02:11:57.089894+00:00 | 1,558 | false | Have you ever tried to generate all unique binary search trees (BSTs) with a given number of nodes? It\'s a crab-tastic challenge, but don\'t worry; Rust is here to help you out!\n\n## Intuition\nImagine you have \\( n \\) distinct numbers. You can pick any number as the root of the BST. Once you pick a root, the remai... | 7 | 0 | ['Dynamic Programming', 'Go', 'Python3', 'Rust'] | 0 |
unique-binary-search-trees-ii | C++ recursive Catalan number-like DP solution | c-recursive-catalan-number-like-dp-solut-mxxu | Intuition\n Describe your first thoughts on how to solve this problem. \nUse 3D array for TreeNode* as memoization. Solve it recursely with memo. Again a DP sol | anwendeng | NORMAL | 2023-07-30T08:28:34.650519+00:00 | 2023-07-30T08:42:01.500135+00:00 | 664 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nUse 3D array for TreeNode* as memoization. Solve it recursely with memo. Again a DP solution is done. \n\nThis code generates all possible unique binary search trees for a given range [1, n] using dynamic programming. It uses a 3D vector... | 7 | 0 | ['Math', 'Dynamic Programming', 'Tree', 'Depth-First Search', 'C++'] | 1 |
unique-binary-search-trees-ii | javascript 8 line solution | javascript-8-line-solution-by-liushuaima-0vyh | ```js\nvar generateTrees = function(n, l = 1, r = n, res = []) {\n for(let i = l; i <= r; i++){\n for(const left of generateTrees(n, l, i - 1)){\n | liushuaimaya | NORMAL | 2019-12-22T01:47:31.033351+00:00 | 2019-12-22T01:48:15.715758+00:00 | 1,149 | false | ```js\nvar generateTrees = function(n, l = 1, r = n, res = []) {\n for(let i = l; i <= r; i++){\n for(const left of generateTrees(n, l, i - 1)){\n for(const right of generateTrees(n, i + 1, r)){\n res.push({val: i, left, right});\n }\n }\n }\n return n ? res.l... | 7 | 2 | ['JavaScript'] | 0 |
unique-binary-search-trees-ii | C++ 16ms, beats 98.1% of cpp submissions | c-16ms-beats-981-of-cpp-submissions-by-i-u5yo | \nvector<TreeNode*> genTreesUtil(int beg, int end) {\n\tif (end < beg) return { nullptr };\n\tif (end == beg) return { new TreeNode(beg) };\n\n\tvector<TreeNode | igorleet | NORMAL | 2019-05-26T00:07:40.158338+00:00 | 2019-05-26T00:09:12.264529+00:00 | 913 | false | ```\nvector<TreeNode*> genTreesUtil(int beg, int end) {\n\tif (end < beg) return { nullptr };\n\tif (end == beg) return { new TreeNode(beg) };\n\n\tvector<TreeNode*> trees;\n\tfor (int i = beg; i <= end; ++i) {\n\n\t\tauto leftTrees = genTreesUtil(beg, i - 1);\n\t\tauto rightTrees = genTreesUtil(i + 1, end);\n\n\t\tfor... | 7 | 0 | ['Recursion', 'C++'] | 4 |
unique-binary-search-trees-ii | (Python) accepted python code | python-accepted-python-code-by-zhshuai1-y8qu | class Solution:\n # @return a list of tree node\n def generate(self,s,t):\n '''recursion with left and right branches'''\n i | zhshuai1 | NORMAL | 2014-09-18T14:06:07+00:00 | 2014-09-18T14:06:07+00:00 | 2,640 | false | class Solution:\n # @return a list of tree node\n def generate(self,s,t):\n '''recursion with left and right branches'''\n if s>t:return [None];\n if s==t:return [TreeNode(s)];\n re=[];\n for i in range(s,t+1):\n left=self.generate(... | 7 | 0 | ['Python'] | 5 |
unique-binary-search-trees-ii | A straightforward python solution | a-straightforward-python-solution-by-kor-irwy | from itertools import product\n \n class Solution:\n # @param {integer} n\n # @return {TreeNode[]}\n def generateTrees(self, n):\n | kord | NORMAL | 2015-06-13T02:50:09+00:00 | 2015-06-13T02:50:09+00:00 | 1,575 | false | from itertools import product\n \n class Solution:\n # @param {integer} n\n # @return {TreeNode[]}\n def generateTrees(self, n):\n return self.BST([i+1 for i in range(n)])\n \n def BST(self, nodes):\n trees = []\n for i in range(len(nodes... | 7 | 0 | [] | 0 |
unique-binary-search-trees-ii | .:: Kotlin ::. Short straightforward explained | kotlin-short-straightforward-explained-b-qg9c | In each step of the recursion, we set a range. By exploring within this range, we can find the nodes on the left and right of the central point, which is the ro | dzmtr | NORMAL | 2023-08-05T11:40:48.819187+00:00 | 2023-08-05T11:40:48.819212+00:00 | 20 | false | In each step of the recursion, we set a range. By exploring within this range, we can find the nodes on the left and right of the central point, which is the root node.\n\n\n> Complexity (time & space): O(n!)\n\n\n```kotlin []\nclass Solution {\nfun generateTrees(n: Int): List<TreeNode?> {\n fun generate(min: Int, m... | 6 | 0 | ['Divide and Conquer', 'Kotlin'] | 0 |
unique-binary-search-trees-ii | Python Elegant & Short | Three lines | Top-Down DP | LRU cache | python-elegant-short-three-lines-top-dow-e576 | Complexity\n- Time complexity: O(n!)\n- Space complexity: O(n^{3})\n\n# Code\n\nclass Solution:\n\n def generateTrees(self, n: int) -> List[Optional[TreeNode | Kyrylo-Ktl | NORMAL | 2022-08-27T16:44:12.769559+00:00 | 2023-08-05T11:32:43.675773+00:00 | 1,516 | false | # Complexity\n- Time complexity: $$O(n!)$$\n- Space complexity: $$O(n^{3})$$\n\n# Code\n```\nclass Solution:\n\n def generateTrees(self, n: int) -> List[Optional[TreeNode]]:\n return self.__generate(lo=1, hi=n)\n\n @classmethod\n @cache\n def __generate(cls, lo: int, hi: int) -> list:\n if lo ... | 6 | 0 | ['Dynamic Programming', 'Python', 'Python3'] | 2 |
unique-binary-search-trees-ii | 12 ms || 97% faster cpp solution || Clean code | 12-ms-97-faster-cpp-solution-clean-code-7x1my | \nclass Solution {\npublic:\n vector<TreeNode*> helper(int start, int end){\n vector<TreeNode*> ans;\n if(start > end){\n ans.push_b | user0382o | NORMAL | 2021-09-30T15:05:24.209196+00:00 | 2021-09-30T15:05:24.209248+00:00 | 500 | false | ```\nclass Solution {\npublic:\n vector<TreeNode*> helper(int start, int end){\n vector<TreeNode*> ans;\n if(start > end){\n ans.push_back(NULL);\n return ans;\n }\n \n for(int i = start; i <= end; i++){\n auto left = helper(start, i - 1);\n ... | 6 | 0 | ['Binary Search', 'Tree', 'Recursion', 'C', 'C++'] | 1 |
unique-binary-search-trees-ii | Simple Java Solution with comments - 1 ms - recursive | simple-java-solution-with-comments-1-ms-v6atw | Algorithm:\n\n- Imagine the input as a continuous array of integers.\n- move in a sliding window fashion considering every element as possible root.\n- For ever | seekers01 | NORMAL | 2020-09-26T19:56:44.196138+00:00 | 2020-09-27T11:49:08.200157+00:00 | 601 | false | Algorithm:\n\n- Imagine the input as a continuous array of integers.\n- move in a sliding window fashion considering every element as possible root.\n- For every element chosen as root\n\t- everything in the array on the left will form the possible left subtrees\n\t- everything in the array on the right will form the p... | 6 | 0 | ['Recursion', 'Binary Tree', 'Java'] | 1 |
unique-binary-search-trees-ii | [C++] Simple recursive solution [Detailed Explanation] | c-simple-recursive-solution-detailed-exp-rrse | \n/*\n 95. Unique Binary Search Trees II\n https://leetcode.com/problems/unique-binary-search-trees-ii/\n*/\n\n/**\n * Definition for a binary tree node.\ | cryptx_ | NORMAL | 2020-03-02T10:59:31.622233+00:00 | 2020-03-02T10:59:31.622288+00:00 | 943 | false | ```\n/*\n 95. Unique Binary Search Trees II\n https://leetcode.com/problems/unique-binary-search-trees-ii/\n*/\n\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode(int x) : val(x), left(NULL), right(NULL) {}\n * };\n... | 6 | 0 | ['C', 'C++'] | 1 |
unique-binary-search-trees-ii | Two Python sol. based on DP and recursion with memorization. [ With explanation ] | two-python-sol-based-on-dp-and-recursion-3wdo | Two Python sol. based on DP and recursion with memorization\n\n------------------------------------------\n\nMethod_#1: bottom-up dynamic programming\n\n\n# Def | brianchiang_tw | NORMAL | 2020-01-14T12:17:19.215688+00:00 | 2020-05-15T06:58:31.223281+00:00 | 1,330 | false | Two Python sol. based on DP and recursion with memorization\n\n------------------------------------------\n\nMethod_#1: bottom-up dynamic programming\n\n```\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, x):\n# self.val = x\n# self.left = None\n# self.right = ... | 6 | 0 | ['Dynamic Programming', 'Recursion', 'Memoization', 'Python'] | 1 |
unique-binary-search-trees-ii | Divide and Conquer in Java / Scala | divide-and-conquer-in-java-scala-by-grac-rb8v | Divide into subproblems\nThe root candidate can be selected from [1, N].\n\n> Recursively solve subproblems\nAfter we decide a root x, two subtrees (i.e. subpro | gracemeng | NORMAL | 2018-08-26T16:52:37.882258+00:00 | 2018-08-29T09:15:49.075357+00:00 | 696 | false | > **Divide into subproblems**\nThe root candidate can be selected from `[1, N]`.\n\n> **Recursively solve subproblems**\nAfter we decide a root `x`, two subtrees (i.e. subproblems) generated. One contains nodes with values with the range `[1, x - 1]`, while the other contains nodes with values within the range `[x + 1,... | 6 | 0 | [] | 2 |
unique-binary-search-trees-ii | Python generator solution | python-generator-solution-by-google-ykhb | credit goes to https://leetcode.com/discuss/3440/help-simplify-my-code-the-second-one?show=4884#a4884\n\n # Definition for a binary tree node\n # class T | google | NORMAL | 2015-03-19T07:41:35+00:00 | 2015-03-19T07:41:35+00:00 | 1,587 | false | credit goes to https://leetcode.com/discuss/3440/help-simplify-my-code-the-second-one?show=4884#a4884\n\n # Definition for a binary tree node\n # class TreeNode:\n # def __init__(self, x):\n # self.val = x\n # self.left = None\n # self.right = None\n \n class Solutio... | 6 | 0 | ['Python'] | 2 |
unique-binary-search-trees-ii | Why is the expected result "[[]]" when the return type is just a List and not List of List? | why-is-the-expected-result-when-the-retu-c9st | Hi,\n\n8/9 test cases are passed except the following edge case:-\n\nInput : 0\nOutput : [ ]\nExpected : [ [ ] ]\n\nIt seems expected output is like a List of l | whitehat | NORMAL | 2015-06-26T05:45:46+00:00 | 2015-06-26T05:45:46+00:00 | 1,140 | false | Hi,\n\n8/9 test cases are passed except the following edge case:-\n\nInput : 0\nOutput : [ ]\nExpected : [ [ ] ]\n\nIt seems expected output is like a List of list. Can someone clarify what is going wrong.\n\nBelow is my java code :-\n\n public class Solution {\n public List<TreeNode> generateTrees(int n) {\n ... | 6 | 0 | [] | 2 |
unique-binary-search-trees-ii | Java concise recursive solution. 3ms | java-concise-recursive-solution-3ms-by-s-pcuh | public class Solution {\n\n public List generateTrees(int n) {\n return n > 0 ? helper(1, n) : new ArrayList();\n }\n \n private List helper( | samparly | NORMAL | 2016-03-29T15:20:23+00:00 | 2016-03-29T15:20:23+00:00 | 1,047 | false | public class Solution {\n\n public List<TreeNode> generateTrees(int n) {\n return n > 0 ? helper(1, n) : new ArrayList<TreeNode>();\n }\n \n private List<TreeNode> helper(int from, int n) {\n\t\tList<TreeNode> trees = new ArrayList<TreeNode>();\n\t\tfor (int i = 0; i <= n - 1; ++i) {\n\t\t\t//left i,... | 6 | 0 | ['Recursion', 'Java'] | 1 |
unique-binary-search-trees-ii | Java 2ms solution beats 92% | java-2ms-solution-beats-92-by-darksidech-x63h | /**\n * Definition for a binary tree node.\n * public class TreeNode {\n * int val;\n * TreeNode left;\n * TreeNode right;\n | darksidechris | NORMAL | 2016-01-22T04:46:12+00:00 | 2016-01-22T04:46:12+00:00 | 2,017 | false | /**\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 */\n public class Solution {\n public List<TreeNode> generateTrees(int n) {\n if(n=... | 6 | 2 | [] | 1 |
unique-binary-search-trees-ii | [Unique Binary Search Trees II] [C++] - Shallow Copy & Deep Copy | unique-binary-search-trees-ii-c-shallow-yf6ru | Shallow Copy - TreeNodes are shared between trees:\n\nclass Solution {\npublic:\n vector<TreeNode*> generateTrees(int n) {\n return n == 0 ? vector<Tr | alexander | NORMAL | 2017-01-02T23:43:44.381000+00:00 | 2017-01-02T23:43:44.381000+00:00 | 367 | false | **Shallow Copy** - TreeNodes are shared between trees:\n```\nclass Solution {\npublic:\n vector<TreeNode*> generateTrees(int n) {\n return n == 0 ? vector<TreeNode*>() : generate(1, n);\n }\n\n vector<TreeNode*> generate(int lo, int hi) {\n vector<TreeNode*> trees;\n if (lo > hi) {\n ... | 6 | 1 | [] | 0 |
unique-binary-search-trees-ii | 🔥🔥 Very Easy || Recursion + Memoization || DP || Java || 99% Faster 🔥🔥 | very-easy-recursion-memoization-dp-java-3w0gi | Approach\n- Try out all ways, i.e, Recursion\n- Memoize the recursion for repeating subproblem\n\n# Code\n\nclass Solution {\n public List<TreeNode> generate | khushdev20211 | NORMAL | 2023-08-05T07:03:19.662491+00:00 | 2023-08-06T02:55:52.861347+00:00 | 860 | false | # Approach\n- Try out all ways, i.e, Recursion\n- Memoize the recursion for repeating subproblem\n\n# Code\n```\nclass Solution {\n public List<TreeNode> generateTrees(int n) {\n List<TreeNode>[][] dp = new List[n + 1][n + 1];\n return f(1, n, dp);\n }\n private List<TreeNode> f(int low, int high... | 5 | 0 | ['Java'] | 0 |
unique-binary-search-trees-ii | C++ 🔥 | Detailed Explanation of the Recursive Approach | c-detailed-explanation-of-the-recursive-hqp8w | Intuition\n- The Trees can have root nodes from 1 to n\n- Left Subtree will have elements less than the root node\n- Right Subtree will have elements greater th | arsalAbbas | NORMAL | 2023-08-05T05:30:50.519766+00:00 | 2023-08-05T16:21:20.571490+00:00 | 418 | false | # Intuition\n- The Trees can have root nodes from 1 to n\n- Left Subtree will have elements less than the root node\n- Right Subtree will have elements greater than the root node\n- Each left and right subtree can be treated as an individual tree which can have previousRoot-1 (starting from prevousRoot-1 till the left ... | 5 | 0 | ['Tree', 'Binary Search Tree', 'Combinatorics', 'C++'] | 2 |
unique-binary-search-trees-ii | Easy Java Solution | easy-java-solution-by-viraj_patil_092-pvi8 | Code\n\nclass Solution {\n public List<TreeNode> generateTrees(int n) {\n\n return upvote(1,n);\n }\n\n private List<TreeNode> upvote(int s, int | Viraj_Patil_092 | NORMAL | 2023-08-05T04:33:47.842558+00:00 | 2023-08-05T04:33:47.842581+00:00 | 555 | false | # Code\n```\nclass Solution {\n public List<TreeNode> generateTrees(int n) {\n\n return upvote(1,n);\n }\n\n private List<TreeNode> upvote(int s, int e){\n List<TreeNode> ans = new ArrayList<>();\n\n if(s>e){\n ans.add(null);\n return ans;\n }\n\n for(in... | 5 | 0 | ['Dynamic Programming', 'Tree', 'Binary Search Tree', 'Binary Tree', 'Java'] | 0 |
unique-binary-search-trees-ii | Java | Memoization | Beats 99% on space and time | 15 lines | java-memoization-beats-99-on-space-and-t-h609 | 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-04-10T03:32:39.775436+00:00 | 2023-04-10T20:07:59.082241+00:00 | 1,319 | 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^2)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n^2)$$\n<!-- Add your space complexity ... | 5 | 0 | ['Dynamic Programming', 'Binary Search Tree', 'Recursion', 'Java'] | 1 |
unique-binary-search-trees-ii | C++ Solution | c-solution-by-pranto1209-1gho | Code\n\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : | pranto1209 | NORMAL | 2023-01-02T13:27:46.389929+00:00 | 2023-03-14T08:19:40.042820+00:00 | 1,736 | false | # Code\n```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod... | 5 | 0 | ['C++'] | 0 |
unique-binary-search-trees-ii | Java fast clean concise memoized recursion explained - 0ms/100% | java-fast-clean-concise-memoized-recursi-znpt | Intuition We can break the problem into a subproblem by asking what are all of structures for a given root value. If we do this, then by virtue of the definiti | mattihito | NORMAL | 2022-04-10T19:13:31.497352+00:00 | 2022-04-10T19:18:52.391503+00:00 | 419 | false | **Intuition** We can break the problem into a subproblem by asking what are all of structures for a given root value. If we do this, then by virtue of the definition of a binary search tree, all the values less than our root value will be within a left subtree, and al the values greater than our root value will be wit... | 5 | 0 | ['Depth-First Search', 'Recursion', 'Memoization', 'Java'] | 0 |
unique-binary-search-trees-ii | C solution | c-solution-by-linhbk93-96lp | \nstruct TreeNode** creatTrees(int low, int high, int *size)\n{ \n if(low > high)\n {\n struct TreeNode ** out = (struct TreeNode **)malloc(sizeof( | linhbk93 | NORMAL | 2021-09-03T04:48:07.570166+00:00 | 2021-09-03T04:48:07.570202+00:00 | 428 | false | ```\nstruct TreeNode** creatTrees(int low, int high, int *size)\n{ \n if(low > high)\n {\n struct TreeNode ** out = (struct TreeNode **)malloc(sizeof(struct TreeNode*));\n *size = 1;\n out[0] = NULL;\n return out;\n }\n struct TreeNode ** out = (struct TreeNode **)malloc(2000*siz... | 5 | 0 | ['Recursion', 'C'] | 0 |
unique-binary-search-trees-ii | java solution explained TC : 92%, better variable naming | java-solution-explained-tc-92-better-var-7wpc | Basic Approach\nwe create another function called generateBSTs which takes two parameters, the strart node value and the end node value\n1. If the start node va | lahari_bitra | NORMAL | 2021-09-02T17:27:11.660986+00:00 | 2021-09-03T08:51:48.414185+00:00 | 680 | false | # Basic Approach\nwe create another function called generateBSTs which takes two parameters, the strart node value and the end node value\n1. If the start node value is greater than the end node then add the null to the currentBST list and return it\n2. for every node from start to end, make it as our root node value d... | 5 | 0 | ['Recursion', 'Java'] | 1 |
unique-binary-search-trees-ii | Simple Solution with detailed Explanation and comments | simple-solution-with-detailed-explanatio-esed | Intuition:\nThe idea is to generate trees in inorder fashion , making each number in the range[1.n] as the root one by one and recursively obtaining its left an | priyankgarg02 | NORMAL | 2021-09-02T15:54:57.631740+00:00 | 2021-09-02T15:54:57.631768+00:00 | 213 | false | **Intuition:**\nThe idea is to generate trees in inorder fashion , making each number in the range[1.n] as the root one by one and recursively obtaining its left and right subtrees ensuring that BST properties doesnt gets violated.\nIf the Root Value is i\n* Left subtree will contain values in range[start,i-1]\n* right... | 5 | 1 | ['Dynamic Programming', 'Tree', 'Recursion', 'C++'] | 0 |
unique-binary-search-trees-ii | C++ Easy to understand Recursive Approach with comments :) | c-easy-to-understand-recursive-approach-5m0mk | Hi,\n\nIf this helps please do UPVOTE or if you have any query or doubt COMMENT it down.\n\nclass Solution {\npublic:\n \n\t// function that takes sub parame | drig | NORMAL | 2021-07-08T14:51:09.944875+00:00 | 2021-07-08T14:51:09.944920+00:00 | 591 | false | Hi,\n\n**If this helps please do UPVOTE or if you have any query or doubt COMMENT it down.**\n```\nclass Solution {\npublic:\n \n\t// function that takes sub parameters left and right value\n vector<TreeNode*> solve(int l,int r){\n vector<TreeNode*> ans;\n \n\t\t// if l>r just return null\n i... | 5 | 1 | ['Tree', 'Recursion', 'C', 'C++'] | 1 |
unique-binary-search-trees-ii | C++; Simple and Elegant | c-simple-and-elegant-by-pranavkumarkunal-9c0f | Runtime: 8 ms, faster than 99.46% of C++ online submissions for Unique Binary Search Trees II.\nMemory Usage: 14 MB, less than 63.64% of C++ online submissions | pranavkumarkunal | NORMAL | 2020-08-23T04:41:24.059224+00:00 | 2020-08-23T04:41:24.059279+00:00 | 393 | false | **Runtime: 8 ms, faster than 99.46% of C++ online submissions for Unique Binary Search Trees II.\nMemory Usage: 14 MB, less than 63.64% of C++ online submissions for Unique Binary Search Trees II.**\n\n```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * ... | 5 | 0 | [] | 1 |
unique-binary-search-trees-ii | C++ #miminalizm | c-miminalizm-by-votrubac-i1d9 | cpp\nvector<TreeNode*> generateTrees(int end, int start = 0) {\n if (start > end)\n return { nullptr };\n vector<TreeNode*> res;\n for (auto i = | votrubac | NORMAL | 2020-03-25T23:58:48.602040+00:00 | 2020-03-25T23:59:41.897541+00:00 | 343 | false | ```cpp\nvector<TreeNode*> generateTrees(int end, int start = 0) {\n if (start > end)\n return { nullptr };\n vector<TreeNode*> res;\n for (auto i = max(1, start); i <= end; ++i) {\n for (auto l : generateTrees(i - 1, max(1, start)))\n for (auto r : generateTrees(end, i + 1)) {\n ... | 5 | 0 | [] | 1 |
unique-binary-search-trees-ii | cpp easy to understand recursive solution with explanation | cpp-easy-to-understand-recursive-solutio-x7bh | Explanation :\nEvery element in the array[1,n] can be a root sometime, because of the BST property, \nif ith element is chosen as the root, then the elements fr | fight_club | NORMAL | 2019-08-04T05:10:25.962325+00:00 | 2019-08-04T05:11:41.508652+00:00 | 261 | false | Explanation :\nEvery element in the array[1,n] can be a root sometime, because of the BST property, \nif ith element is chosen as the root, then the elements from [1,i-1] will form the left subtree & [i+1,n] will form the right subtree.\nWhat we ask recursion to do is , for each ith element , return all possible combin... | 5 | 0 | [] | 0 |
unique-binary-search-trees-ii | C++ iterative DP solution, reuse tree nodes (both 100%) | c-iterative-dp-solution-reuse-tree-nodes-5v6r | Runtime: 16 ms, faster than 100.00% of C++ online submissions for Unique Binary Search Trees II.\nMemory Usage: 11.6 MB, less than 100.00% of C++ online submiss | alreadydone | NORMAL | 2019-03-27T06:37:11.849068+00:00 | 2019-03-27T06:37:11.850113+00:00 | 484 | false | Runtime: 16 ms, faster than 100.00% of C++ online submissions for Unique Binary Search Trees II.\nMemory Usage: 11.6 MB, less than 100.00% of C++ online submissions for Unique Binary Search Trees II.\n\nBTW, I think {NULL} should be returned in the case n = 0, but I have to return {} to pass the test case; I think this... | 5 | 0 | [] | 1 |
unique-binary-search-trees-ii | My non-recursive C++ solution | my-non-recursive-c-solution-by-roddykou-pf2s | vector<TreeNode *> generateTrees(int n) {\n vector<TreeNode *> tmp;\n vector<TreeNode *> ret;\n tmp.push_back(NULL); \n ret.p | roddykou | NORMAL | 2015-04-01T05:28:48+00:00 | 2015-04-01T05:28:48+00:00 | 1,040 | false | vector<TreeNode *> generateTrees(int n) {\n vector<TreeNode *> tmp;\n vector<TreeNode *> ret;\n tmp.push_back(NULL); \n ret.push_back(new TreeNode(1));\n if (!n) return tmp;\n\n\t\t/* insert the largeset number into previously contructed trees */\n for (int i = 2; i ... | 5 | 0 | [] | 0 |
unique-binary-search-trees-ii | Possibly the worst C++ solution ever! 💣💥 | No DP | Pure Backtracking | Bruteforce of Bruteforce | possibly-the-worst-c-solution-ever-no-dp-cl54 | Intuition \n\n- ###### Generate all possible permutations of numbers from 1 to n and construct a binary search tree (BST) for each permutation.\n - for exampl | quagmire8 | NORMAL | 2024-03-31T04:10:08.110413+00:00 | 2024-03-31T04:10:08.110438+00:00 | 41 | false | # Intuition \n\n- ###### Generate all possible permutations of numbers from 1 to n and construct a binary search tree (BST) for each permutation.\n - for example we have <1,2,3>\n - All possible permutations for 1,2,3 will be :\n ```\n # In total there will be 3! factorial permuatations\n ... | 4 | 0 | ['Backtracking', 'Tree', 'Binary Search Tree', 'Binary Tree', 'C++'] | 0 |
unique-binary-search-trees-ii | Recusive solution with and without memoization in c++ 💯💯🔥🔥✅✅ | recusive-solution-with-and-without-memoi-sa9u | Intuition\n Describe your first thoughts on how to solve this problem. \n The recursive approach explores all possible combinations of left and right subtrees | pahadi_rawat | NORMAL | 2024-02-14T10:06:47.163650+00:00 | 2024-02-14T10:07:15.032115+00:00 | 1,071 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n The recursive approach explores all possible combinations of left and right subtrees for each root value within the given range.\n\n The DP table is used to store and retrieve already computed results, avoiding redundant calculations... | 4 | 0 | ['Dynamic Programming', 'Backtracking', 'Tree', 'Binary Search Tree', 'Recursion', 'Binary Tree', 'C++'] | 2 |
unique-binary-search-trees-ii | Recursion || Easy Solution || Using Left and right pointers | recursion-easy-solution-using-left-and-r-l1eu | Intuition\n Describe your first thoughts on how to solve this problem. \nBy using start and end variables to generate all the trees.\n\n# Approach\n Describe yo | M_S_L | NORMAL | 2023-08-05T05:09:49.716959+00:00 | 2023-08-05T05:09:49.716981+00:00 | 20 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nBy using start and end variables to generate all the trees.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nNow let\'s look how our helper function will work!\n\n1. As there will be trees with root as 1, 2, 3...n. ... | 4 | 0 | ['Recursion', 'C++'] | 0 |
unique-binary-search-trees-ii | 🔥🔥C++ Solution || ✅Recursion✅|| SuperEasy Explanation🔥🔥 | c-solution-recursion-supereasy-explanati-ol78 | Approach\n Describe your approach to solving the problem. \n1. The function generateTrees takes two parameters: n, which represents the number of nodes to be us | aa98-45556443355666 | NORMAL | 2023-08-05T04:38:11.509444+00:00 | 2023-08-05T09:22:37.242983+00:00 | 340 | false | # Approach\n<!-- Describe your approach to solving the problem. -->\n1. The function `generateTrees` takes two parameters: `n`, which represents the number of nodes to be used for constructing BSTs, and `start`, which represents the starting value of the node range.\n\n2. The function uses recursion to generate all pos... | 4 | 0 | ['Tree', 'Binary Search Tree', 'Recursion', 'Binary Tree', 'C++'] | 0 |
unique-binary-search-trees-ii | Python3 Solution | python3-solution-by-motaharozzaman1996-qchz | \n\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.l | Motaharozzaman1996 | NORMAL | 2023-08-05T04:18:25.848909+00:00 | 2023-08-05T04:18:25.848926+00:00 | 975 | false | \n```\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 generateTrees(self, n: int) -> List[Optional[TreeNode]]:\n @lru_cache\n def ... | 4 | 0 | ['Python', 'Python3'] | 1 |
unique-binary-search-trees-ii | Most concise one to beat 99% | memoization | Java | most-concise-one-to-beat-99-memoization-j1ru9 | Recursive to memoization\n\nsee recursive implementation here.\nThanks to @ArpAry.\n\n\nclass Solution {\n public List<TreeNode> generateTrees(int n) {\n | Nagaraj_Poojari | NORMAL | 2023-06-04T15:17:37.050038+00:00 | 2023-06-04T15:17:37.050087+00:00 | 2,297 | false | #### Recursive to memoization\n\nsee recursive implementation [here](https://leetcode.com/problems/unique-binary-search-trees-ii/solutions/3477600/95-unique-binary-search-trees-ii-java/?envType=study-plan-v2&envId=dynamic-programming).\nThanks to [@ArpAry](https://leetcode.com/ArpAry/).\n\n```\nclass Solution {\n pu... | 4 | 0 | ['Recursion', 'Memoization', 'Java'] | 0 |
unique-binary-search-trees-ii | C# Divide and conquer, easy to understand | c-divide-and-conquer-easy-to-understand-q5p6x | Algorithm\n1. Pick a number i from 1 .. n\n2. Use it as the root of the current tree\n3. Solve 2 subproblems 1 .. i - 1 and i + 1 .. n\n4. Repeat for every i fr | maskalek | NORMAL | 2023-03-02T06:39:40.512237+00:00 | 2023-03-02T06:39:40.512284+00:00 | 307 | false | # Algorithm\n1. Pick a number `i` from `1 .. n`\n2. Use it as the root of the current tree\n3. Solve 2 subproblems `1 .. i - 1` and `i + 1 .. n`\n4. Repeat for every `i` from `1 .. n`\n\n# Code\n```\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * public int val;\n * public TreeNode l... | 4 | 0 | ['Divide and Conquer', 'C#'] | 1 |
unique-binary-search-trees-ii | c++ short solution || Recursion | c-short-solution-recursion-by-shristha-2cz9 | \n\n# Code\n\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNod | Shristha | NORMAL | 2023-01-27T10:43:03.846198+00:00 | 2023-01-27T10:43:03.846229+00:00 | 2,835 | false | \n\n# Code\n```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, Tre... | 4 | 0 | ['Recursion', 'C++'] | 1 |
unique-binary-search-trees-ii | C++ recursion very fast (Minimum Line) | c-recursion-very-fast-minimum-line-by-ma-uoc0 | \t\tclass Solution {\n\t\tpublic:\n\t\t\tvector generateTrees(int n, int s = 1) {\n\t\t\t\tvector ans;\n\t\t\t\tif(n < s) return {nullptr}; | manishx112 | NORMAL | 2022-09-22T17:22:27.214317+00:00 | 2022-09-22T17:22:27.214359+00:00 | 2,061 | false | \t\tclass Solution {\n\t\tpublic:\n\t\t\tvector<TreeNode*> generateTrees(int n, int s = 1) {\n\t\t\t\tvector<TreeNode*> ans;\n\t\t\t\tif(n < s) return {nullptr}; \n\t\t\t\t for(int i=s; i<=n; i++) { \t // Consider every number in range [s,n] as root \n\t\t\t\... | 4 | 0 | ['Dynamic Programming', 'Recursion', 'C', 'C++'] | 1 |
unique-binary-search-trees-ii | "Trust Me boys and girls "-By Recursion | trust-me-boys-and-girls-by-recursion-by-xl6u1 | Solution -->\n\nclass Solution {\npublic:\n \n vector<TreeNode*> generateBST(int begin, int end)\n {\n vector<TreeNode*> arr;\n \n | shubhaamtiwary_01 | NORMAL | 2022-08-16T16:32:19.579016+00:00 | 2022-08-16T16:32:19.579057+00:00 | 260 | false | **Solution -->**\n```\nclass Solution {\npublic:\n \n vector<TreeNode*> generateBST(int begin, int end)\n {\n vector<TreeNode*> arr;\n \n if(begin>end)\n {\n arr.push_back(NULL);\n return arr;\n }\n \n for(int i=begin; i<=end; i++)\n ... | 4 | 0 | ['Recursion', 'C'] | 0 |
unique-binary-search-trees-ii | JAVA || RECURSION || BEGINNER FRIENDLY | java-recursion-beginner-friendly-by-madd-08fy | \nclass Solution {\n public List<TreeNode> generateTrees(int n) {\n return solve(1,n) ;\n }\n \n public List<TreeNode> solve(int left,int rig | maddy310 | NORMAL | 2022-07-13T10:13:07.127136+00:00 | 2022-07-13T10:13:07.127164+00:00 | 710 | false | ```\nclass Solution {\n public List<TreeNode> generateTrees(int n) {\n return solve(1,n) ;\n }\n \n public List<TreeNode> solve(int left,int right){\n List<TreeNode> ans = new ArrayList<>() ;\n if(left > right){\n ans.add(null) ;\n return ans ;\n }\n ... | 4 | 0 | ['Recursion', 'Java'] | 0 |
unique-binary-search-trees-ii | easy understandable recursion solution (with comments) | easy-understandable-recursion-solution-w-u1sv | \nclass Solution {\npublic:\n vector<TreeNode*> generate(int start,int end){\n vector<TreeNode*> ans; // to store subtree for current node.\n | ravishankarnitr | NORMAL | 2022-05-26T05:55:05.877271+00:00 | 2022-05-26T05:55:05.877304+00:00 | 286 | false | ```\nclass Solution {\npublic:\n vector<TreeNode*> generate(int start,int end){\n vector<TreeNode*> ans; // to store subtree for current node.\n if(start>end){ // means null\n ans.push_back(NULL);\n return ans;\n }\n \n for(int i=start;i<=end;i++){\n ... | 4 | 0 | ['Recursion', 'C'] | 0 |
longest-subarray-of-1s-after-deleting-one-element | [Java/C++/Python] Sliding Window, at most one 0 | javacpython-sliding-window-at-most-one-0-2f0m | Intuition\nAlmost exactly same as 1004. Max Consecutive Ones III\nGiven an array A of 0s and 1s,\nwe may change up to k = 1 values from 0 to 1.\nReturn the leng | lee215 | NORMAL | 2020-06-27T16:03:01.985512+00:00 | 2020-06-27T16:03:01.985559+00:00 | 35,727 | false | # **Intuition**\nAlmost exactly same as 1004. [Max Consecutive Ones III](https://leetcode.com/problems/max-consecutive-ones-iii/discuss/247564/JavaC%2B%2BPython-Sliding-Window)\nGiven an array A of 0s and 1s,\nwe may change up to k = 1 values from 0 to 1.\nReturn the **length - 1** of the longest (contiguous) subarray ... | 374 | 15 | [] | 63 |
longest-subarray-of-1s-after-deleting-one-element | ✅Beat's 100% || C++ || JAVA || PYTHON || Beginner Friendly🔥🔥🔥 | beats-100-c-java-python-beginner-friendl-mher | Intuition:\nThe Intuition is to use a sliding window approach to find the length of the longest subarray of 1\'s after removing at most one element (0 or 1) fro | rahulvarma5297 | NORMAL | 2023-07-05T00:55:52.543264+00:00 | 2023-07-05T09:02:15.773696+00:00 | 44,562 | false | # Intuition:\nThe Intuition is to use a sliding window approach to find the length of the longest subarray of 1\'s after removing at most one element (0 or 1) from the original array. It adjusts the window to have at most one zero, calculates the subarray length, and returns the maximum length found.\n\n# Explanation:\... | 315 | 0 | ['Sliding Window', 'C++', 'Java', 'Python3'] | 16 |
longest-subarray-of-1s-after-deleting-one-element | Java O(n) time, O(1) space, no sliding window | java-on-time-o1-space-no-sliding-window-ibhkf | Just calculate the max sum of counts of prev Ones and after Ones for each non-ones;\nprevCnt: counts of prev Ones\ncnt: counts of Ones after.\npitfalls:\nif al | hobiter | NORMAL | 2020-06-27T18:22:33.087305+00:00 | 2020-06-28T23:49:47.373746+00:00 | 6,976 | false | Just calculate the max sum of counts of prev Ones and after Ones for each non-ones;\nprevCnt: counts of prev Ones\ncnt: counts of Ones after.\npitfalls:\nif all ones, must remove one;\n\nO(n) time, O(1) space, concise and straightforward\n\n```\n public int longestSubarray(int[] nums) {\n int prevCnt = 0, cn... | 126 | 1 | [] | 17 |
longest-subarray-of-1s-after-deleting-one-element | [Python] O(n) dynamic programming, detailed explanation | python-on-dynamic-programming-detailed-e-e252 | One way to deal this problem is dynamic programming. Let dp[i][0] be the length of longest subarray of ones, such that it ends at point i. Let dp[i][1] be the l | dbabichev | NORMAL | 2020-06-27T16:02:50.972351+00:00 | 2020-06-27T16:02:50.972413+00:00 | 6,726 | false | One way to deal this problem is **dynamic programming**. Let `dp[i][0]` be the length of longest subarray of ones, such that it ends at point `i`. Let `dp[i][1]` be the length longest subarray, which has one **zero** and ends at point `i`. Let us traverse through our `nums` and update our `dp` table, we have two option... | 121 | 4 | ['Dynamic Programming'] | 10 |
longest-subarray-of-1s-after-deleting-one-element | [Java/Python 3] Sliding Window with at most one zero w/ detailed explanation and brief analysis. | javapython-3-sliding-window-with-at-most-fd8l | Two methods.\n\n----\nMethod 1:\n1. Use i and j as the lower and upper bounds, both inclusively;\n2. Loop through the nums by upper bound, accumulate current el | rock | NORMAL | 2020-06-27T16:10:56.221473+00:00 | 2023-01-18T13:55:19.749290+00:00 | 7,838 | false | Two methods.\n\n----\n**Method 1:**\n1. Use `i` and `j` as the lower and upper bounds, both inclusively;\n2. Loop through the `nums` by upper bound, accumulate current element on the `sum`, then check if `sum` is less than `j - i` (which **implies the window includes at least 2 `0`s**) : if yes, keep increase the lower... | 57 | 1 | ['Sliding Window', 'Java', 'Python3'] | 6 |
longest-subarray-of-1s-after-deleting-one-element | Python, Java | Elegant & Short | One pass | O(n) time | O(1) memory | python-java-elegant-short-one-pass-on-ti-4ftg | Python:\n\n\ndef longestSubarray(self, nums: List[int]) -> int:\n\tlongest = prev = curr = 0\n\n\tfor bit in nums:\n\t\tif bit:\n\t\t\tcurr += 1\n\t\t\tlongest | Kyrylo-Ktl | NORMAL | 2022-09-22T16:07:49.757109+00:00 | 2022-09-30T13:17:04.160094+00:00 | 4,945 | false | ## Python:\n\n```\ndef longestSubarray(self, nums: List[int]) -> int:\n\tlongest = prev = curr = 0\n\n\tfor bit in nums:\n\t\tif bit:\n\t\t\tcurr += 1\n\t\t\tlongest = max(longest, prev + curr)\n\t\telse:\n\t\t\tprev, curr = curr, 0\n\n\treturn longest - (longest == len(nums))\n```\n\n## Java:\n\n```\npublic int longes... | 48 | 0 | ['Python', 'Java', 'Python3'] | 3 |
longest-subarray-of-1s-after-deleting-one-element | 100% fast | C++ | Java | Python | Easy Line by Line Explanation + Video Explanation | 100-fast-c-java-python-easy-line-by-line-ni2e | \n# Intuition\n Describe your first thoughts on how to solve this problem. \nHere we can figure out that max one subarray will be formed when we \ndelete any 0 | shivam-727 | NORMAL | 2023-07-05T04:02:06.470422+00:00 | 2023-08-15T20:18:21.949337+00:00 | 6,169 | false | \n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nHere we can figure out that max one subarray will be formed when we \ndelete any 0 from array\n\nFor detailed explanation you can refer to my youtube channel (Hindi Language)\n https://youtu.be/RXWLIqpDosY\n or link in my profile.Here,... | 25 | 0 | ['Array', 'Sliding Window', 'Python', 'C++', 'Java'] | 2 |
longest-subarray-of-1s-after-deleting-one-element | Sliding Window | sliding-window-by-votrubac-dokl | C++\ncpp\nint longestSubarray(vector<int>& nums) {\n int max_sz = 0;\n for (int i = 0, j = 0, skip = 0; i < nums.size(); ++i) {\n skip += nums[i] = | votrubac | NORMAL | 2020-06-27T16:02:51.343711+00:00 | 2022-04-01T13:55:43.826183+00:00 | 3,160 | false | **C++**\n```cpp\nint longestSubarray(vector<int>& nums) {\n int max_sz = 0;\n for (int i = 0, j = 0, skip = 0; i < nums.size(); ++i) {\n skip += nums[i] == 0;\n if (skip > 1)\n skip -= nums[j++] == 0;\n max_sz = max(max_sz, i - j);\n }\n return max_sz;\n}\n``` | 24 | 3 | ['C'] | 1 |
longest-subarray-of-1s-after-deleting-one-element | 🏆C++ || Sliding Window EASY | c-sliding-window-easy-by-chiikuu-u7h9 | Code\n\nclass Solution {\npublic:\n int longestSubarray(vector<int>& nums) {\n int l=0,r=0;\n int mx=0,count=0,total=0;\n while(r<nums.s | CHIIKUU | NORMAL | 2023-07-05T05:01:23.456344+00:00 | 2023-07-05T05:01:23.456380+00:00 | 7,079 | false | # Code\n```\nclass Solution {\npublic:\n int longestSubarray(vector<int>& nums) {\n int l=0,r=0;\n int mx=0,count=0,total=0;\n while(r<nums.size()){\n if(nums[r]==0){\n count++;\n while(count>1){\n if(nums[l]==0)count--;\n ... | 23 | 0 | ['C++'] | 1 |
longest-subarray-of-1s-after-deleting-one-element | Just a clever sliding window | just-a-clever-sliding-window-by-slowbutf-xjgk | \nclass Solution {\npublic:\n int longestSubarray(vector<int>& nums) {\n int zero=0;\n int i=0,j=0,res=0;\n while(j<nums.size()){\n | slowbutfast | NORMAL | 2020-06-27T18:01:27.791675+00:00 | 2020-06-27T18:01:27.791712+00:00 | 2,680 | false | ```\nclass Solution {\npublic:\n int longestSubarray(vector<int>& nums) {\n int zero=0;\n int i=0,j=0,res=0;\n while(j<nums.size()){\n if(nums[j]==0)\n zero++;\n if(zero>1){\n while(nums[i++]!=0);\n zero--;\n }\n ... | 22 | 1 | [] | 8 |
longest-subarray-of-1s-after-deleting-one-element | [Python] Sliding window solution explained | python-sliding-window-solution-explained-myxc | \nclass Solution:\n def longestSubarray(self, nums: List[int]) -> int:\n res = 0\n k = 1 # max zeroes we can delete\n l = 0\n \n | buccatini | NORMAL | 2022-02-06T04:35:06.205705+00:00 | 2022-02-06T04:35:06.205736+00:00 | 2,384 | false | ```\nclass Solution:\n def longestSubarray(self, nums: List[int]) -> int:\n res = 0\n k = 1 # max zeroes we can delete\n l = 0\n \n for r in range(len(nums)):\n # if we encounter a zero, decrement\n # the num zeroes we can now delete\n if nums[r] ==... | 20 | 0 | ['Sliding Window', 'Python', 'Python3'] | 2 |
longest-subarray-of-1s-after-deleting-one-element | [Easy Solution without DP] Simple Pictorial Explanation | Python Solution. | easy-solution-without-dp-simple-pictoria-w6av | Explanation\n\n\n\nCode\n\nclass Solution:\n def longestSubarray(self, nums: List[int]) -> int:\n m=0\n l=len(nums)\n one=True\n | lazerx | NORMAL | 2020-06-27T16:03:20.719690+00:00 | 2022-01-09T18:06:48.690321+00:00 | 1,982 | false | **Explanation**\n\n\n\n**Code**\n```\nclass Solution:\n def longestSubarray(self, nums: List[int]) -> int:\n m=0\n l=len(nums)\n one=True\n for i in range(0,l):\n if nu... | 18 | 5 | ['Python', 'Python3'] | 5 |
longest-subarray-of-1s-after-deleting-one-element | Beats 100% + Video | Java C++ Pyhton | beats-100-video-java-c-pyhton-by-jeevank-b3gf | \n\n\nclass Solution {\n public int longestSubarray(int[] nums) {\n int prev = 0; int curr = 0;\n int ans = 0;\n for(int i: nums){\n | jeevankumar159 | NORMAL | 2023-07-05T01:35:33.800157+00:00 | 2023-07-05T01:37:59.288800+00:00 | 2,703 | false | <iframe width="560" height="315" src="https://www.youtube.com/embed/jhBrybXSFTs" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>\n\n```\nclass Solution {\n public int longestSubarray(int... | 16 | 0 | ['C', 'Python', 'Java'] | 3 |
longest-subarray-of-1s-after-deleting-one-element | 🔥[Python3] Sliding windows with shrinking and not shrinking size | python3-sliding-windows-with-shrinking-a-ncly | The window size is r - l + 1, but because here in the window we should remove 1 symbol (it can be 1 or 0) the window size will be r - l. \nThe condition prefix | yourick | NORMAL | 2023-07-05T05:30:52.027135+00:00 | 2023-12-04T16:13:21.733027+00:00 | 1,828 | false | The window size is `r - l + 1`, but because here in the window we should remove 1 symbol (it can be `1` or `0`) the window size will be `r - l`. \nThe condition `prefix < r - l` means that in the window more than one `0`.\n\n###### Window can be invalid, shrink not more than 1 symbol for every iteration if window is i... | 15 | 0 | ['Sliding Window', 'Python', 'Python3'] | 0 |
longest-subarray-of-1s-after-deleting-one-element | c++ easy solution using simple for loop | c-easy-solution-using-simple-for-loop-by-hgy7 | \nclass Solution {\npublic:\n int longestSubarray(vector<int>& nums) {\n int a=0,b=0,c=0;\n for(int i=0;i<nums.size();++i){\n if(num | rknimkande1781 | NORMAL | 2022-08-25T19:36:06.345222+00:00 | 2022-08-25T19:36:06.345252+00:00 | 1,832 | false | ```\nclass Solution {\npublic:\n int longestSubarray(vector<int>& nums) {\n int a=0,b=0,c=0;\n for(int i=0;i<nums.size();++i){\n if(nums[i]==1){\n a++;\n }else{\n b=a;\n a=0;\n }\n c=max(c,b+a);\n }\n ... | 15 | 1 | ['C', 'C++'] | 4 |
longest-subarray-of-1s-after-deleting-one-element | Concise Clean Code! Different Solution | Similar Pattern | concise-clean-code-different-solution-si-0dx9 | Status: Accepted\nExplanation:\n toLeft[i] Stores the consecutive ones which is present left of i\n toRight[i] Stores the consecutive ones which is present righ | applefanboi | NORMAL | 2020-06-27T16:01:23.289206+00:00 | 2020-06-30T13:08:10.130644+00:00 | 1,355 | false | **Status**: Accepted\n**Explanation**:\n* toLeft[i] Stores the consecutive ones which is present left of i\n* toRight[i] Stores the consecutive ones which is present right of i\n* If we delete a element the ones in the subarray must be consecutive ones to the right and left.\n\n**Similar Problem:** [Maximum Subarray Su... | 14 | 2 | ['C', 'C++'] | 0 |
longest-subarray-of-1s-after-deleting-one-element | Easy Python Code | easy-python-code-by-hakkiot-ubvm | 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 | hakkiot | NORMAL | 2023-07-05T02:36:39.327797+00:00 | 2023-07-05T02:36:39.327814+00:00 | 1,471 | 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)$$ --... | 13 | 0 | ['Python3'] | 1 |
longest-subarray-of-1s-after-deleting-one-element | C++ | using iteration | | c-using-iteration-by-ashish_madhup-lfna | Intuition\n Describe your first thoughts on how to solve this problem. Upon analyzing the code, it appears that the objective of the longestSubarray function is | ashish_madhup | NORMAL | 2023-07-05T04:42:51.288125+00:00 | 2023-07-05T04:42:51.288157+00:00 | 1,775 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->Upon analyzing the code, it appears that the objective of the `longestSubarray` function is to find the length of the longest subarray in a given vector of integers (`a`). The subarray should satisfy the condition that it can be obtained by... | 12 | 0 | ['Array', 'Dynamic Programming', 'Sliding Window', 'C++'] | 1 |
longest-subarray-of-1s-after-deleting-one-element | [BEATS 95%] 🔥BEGINNER FRIENDLY | ONE PASS, SIMPLE ROLLING SUM | beats-95-beginner-friendly-one-pass-simp-pumz | Intuition/Approach\n Describe your first thoughts on how to solve this problem. \nIf the array has zeros, then we want to delete the zero that has the most cons | aaronlw | NORMAL | 2023-07-05T00:47:05.442823+00:00 | 2023-07-05T01:00:48.801385+00:00 | 1,965 | false | # Intuition/Approach\n<!-- Describe your first thoughts on how to solve this problem. -->\nIf the array has zeros, then we want to delete the zero that has the most consecutive ones surrounding both sides of it. We use prevSum and curSum to track the number of consecutive ones to the left and right of each zero. At the... | 12 | 0 | ['Python3'] | 6 |
longest-subarray-of-1s-after-deleting-one-element | Simple java solution | simple-java-solution-by-siddhant_1602-vj36 | Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(n)\n\n# Code\n\nclass Solution {\n public int longestSubarray(int[] nums) {\n int count=0 | Siddhant_1602 | NORMAL | 2023-07-05T04:12:56.583190+00:00 | 2023-07-05T04:12:56.583220+00:00 | 4,461 | false | # Complexity\n- Time complexity: $$O(n)$$\n\n- Space complexity: $$O(n)$$\n\n# Code\n```\nclass Solution {\n public int longestSubarray(int[] nums) {\n int count=0;\n List<Integer> nm=new ArrayList<>();\n for(int i=0;i<nums.length;i++)\n {\n if(nums[i]==0)\n {\n ... | 11 | 0 | ['Sliding Window', 'Java'] | 1 |
longest-subarray-of-1s-after-deleting-one-element | EASIEST C++ CODE (HIGHLY UNDERSTANDABLE) | easiest-c-code-highly-understandable-by-f462m | 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 | baibhavsingh07 | NORMAL | 2023-07-05T03:40:41.228372+00:00 | 2023-07-05T03:40:41.228395+00:00 | 2,362 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(N)\n<!-- Add your space complexity here, e.g. $$O... | 11 | 1 | ['Array', 'Sliding Window', 'C++'] | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.