title stringlengths 1 100 | titleSlug stringlengths 3 77 | Java int64 0 1 | Python3 int64 1 1 | content stringlengths 28 44.4k | voteCount int64 0 3.67k | question_content stringlengths 65 5k | question_hints stringclasses 970
values |
|---|---|---|---|---|---|---|---|
Python Solution O(n^2) Solution Beats 94% | construct-quad-tree | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThink of how you would do divide and conquer on an array if we had to divide the array in half everytime we do a recursion.\n\nWe will typically have a start index and number of elements from the start. \nFor example if our array is like ... | 1 | Implement a basic calculator to evaluate a simple expression string.
The expression string contains only non-negative integers, `'+'`, `'-'`, `'*'`, `'/'` operators, and open `'('` and closing parentheses `')'`. The integer division should **truncate toward zero**.
You may assume that the given expression is always v... | null |
Простое решение на Петухоне | construct-quad-tree | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | Given a `n * n` matrix `grid` of `0's` and `1's` only. We want to represent `grid` with a Quad-Tree.
Return _the root of the Quad-Tree representing_ `grid`.
A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:
* `val`: True if the node r... | null |
Простое решение на Петухоне | construct-quad-tree | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | Implement a basic calculator to evaluate a simple expression string.
The expression string contains only non-negative integers, `'+'`, `'-'`, `'*'`, `'/'` operators, and open `'('` and closing parentheses `')'`. The integer division should **truncate toward zero**.
You may assume that the given expression is always v... | null |
Python || Dividing matrix recursively | construct-quad-tree | 0 | 1 | # Code\n```\n"""\n# Definition for a QuadTree node.\nclass Node:\n def __init__(self, val, isLeaf, topLeft, topRight, bottomLeft, bottomRight):\n self.val = val\n self.isLeaf = isLeaf\n self.topLeft = topLeft\n self.topRight = topRight\n self.bottomLeft = bottomLeft\n self.b... | 1 | Given a `n * n` matrix `grid` of `0's` and `1's` only. We want to represent `grid` with a Quad-Tree.
Return _the root of the Quad-Tree representing_ `grid`.
A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:
* `val`: True if the node r... | null |
Python || Dividing matrix recursively | construct-quad-tree | 0 | 1 | # Code\n```\n"""\n# Definition for a QuadTree node.\nclass Node:\n def __init__(self, val, isLeaf, topLeft, topRight, bottomLeft, bottomRight):\n self.val = val\n self.isLeaf = isLeaf\n self.topLeft = topLeft\n self.topRight = topRight\n self.bottomLeft = bottomLeft\n self.b... | 1 | Implement a basic calculator to evaluate a simple expression string.
The expression string contains only non-negative integers, `'+'`, `'-'`, `'*'`, `'/'` operators, and open `'('` and closing parentheses `')'`. The integer division should **truncate toward zero**.
You may assume that the given expression is always v... | null |
Python one liner | construct-quad-tree | 0 | 1 | # Code\n```\n"""\n# Definition for a QuadTree node.\nclass Node:\n def __init__(self, val, isLeaf, topLeft, topRight, bottomLeft, bottomRight):\n self.val = val\n self.isLeaf = isLeaf\n self.topLeft = topLeft\n self.topRight = topRight\n self.bottomLeft = bottomLeft\n self.b... | 0 | Given a `n * n` matrix `grid` of `0's` and `1's` only. We want to represent `grid` with a Quad-Tree.
Return _the root of the Quad-Tree representing_ `grid`.
A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:
* `val`: True if the node r... | null |
Python one liner | construct-quad-tree | 0 | 1 | # Code\n```\n"""\n# Definition for a QuadTree node.\nclass Node:\n def __init__(self, val, isLeaf, topLeft, topRight, bottomLeft, bottomRight):\n self.val = val\n self.isLeaf = isLeaf\n self.topLeft = topLeft\n self.topRight = topRight\n self.bottomLeft = bottomLeft\n self.b... | 0 | Implement a basic calculator to evaluate a simple expression string.
The expression string contains only non-negative integers, `'+'`, `'-'`, `'*'`, `'/'` operators, and open `'('` and closing parentheses `')'`. The integer division should **truncate toward zero**.
You may assume that the given expression is always v... | null |
Python | Beats 100% | Simple and Efficient Solution | DFS | construct-quad-tree | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def construct(self, grid: List[List[int]]) -> \'Node\':\n def dfs(n, r, c):\n allSame = True\n for i in range(n):\n for j in range(n):\n if grid[r][c] != grid[r+i][c+j]:\n allSame = False\n ... | 1 | Given a `n * n` matrix `grid` of `0's` and `1's` only. We want to represent `grid` with a Quad-Tree.
Return _the root of the Quad-Tree representing_ `grid`.
A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:
* `val`: True if the node r... | null |
Python | Beats 100% | Simple and Efficient Solution | DFS | construct-quad-tree | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def construct(self, grid: List[List[int]]) -> \'Node\':\n def dfs(n, r, c):\n allSame = True\n for i in range(n):\n for j in range(n):\n if grid[r][c] != grid[r+i][c+j]:\n allSame = False\n ... | 1 | Implement a basic calculator to evaluate a simple expression string.
The expression string contains only non-negative integers, `'+'`, `'-'`, `'*'`, `'/'` operators, and open `'('` and closing parentheses `')'`. The integer division should **truncate toward zero**.
You may assume that the given expression is always v... | null |
Clean Codes🔥🔥|| Full Explanation✅|| Helper Method✅|| C++|| Java|| Python3 | construct-quad-tree | 1 | 1 | # Intuition :\n- Here we have to construct a Quadtree from a 2D binary grid, where each node of the Quadtree represents a square of the grid. \n\n# What is Quad Tree ?\n- A Quadtree is represented by a tree of Node objects, where each Node has four children (topLeft, topRight, bottomLeft, and bottomRight) that represen... | 71 | Given a `n * n` matrix `grid` of `0's` and `1's` only. We want to represent `grid` with a Quad-Tree.
Return _the root of the Quad-Tree representing_ `grid`.
A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:
* `val`: True if the node r... | null |
Clean Codes🔥🔥|| Full Explanation✅|| Helper Method✅|| C++|| Java|| Python3 | construct-quad-tree | 1 | 1 | # Intuition :\n- Here we have to construct a Quadtree from a 2D binary grid, where each node of the Quadtree represents a square of the grid. \n\n# What is Quad Tree ?\n- A Quadtree is represented by a tree of Node objects, where each Node has four children (topLeft, topRight, bottomLeft, and bottomRight) that represen... | 71 | Implement a basic calculator to evaluate a simple expression string.
The expression string contains only non-negative integers, `'+'`, `'-'`, `'*'`, `'/'` operators, and open `'('` and closing parentheses `')'`. The integer division should **truncate toward zero**.
You may assume that the given expression is always v... | null |
Python Fully Optimized Recursion || O(n^2) | construct-quad-tree | 0 | 1 | # Intuition\nWe will recursively construct the quadtree. Given some grid, we divide it into 4 subgrids, construct the nodes for those subgrids, then combine those children nodes into a tree. These base case is when we reach a grid of size 1, we know this must be a leaf node.\n\n\n# Approach\n### Case 1: Grid contains s... | 24 | Given a `n * n` matrix `grid` of `0's` and `1's` only. We want to represent `grid` with a Quad-Tree.
Return _the root of the Quad-Tree representing_ `grid`.
A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:
* `val`: True if the node r... | null |
Python Fully Optimized Recursion || O(n^2) | construct-quad-tree | 0 | 1 | # Intuition\nWe will recursively construct the quadtree. Given some grid, we divide it into 4 subgrids, construct the nodes for those subgrids, then combine those children nodes into a tree. These base case is when we reach a grid of size 1, we know this must be a leaf node.\n\n\n# Approach\n### Case 1: Grid contains s... | 24 | Implement a basic calculator to evaluate a simple expression string.
The expression string contains only non-negative integers, `'+'`, `'-'`, `'*'`, `'/'` operators, and open `'('` and closing parentheses `')'`. The integer division should **truncate toward zero**.
You may assume that the given expression is always v... | null |
Python short and clean. Divide and Conquer. Optimized DFS. | construct-quad-tree | 0 | 1 | # Approach\nSimilar to the [Official solution](https://leetcode.com/problems/construct-quad-tree/editorial/).\n\n# Complexity\n- Time complexity: $$O(m * n)$$\n\n- Space complexity: $$O(log\u2084(m * n))$$\n\nwhere, `m x n is the size of the grid`.\n\n# Code\n```python\nclass Solution:\n def construct(self, grid: li... | 3 | Given a `n * n` matrix `grid` of `0's` and `1's` only. We want to represent `grid` with a Quad-Tree.
Return _the root of the Quad-Tree representing_ `grid`.
A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:
* `val`: True if the node r... | null |
Python short and clean. Divide and Conquer. Optimized DFS. | construct-quad-tree | 0 | 1 | # Approach\nSimilar to the [Official solution](https://leetcode.com/problems/construct-quad-tree/editorial/).\n\n# Complexity\n- Time complexity: $$O(m * n)$$\n\n- Space complexity: $$O(log\u2084(m * n))$$\n\nwhere, `m x n is the size of the grid`.\n\n# Code\n```python\nclass Solution:\n def construct(self, grid: li... | 3 | Implement a basic calculator to evaluate a simple expression string.
The expression string contains only non-negative integers, `'+'`, `'-'`, `'*'`, `'/'` operators, and open `'('` and closing parentheses `')'`. The integer division should **truncate toward zero**.
You may assume that the given expression is always v... | null |
📌📌Python3 || ⚡103 ms, faster than 96.69% of Python3 | construct-quad-tree | 0 | 1 | \n\n```\nclass Solution:\n def construct(self, grid: List[List[int]]) -> \'Node\':\n def is_same(grid, x, y, length):\n val = grid[x][y]\n for i in range(x, x+length):\n ... | 5 | Given a `n * n` matrix `grid` of `0's` and `1's` only. We want to represent `grid` with a Quad-Tree.
Return _the root of the Quad-Tree representing_ `grid`.
A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:
* `val`: True if the node r... | null |
📌📌Python3 || ⚡103 ms, faster than 96.69% of Python3 | construct-quad-tree | 0 | 1 | \n\n```\nclass Solution:\n def construct(self, grid: List[List[int]]) -> \'Node\':\n def is_same(grid, x, y, length):\n val = grid[x][y]\n for i in range(x, x+length):\n ... | 5 | Implement a basic calculator to evaluate a simple expression string.
The expression string contains only non-negative integers, `'+'`, `'-'`, `'*'`, `'/'` operators, and open `'('` and closing parentheses `')'`. The integer division should **truncate toward zero**.
You may assume that the given expression is always v... | null |
Python 3 || 6 lines, recursion, w/ explanation || T/M: 99.8% / 30% | construct-quad-tree | 0 | 1 | ```\nclass Solution:\n def construct(self, grid: List[List[int]]) -> Node:\n\n def dfs(x = 0, y = 0, n = len(grid)): # <- (x,y) is the upper-left corner of the quad\n # and n is the length of the side of the quad\n\n if all... | 10 | Given a `n * n` matrix `grid` of `0's` and `1's` only. We want to represent `grid` with a Quad-Tree.
Return _the root of the Quad-Tree representing_ `grid`.
A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:
* `val`: True if the node r... | null |
Python 3 || 6 lines, recursion, w/ explanation || T/M: 99.8% / 30% | construct-quad-tree | 0 | 1 | ```\nclass Solution:\n def construct(self, grid: List[List[int]]) -> Node:\n\n def dfs(x = 0, y = 0, n = len(grid)): # <- (x,y) is the upper-left corner of the quad\n # and n is the length of the side of the quad\n\n if all... | 10 | Implement a basic calculator to evaluate a simple expression string.
The expression string contains only non-negative integers, `'+'`, `'-'`, `'*'`, `'/'` operators, and open `'('` and closing parentheses `')'`. The integer division should **truncate toward zero**.
You may assume that the given expression is always v... | null |
427: Solution with step by step explanation | construct-quad-tree | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Define the Node class with val, isLeaf, topLeft, topRight, bottomLeft, and bottomRight attributes.\n\n2. Define the Solution class with the construct method that takes in a 2D list of 0\'s and 1\'s as input.\n\n3. Within ... | 4 | Given a `n * n` matrix `grid` of `0's` and `1's` only. We want to represent `grid` with a Quad-Tree.
Return _the root of the Quad-Tree representing_ `grid`.
A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:
* `val`: True if the node r... | null |
427: Solution with step by step explanation | construct-quad-tree | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Define the Node class with val, isLeaf, topLeft, topRight, bottomLeft, and bottomRight attributes.\n\n2. Define the Solution class with the construct method that takes in a 2D list of 0\'s and 1\'s as input.\n\n3. Within ... | 4 | Implement a basic calculator to evaluate a simple expression string.
The expression string contains only non-negative integers, `'+'`, `'-'`, `'*'`, `'/'` operators, and open `'('` and closing parentheses `')'`. The integer division should **truncate toward zero**.
You may assume that the given expression is always v... | null |
[Java/C++/Python/JavaScript/Swift]O(n)time/BEATS 99.97% MEMORY/SPEED 0ms APRIL 2022 | construct-quad-tree | 1 | 1 | ```\n```\n\n(Note: This is part of a series of Leetcode solution explanations. If you like this solution or find it useful, ***please upvote*** this post.)\n***Take care brother, peace, love!***\n\n```\n```\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***0ms / 38.2MB*** (beats 92.04% / 24.00%).\n* *... | 4 | Given a `n * n` matrix `grid` of `0's` and `1's` only. We want to represent `grid` with a Quad-Tree.
Return _the root of the Quad-Tree representing_ `grid`.
A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:
* `val`: True if the node r... | null |
[Java/C++/Python/JavaScript/Swift]O(n)time/BEATS 99.97% MEMORY/SPEED 0ms APRIL 2022 | construct-quad-tree | 1 | 1 | ```\n```\n\n(Note: This is part of a series of Leetcode solution explanations. If you like this solution or find it useful, ***please upvote*** this post.)\n***Take care brother, peace, love!***\n\n```\n```\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***0ms / 38.2MB*** (beats 92.04% / 24.00%).\n* *... | 4 | Implement a basic calculator to evaluate a simple expression string.
The expression string contains only non-negative integers, `'+'`, `'-'`, `'*'`, `'/'` operators, and open `'('` and closing parentheses `')'`. The integer division should **truncate toward zero**.
You may assume that the given expression is always v... | null |
429: Space 92.13%, Solution with step by step explanation | n-ary-tree-level-order-traversal | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. We start by checking if the input root is empty. If it is, we return an empty list.\n```\n if not root:\n return []\n```\n2. We initialize two lists, result and level. result will store the final level o... | 2 | Given an n-ary tree, return the _level order_ traversal of its nodes' values.
_Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples)._
**Example 1:**
**Input:** root = \[1,null,3,2,4,null,5,6\]
**Output:** \[\[1\],\[3,2,4\],\... | null |
429: Space 92.13%, Solution with step by step explanation | n-ary-tree-level-order-traversal | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. We start by checking if the input root is empty. If it is, we return an empty list.\n```\n if not root:\n return []\n```\n2. We initialize two lists, result and level. result will store the final level o... | 2 | You are given an integer `n`. You have an `n x n` binary grid `grid` with all values initially `1`'s except for some indices given in the array `mines`. The `ith` element of the array `mines` is defined as `mines[i] = [xi, yi]` where `grid[xi][yi] == 0`.
Return _the order of the largest **axis-aligned** plus sign of_ ... | null |
N-ary Tree Level Order Traversal | n-ary-tree-level-order-traversal | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | Given an n-ary tree, return the _level order_ traversal of its nodes' values.
_Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples)._
**Example 1:**
**Input:** root = \[1,null,3,2,4,null,5,6\]
**Output:** \[\[1\],\[3,2,4\],\... | null |
N-ary Tree Level Order Traversal | n-ary-tree-level-order-traversal | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | You are given an integer `n`. You have an `n x n` binary grid `grid` with all values initially `1`'s except for some indices given in the array `mines`. The `ith` element of the array `mines` is defined as `mines[i] = [xi, yi]` where `grid[xi][yi] == 0`.
Return _the order of the largest **axis-aligned** plus sign of_ ... | null |
Solution | n-ary-tree-level-order-traversal | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n vector<vector<int>> levelOrder(Node* root) {\n queue<Node*>q;\n vector<vector<int>> ans;\n if(!root) return ans;\n q.push(root);\n while(!q.empty()){\n int size=q.size();\n vector<int>temp;\n while(size--){\n ... | 1 | Given an n-ary tree, return the _level order_ traversal of its nodes' values.
_Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples)._
**Example 1:**
**Input:** root = \[1,null,3,2,4,null,5,6\]
**Output:** \[\[1\],\[3,2,4\],\... | null |
Solution | n-ary-tree-level-order-traversal | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n vector<vector<int>> levelOrder(Node* root) {\n queue<Node*>q;\n vector<vector<int>> ans;\n if(!root) return ans;\n q.push(root);\n while(!q.empty()){\n int size=q.size();\n vector<int>temp;\n while(size--){\n ... | 1 | You are given an integer `n`. You have an `n x n` binary grid `grid` with all values initially `1`'s except for some indices given in the array `mines`. The `ith` element of the array `mines` is defined as `mines[i] = [xi, yi]` where `grid[xi][yi] == 0`.
Return _the order of the largest **axis-aligned** plus sign of_ ... | null |
PYTHON3 beats 95% using queue and None method space and time O(N) | n-ary-tree-level-order-traversal | 0 | 1 | # Approach\n<!-- Describe your first thoughts on how to solve this problem. -->\nwe can have a queue which holds the root node and then a None node,this None node being the point where a level ends\nfrom here we attach the values of node to an array b \n for the children each child is similarly appended to the queue... | 1 | Given an n-ary tree, return the _level order_ traversal of its nodes' values.
_Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples)._
**Example 1:**
**Input:** root = \[1,null,3,2,4,null,5,6\]
**Output:** \[\[1\],\[3,2,4\],\... | null |
PYTHON3 beats 95% using queue and None method space and time O(N) | n-ary-tree-level-order-traversal | 0 | 1 | # Approach\n<!-- Describe your first thoughts on how to solve this problem. -->\nwe can have a queue which holds the root node and then a None node,this None node being the point where a level ends\nfrom here we attach the values of node to an array b \n for the children each child is similarly appended to the queue... | 1 | You are given an integer `n`. You have an `n x n` binary grid `grid` with all values initially `1`'s except for some indices given in the array `mines`. The `ith` element of the array `mines` is defined as `mines[i] = [xi, yi]` where `grid[xi][yi] == 0`.
Return _the order of the largest **axis-aligned** plus sign of_ ... | null |
✅Python3 57ms 🔥🔥🔥 easy detailed explanation. | n-ary-tree-level-order-traversal | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSimple BFS can solve this problem.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- store nodes by level.\n- traverse all children.\n- if current node is None return.\n- return answer\n\n# Approach for array\n- he... | 5 | Given an n-ary tree, return the _level order_ traversal of its nodes' values.
_Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples)._
**Example 1:**
**Input:** root = \[1,null,3,2,4,null,5,6\]
**Output:** \[\[1\],\[3,2,4\],\... | null |
✅Python3 57ms 🔥🔥🔥 easy detailed explanation. | n-ary-tree-level-order-traversal | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSimple BFS can solve this problem.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- store nodes by level.\n- traverse all children.\n- if current node is None return.\n- return answer\n\n# Approach for array\n- he... | 5 | You are given an integer `n`. You have an `n x n` binary grid `grid` with all values initially `1`'s except for some indices given in the array `mines`. The `ith` element of the array `mines` is defined as `mines[i] = [xi, yi]` where `grid[xi][yi] == 0`.
Return _the order of the largest **axis-aligned** plus sign of_ ... | null |
🍀Java 🔥 | | Python 🔥| | C++ 🔥| | Easy 🔥🍀 | n-ary-tree-level-order-traversal | 1 | 1 | # N-ary Tree level Order Traversal \n> ### *Try to understand the code line by line. Easy BFS solution.* \n```java []\nclass Solution {\n public List<List<Integer>> levelOrder(Node root) {\n Queue<Node> queue = new LinkedList<>();\n List<List<Integer>> result = new ArrayList<>();\n if(root == nu... | 5 | Given an n-ary tree, return the _level order_ traversal of its nodes' values.
_Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples)._
**Example 1:**
**Input:** root = \[1,null,3,2,4,null,5,6\]
**Output:** \[\[1\],\[3,2,4\],\... | null |
🍀Java 🔥 | | Python 🔥| | C++ 🔥| | Easy 🔥🍀 | n-ary-tree-level-order-traversal | 1 | 1 | # N-ary Tree level Order Traversal \n> ### *Try to understand the code line by line. Easy BFS solution.* \n```java []\nclass Solution {\n public List<List<Integer>> levelOrder(Node root) {\n Queue<Node> queue = new LinkedList<>();\n List<List<Integer>> result = new ArrayList<>();\n if(root == nu... | 5 | You are given an integer `n`. You have an `n x n` binary grid `grid` with all values initially `1`'s except for some indices given in the array `mines`. The `ith` element of the array `mines` is defined as `mines[i] = [xi, yi]` where `grid[xi][yi] == 0`.
Return _the order of the largest **axis-aligned** plus sign of_ ... | null |
🔥 [LeetCode The Hard Way]🔥 Easy BFS Explained Line By Line | n-ary-tree-level-order-traversal | 0 | 1 | Please check out [LeetCode The Hard Way](https://wingkwong.github.io/leetcode-the-hard-way/) for more solution explanations and tutorials. I\'ll explain my solution line by line daily. \nIf you like it, please give a star, watch my [Github Repository](https://github.com/wingkwong/leetcode-the-hard-way) and upvote this ... | 48 | Given an n-ary tree, return the _level order_ traversal of its nodes' values.
_Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples)._
**Example 1:**
**Input:** root = \[1,null,3,2,4,null,5,6\]
**Output:** \[\[1\],\[3,2,4\],\... | null |
🔥 [LeetCode The Hard Way]🔥 Easy BFS Explained Line By Line | n-ary-tree-level-order-traversal | 0 | 1 | Please check out [LeetCode The Hard Way](https://wingkwong.github.io/leetcode-the-hard-way/) for more solution explanations and tutorials. I\'ll explain my solution line by line daily. \nIf you like it, please give a star, watch my [Github Repository](https://github.com/wingkwong/leetcode-the-hard-way) and upvote this ... | 48 | You are given an integer `n`. You have an `n x n` binary grid `grid` with all values initially `1`'s except for some indices given in the array `mines`. The `ith` element of the array `mines` is defined as `mines[i] = [xi, yi]` where `grid[xi][yi] == 0`.
Return _the order of the largest **axis-aligned** plus sign of_ ... | null |
Python, BFS | n-ary-tree-level-order-traversal | 0 | 1 | ```\nclass Solution:\n def levelOrder(self, root: \'Node\') -> List[List[int]]:\n result = [] \n q = deque([root] if root else [])\n while q:\n result.append([])\n for _ in range(len(q)):\n node = q.popleft()\n result[-1].append(node.val... | 15 | Given an n-ary tree, return the _level order_ traversal of its nodes' values.
_Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples)._
**Example 1:**
**Input:** root = \[1,null,3,2,4,null,5,6\]
**Output:** \[\[1\],\[3,2,4\],\... | null |
Python, BFS | n-ary-tree-level-order-traversal | 0 | 1 | ```\nclass Solution:\n def levelOrder(self, root: \'Node\') -> List[List[int]]:\n result = [] \n q = deque([root] if root else [])\n while q:\n result.append([])\n for _ in range(len(q)):\n node = q.popleft()\n result[-1].append(node.val... | 15 | You are given an integer `n`. You have an `n x n` binary grid `grid` with all values initially `1`'s except for some indices given in the array `mines`. The `ith` element of the array `mines` is defined as `mines[i] = [xi, yi]` where `grid[xi][yi] == 0`.
Return _the order of the largest **axis-aligned** plus sign of_ ... | null |
Python Elegant & Short | BFS + DFS | Generators | n-ary-tree-level-order-traversal | 0 | 1 | # BFS solution\n\n\tclass Solution:\n\t\t"""\n\t\tTime: O(n)\n\t\tMemory: O(n)\n\t\t"""\n\n\t\tdef levelOrder(self, root: Optional[\'Node\']) -> List[List[int]]:\n\t\t\tif root is None:\n\t\t\t\treturn []\n\n\t\t\tqueue = deque([root])\n\t\t\tlevels = []\n\n\t\t\twhile queue:\n\t\t\t\tlevels.append([])\n\t\t\t\tfor _... | 2 | Given an n-ary tree, return the _level order_ traversal of its nodes' values.
_Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples)._
**Example 1:**
**Input:** root = \[1,null,3,2,4,null,5,6\]
**Output:** \[\[1\],\[3,2,4\],\... | null |
Python Elegant & Short | BFS + DFS | Generators | n-ary-tree-level-order-traversal | 0 | 1 | # BFS solution\n\n\tclass Solution:\n\t\t"""\n\t\tTime: O(n)\n\t\tMemory: O(n)\n\t\t"""\n\n\t\tdef levelOrder(self, root: Optional[\'Node\']) -> List[List[int]]:\n\t\t\tif root is None:\n\t\t\t\treturn []\n\n\t\t\tqueue = deque([root])\n\t\t\tlevels = []\n\n\t\t\twhile queue:\n\t\t\t\tlevels.append([])\n\t\t\t\tfor _... | 2 | You are given an integer `n`. You have an `n x n` binary grid `grid` with all values initially `1`'s except for some indices given in the array `mines`. The `ith` element of the array `mines` is defined as `mines[i] = [xi, yi]` where `grid[xi][yi] == 0`.
Return _the order of the largest **axis-aligned** plus sign of_ ... | null |
Simplest Python code [Just do level-order] | n-ary-tree-level-order-traversal | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def levelOrder(self, root: \'Node\') -> List[List[int]]:\n a = []\n\n def dfs(node, h):\n if not node: return\n if len(a) == h: a.append([])\n\n a[h].append(node.val)\n\n for child in node.children:\n dfs(chil... | 2 | Given an n-ary tree, return the _level order_ traversal of its nodes' values.
_Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples)._
**Example 1:**
**Input:** root = \[1,null,3,2,4,null,5,6\]
**Output:** \[\[1\],\[3,2,4\],\... | null |
Simplest Python code [Just do level-order] | n-ary-tree-level-order-traversal | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def levelOrder(self, root: \'Node\') -> List[List[int]]:\n a = []\n\n def dfs(node, h):\n if not node: return\n if len(a) == h: a.append([])\n\n a[h].append(node.val)\n\n for child in node.children:\n dfs(chil... | 2 | You are given an integer `n`. You have an `n x n` binary grid `grid` with all values initially `1`'s except for some indices given in the array `mines`. The `ith` element of the array `mines` is defined as `mines[i] = [xi, yi]` where `grid[xi][yi] == 0`.
Return _the order of the largest **axis-aligned** plus sign of_ ... | null |
[Python] Recursive DFS Solution | n-ary-tree-level-order-traversal | 0 | 1 | ```\nclass Solution:\n def levelOrder(self, root: \'Node\') -> List[List[int]]:\n if not root: return []\n res = []\n levels = set()\n def dfs(node, level):\n if level not in levels:\n levels.add(level)\n res.append([])\n res[level].appe... | 1 | Given an n-ary tree, return the _level order_ traversal of its nodes' values.
_Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples)._
**Example 1:**
**Input:** root = \[1,null,3,2,4,null,5,6\]
**Output:** \[\[1\],\[3,2,4\],\... | null |
[Python] Recursive DFS Solution | n-ary-tree-level-order-traversal | 0 | 1 | ```\nclass Solution:\n def levelOrder(self, root: \'Node\') -> List[List[int]]:\n if not root: return []\n res = []\n levels = set()\n def dfs(node, level):\n if level not in levels:\n levels.add(level)\n res.append([])\n res[level].appe... | 1 | You are given an integer `n`. You have an `n x n` binary grid `grid` with all values initially `1`'s except for some indices given in the array `mines`. The `ith` element of the array `mines` is defined as `mines[i] = [xi, yi]` where `grid[xi][yi] == 0`.
Return _the order of the largest **axis-aligned** plus sign of_ ... | null |
🌻 100% | Classic BFS . 2 ways in each language (C++, Go, Python, Ts/Js, Java) | n-ary-tree-level-order-traversal | 0 | 1 | There are two ways to add values of each level to the `result` array \n1. add one at a time, when we have finished iterating through the children of a particular node to the `deque`, like this:\n```\nfor (Node *n: node->children)\n{\n\tdq.push_back(n);\n}\ntemp.push_back(node->val);\n```\n2. or, we can add them as we i... | 1 | Given an n-ary tree, return the _level order_ traversal of its nodes' values.
_Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples)._
**Example 1:**
**Input:** root = \[1,null,3,2,4,null,5,6\]
**Output:** \[\[1\],\[3,2,4\],\... | null |
🌻 100% | Classic BFS . 2 ways in each language (C++, Go, Python, Ts/Js, Java) | n-ary-tree-level-order-traversal | 0 | 1 | There are two ways to add values of each level to the `result` array \n1. add one at a time, when we have finished iterating through the children of a particular node to the `deque`, like this:\n```\nfor (Node *n: node->children)\n{\n\tdq.push_back(n);\n}\ntemp.push_back(node->val);\n```\n2. or, we can add them as we i... | 1 | You are given an integer `n`. You have an `n x n` binary grid `grid` with all values initially `1`'s except for some indices given in the array `mines`. The `ith` element of the array `mines` is defined as `mines[i] = [xi, yi]` where `grid[xi][yi] == 0`.
Return _the order of the largest **axis-aligned** plus sign of_ ... | null |
Easy python solution 18 lines only | n-ary-tree-level-order-traversal | 0 | 1 | ```\nclass Solution:\n def levelOrder(self, root: \'Node\') -> List[List[int]]:\n if root is None:\n return []\n #print(root.children)\n valList=[[root.val]]\n flst=[]\n xlst=[]\n lst=[root]\n while lst:\n x=lst.pop(0)\n if x.children:... | 1 | Given an n-ary tree, return the _level order_ traversal of its nodes' values.
_Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples)._
**Example 1:**
**Input:** root = \[1,null,3,2,4,null,5,6\]
**Output:** \[\[1\],\[3,2,4\],\... | null |
Easy python solution 18 lines only | n-ary-tree-level-order-traversal | 0 | 1 | ```\nclass Solution:\n def levelOrder(self, root: \'Node\') -> List[List[int]]:\n if root is None:\n return []\n #print(root.children)\n valList=[[root.val]]\n flst=[]\n xlst=[]\n lst=[root]\n while lst:\n x=lst.pop(0)\n if x.children:... | 1 | You are given an integer `n`. You have an `n x n` binary grid `grid` with all values initially `1`'s except for some indices given in the array `mines`. The `ith` element of the array `mines` is defined as `mines[i] = [xi, yi]` where `grid[xi][yi] == 0`.
Return _the order of the largest **axis-aligned** plus sign of_ ... | null |
Unlocking the Mysteries of the Squiggly List: A Tale of Flattening Multilevel Doubly Linked Lists | flatten-a-multilevel-doubly-linked-list | 0 | 1 | # Intuition\nWhen reading the question to flatten a multilevel doubly linked list, a first intuition would be to use recursion or iteration to traverse the list and handle the child nodes.\n\n# Approach\nThe approach used in this code is a recursive traversal of the multilevel doubly linked list to flatten it.\nThe fla... | 2 | You are given a doubly linked list, which contains nodes that have a next pointer, a previous pointer, and an additional **child pointer**. This child pointer may or may not point to a separate doubly linked list, also containing these special nodes. These child lists may have one or more children of their own, and so ... | null |
Unlocking the Mysteries of the Squiggly List: A Tale of Flattening Multilevel Doubly Linked Lists | flatten-a-multilevel-doubly-linked-list | 0 | 1 | # Intuition\nWhen reading the question to flatten a multilevel doubly linked list, a first intuition would be to use recursion or iteration to traverse the list and handle the child nodes.\n\n# Approach\nThe approach used in this code is a recursive traversal of the multilevel doubly linked list to flatten it.\nThe fla... | 2 | Given an `m x n` `matrix`, return _`true` if the matrix is Toeplitz. Otherwise, return `false`._
A matrix is **Toeplitz** if every diagonal from top-left to bottom-right has the same elements.
**Example 1:**
**Input:** matrix = \[\[1,2,3,4\],\[5,1,2,3\],\[9,5,1,2\]\]
**Output:** true
**Explanation:**
In the above gr... | null |
[Python 3] Using recursion || beats 98% || 36ms 🥷🏼 | flatten-a-multilevel-doubly-linked-list | 0 | 1 | \n```python3 []\nclass Solution:\n def flatten(self, head: \'Optional[Node]\') -> \'Optional[Node]\':\n def unpack(head):\n cur = tail = head\n while cur:\n if cur.child:\n start, end = unpack(cur.child)\n if cur.next: cur.next.prev = ... | 3 | You are given a doubly linked list, which contains nodes that have a next pointer, a previous pointer, and an additional **child pointer**. This child pointer may or may not point to a separate doubly linked list, also containing these special nodes. These child lists may have one or more children of their own, and so ... | null |
[Python 3] Using recursion || beats 98% || 36ms 🥷🏼 | flatten-a-multilevel-doubly-linked-list | 0 | 1 | \n```python3 []\nclass Solution:\n def flatten(self, head: \'Optional[Node]\') -> \'Optional[Node]\':\n def unpack(head):\n cur = tail = head\n while cur:\n if cur.child:\n start, end = unpack(cur.child)\n if cur.next: cur.next.prev = ... | 3 | Given an `m x n` `matrix`, return _`true` if the matrix is Toeplitz. Otherwise, return `false`._
A matrix is **Toeplitz** if every diagonal from top-left to bottom-right has the same elements.
**Example 1:**
**Input:** matrix = \[\[1,2,3,4\],\[5,1,2,3\],\[9,5,1,2\]\]
**Output:** true
**Explanation:**
In the above gr... | null |
Python || 99.36% Faster || Recursive Solution | flatten-a-multilevel-doubly-linked-list | 0 | 1 | ```\nclass Solution:\n def child(self,node,bottom,nexx):\n curr=bottom\n while curr.next:\n if curr.child:\n self.child(curr,curr.child,curr.next)\n curr=curr.next\n node.next=bottom\n bottom.prev=node\n node.child=None\n curr.next=nexx\n... | 1 | You are given a doubly linked list, which contains nodes that have a next pointer, a previous pointer, and an additional **child pointer**. This child pointer may or may not point to a separate doubly linked list, also containing these special nodes. These child lists may have one or more children of their own, and so ... | null |
Python || 99.36% Faster || Recursive Solution | flatten-a-multilevel-doubly-linked-list | 0 | 1 | ```\nclass Solution:\n def child(self,node,bottom,nexx):\n curr=bottom\n while curr.next:\n if curr.child:\n self.child(curr,curr.child,curr.next)\n curr=curr.next\n node.next=bottom\n bottom.prev=node\n node.child=None\n curr.next=nexx\n... | 1 | Given an `m x n` `matrix`, return _`true` if the matrix is Toeplitz. Otherwise, return `false`._
A matrix is **Toeplitz** if every diagonal from top-left to bottom-right has the same elements.
**Example 1:**
**Input:** matrix = \[\[1,2,3,4\],\[5,1,2,3\],\[9,5,1,2\]\]
**Output:** true
**Explanation:**
In the above gr... | null |
430: Solution with step by step explanation | flatten-a-multilevel-doubly-linked-list | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Define the function flattenDFS which takes a Node object as input and returns a Node object.\n2. Check if the node object is None. If it is, return None.\n3. Check if node object has no child and no next pointers. If it d... | 6 | You are given a doubly linked list, which contains nodes that have a next pointer, a previous pointer, and an additional **child pointer**. This child pointer may or may not point to a separate doubly linked list, also containing these special nodes. These child lists may have one or more children of their own, and so ... | null |
430: Solution with step by step explanation | flatten-a-multilevel-doubly-linked-list | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Define the function flattenDFS which takes a Node object as input and returns a Node object.\n2. Check if the node object is None. If it is, return None.\n3. Check if node object has no child and no next pointers. If it d... | 6 | Given an `m x n` `matrix`, return _`true` if the matrix is Toeplitz. Otherwise, return `false`._
A matrix is **Toeplitz** if every diagonal from top-left to bottom-right has the same elements.
**Example 1:**
**Input:** matrix = \[\[1,2,3,4\],\[5,1,2,3\],\[9,5,1,2\]\]
**Output:** true
**Explanation:**
In the above gr... | null |
Implementation using stack and dummy pointer [99% Space Complexity] | flatten-a-multilevel-doubly-linked-list | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nMy First thoughts upon viewing the questions is to some how save the `head.next` nodes when encountering the nodes with the `head.child` nodes. As saving the `head.next` once we continue moving forward in the child node and reach the end ... | 1 | You are given a doubly linked list, which contains nodes that have a next pointer, a previous pointer, and an additional **child pointer**. This child pointer may or may not point to a separate doubly linked list, also containing these special nodes. These child lists may have one or more children of their own, and so ... | null |
Implementation using stack and dummy pointer [99% Space Complexity] | flatten-a-multilevel-doubly-linked-list | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nMy First thoughts upon viewing the questions is to some how save the `head.next` nodes when encountering the nodes with the `head.child` nodes. As saving the `head.next` once we continue moving forward in the child node and reach the end ... | 1 | Given an `m x n` `matrix`, return _`true` if the matrix is Toeplitz. Otherwise, return `false`._
A matrix is **Toeplitz** if every diagonal from top-left to bottom-right has the same elements.
**Example 1:**
**Input:** matrix = \[\[1,2,3,4\],\[5,1,2,3\],\[9,5,1,2\]\]
**Output:** true
**Explanation:**
In the above gr... | null |
Python O(n), O(1) easy solution with explanation | flatten-a-multilevel-doubly-linked-list | 0 | 1 | Easy iterative approach without using extra memory.\nO(n) time complexity\nO(1) space complexity\n\n```\n"""\n# Definition for a Node.\nclass Node(object):\n def __init__(self, val, prev, next, child):\n self.val = val\n self.prev = prev\n self.next = next\n self.child = child\n"""\n\ncla... | 21 | You are given a doubly linked list, which contains nodes that have a next pointer, a previous pointer, and an additional **child pointer**. This child pointer may or may not point to a separate doubly linked list, also containing these special nodes. These child lists may have one or more children of their own, and so ... | null |
Python O(n), O(1) easy solution with explanation | flatten-a-multilevel-doubly-linked-list | 0 | 1 | Easy iterative approach without using extra memory.\nO(n) time complexity\nO(1) space complexity\n\n```\n"""\n# Definition for a Node.\nclass Node(object):\n def __init__(self, val, prev, next, child):\n self.val = val\n self.prev = prev\n self.next = next\n self.child = child\n"""\n\ncla... | 21 | Given an `m x n` `matrix`, return _`true` if the matrix is Toeplitz. Otherwise, return `false`._
A matrix is **Toeplitz** if every diagonal from top-left to bottom-right has the same elements.
**Example 1:**
**Input:** matrix = \[\[1,2,3,4\],\[5,1,2,3\],\[9,5,1,2\]\]
**Output:** true
**Explanation:**
In the above gr... | null |
Solution | flatten-a-multilevel-doubly-linked-list | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n Node* flat(Node* head) {\n if (!head)\n return NULL;\n Node* temp1=NULL;\n while(head) {\n if (head->child) {\n Node* tail = flat(head->child);\n Node* temp = head->next;\n head->next = head... | 2 | You are given a doubly linked list, which contains nodes that have a next pointer, a previous pointer, and an additional **child pointer**. This child pointer may or may not point to a separate doubly linked list, also containing these special nodes. These child lists may have one or more children of their own, and so ... | null |
Solution | flatten-a-multilevel-doubly-linked-list | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n Node* flat(Node* head) {\n if (!head)\n return NULL;\n Node* temp1=NULL;\n while(head) {\n if (head->child) {\n Node* tail = flat(head->child);\n Node* temp = head->next;\n head->next = head... | 2 | Given an `m x n` `matrix`, return _`true` if the matrix is Toeplitz. Otherwise, return `false`._
A matrix is **Toeplitz** if every diagonal from top-left to bottom-right has the same elements.
**Example 1:**
**Input:** matrix = \[\[1,2,3,4\],\[5,1,2,3\],\[9,5,1,2\]\]
**Output:** true
**Explanation:**
In the above gr... | null |
[Python3] Recursion | flatten-a-multilevel-doubly-linked-list | 0 | 1 | ```\n"""\n# Definition for a Node.\nclass Node:\n def __init__(self, val, prev, next, child):\n self.val = val\n self.prev = prev\n self.next = next\n self.child = child\n"""\n\nclass Solution:\n def flatten(self, head: \'Optional[Node]\') -> \'Optional[Node]\':\n res = head\n ... | 1 | You are given a doubly linked list, which contains nodes that have a next pointer, a previous pointer, and an additional **child pointer**. This child pointer may or may not point to a separate doubly linked list, also containing these special nodes. These child lists may have one or more children of their own, and so ... | null |
[Python3] Recursion | flatten-a-multilevel-doubly-linked-list | 0 | 1 | ```\n"""\n# Definition for a Node.\nclass Node:\n def __init__(self, val, prev, next, child):\n self.val = val\n self.prev = prev\n self.next = next\n self.child = child\n"""\n\nclass Solution:\n def flatten(self, head: \'Optional[Node]\') -> \'Optional[Node]\':\n res = head\n ... | 1 | Given an `m x n` `matrix`, return _`true` if the matrix is Toeplitz. Otherwise, return `false`._
A matrix is **Toeplitz** if every diagonal from top-left to bottom-right has the same elements.
**Example 1:**
**Input:** matrix = \[\[1,2,3,4\],\[5,1,2,3\],\[9,5,1,2\]\]
**Output:** true
**Explanation:**
In the above gr... | null |
TC: O(n), SC: O(1) No stack, No recursion Simple One Pass Python Solution | flatten-a-multilevel-doubly-linked-list | 0 | 1 | ```\ndef flatten(self, head: \'Optional[Node]\') -> \'Optional[Node]\':\n if not head: return\n curr = head\n while curr:\n if curr.child:\n childNode = curr.child\n childNode.prev = curr #setting previous pointer\n \n whil... | 9 | You are given a doubly linked list, which contains nodes that have a next pointer, a previous pointer, and an additional **child pointer**. This child pointer may or may not point to a separate doubly linked list, also containing these special nodes. These child lists may have one or more children of their own, and so ... | null |
TC: O(n), SC: O(1) No stack, No recursion Simple One Pass Python Solution | flatten-a-multilevel-doubly-linked-list | 0 | 1 | ```\ndef flatten(self, head: \'Optional[Node]\') -> \'Optional[Node]\':\n if not head: return\n curr = head\n while curr:\n if curr.child:\n childNode = curr.child\n childNode.prev = curr #setting previous pointer\n \n whil... | 9 | Given an `m x n` `matrix`, return _`true` if the matrix is Toeplitz. Otherwise, return `false`._
A matrix is **Toeplitz** if every diagonal from top-left to bottom-right has the same elements.
**Example 1:**
**Input:** matrix = \[\[1,2,3,4\],\[5,1,2,3\],\[9,5,1,2\]\]
**Output:** true
**Explanation:**
In the above gr... | null |
Python Intuitive Recursive Approach with Detailed Explanation | flatten-a-multilevel-doubly-linked-list | 0 | 1 | ```\nclass Solution:\n def flatten(self, head: \'Optional[Node]\') -> \'Optional[Node]\':\n if not head:\n return\n \n def dfs(node):\n if node.child: #if node has a child, we go down\n temp = node.next # store the next node to temp \n node.nex... | 2 | You are given a doubly linked list, which contains nodes that have a next pointer, a previous pointer, and an additional **child pointer**. This child pointer may or may not point to a separate doubly linked list, also containing these special nodes. These child lists may have one or more children of their own, and so ... | null |
Python Intuitive Recursive Approach with Detailed Explanation | flatten-a-multilevel-doubly-linked-list | 0 | 1 | ```\nclass Solution:\n def flatten(self, head: \'Optional[Node]\') -> \'Optional[Node]\':\n if not head:\n return\n \n def dfs(node):\n if node.child: #if node has a child, we go down\n temp = node.next # store the next node to temp \n node.nex... | 2 | Given an `m x n` `matrix`, return _`true` if the matrix is Toeplitz. Otherwise, return `false`._
A matrix is **Toeplitz** if every diagonal from top-left to bottom-right has the same elements.
**Example 1:**
**Input:** matrix = \[\[1,2,3,4\],\[5,1,2,3\],\[9,5,1,2\]\]
**Output:** true
**Explanation:**
In the above gr... | null |
Python Recursion: Easy-to-understand with Explanation | flatten-a-multilevel-doubly-linked-list | 0 | 1 | ### Intuition\n\nSince we are working with a doubly-linked list, for every `node.child` that we handle, we need to obtain the end `node` of the list at that particular level, so that we can assign its `node.next` appropriately. We can thus write a recursive function that does the following:\n\n- Loop through each `node... | 8 | You are given a doubly linked list, which contains nodes that have a next pointer, a previous pointer, and an additional **child pointer**. This child pointer may or may not point to a separate doubly linked list, also containing these special nodes. These child lists may have one or more children of their own, and so ... | null |
Python Recursion: Easy-to-understand with Explanation | flatten-a-multilevel-doubly-linked-list | 0 | 1 | ### Intuition\n\nSince we are working with a doubly-linked list, for every `node.child` that we handle, we need to obtain the end `node` of the list at that particular level, so that we can assign its `node.next` appropriately. We can thus write a recursive function that does the following:\n\n- Loop through each `node... | 8 | Given an `m x n` `matrix`, return _`true` if the matrix is Toeplitz. Otherwise, return `false`._
A matrix is **Toeplitz** if every diagonal from top-left to bottom-right has the same elements.
**Example 1:**
**Input:** matrix = \[\[1,2,3,4\],\[5,1,2,3\],\[9,5,1,2\]\]
**Output:** true
**Explanation:**
In the above gr... | null |
Solution O(1). Runtime > 99% | all-oone-data-structure | 0 | 1 | \n\n```\nclass ListCount:\n def __init__(self, set_string, count, prev= None, next= None):\n self.set... | 4 | Design a data structure to store the strings' count with the ability to return the strings with minimum and maximum counts.
Implement the `AllOne` class:
* `AllOne()` Initializes the object of the data structure.
* `inc(String key)` Increments the count of the string `key` by `1`. If `key` does not exist in the d... | null |
Solution | all-oone-data-structure | 1 | 1 | ```C++ []\nclass AllOne {\npublic:\nunordered_map<string, int> m;\n AllOne() {\n ios_base::sync_with_stdio(0);\n cin.tie(0); cout.tie(0);\n }\n void updateMin() { \n _min.second = INT_MAX;\n for(auto & nd : m) {\n if(_min.second > nd.second) {\n _min... | 2 | Design a data structure to store the strings' count with the ability to return the strings with minimum and maximum counts.
Implement the `AllOne` class:
* `AllOne()` Initializes the object of the data structure.
* `inc(String key)` Increments the count of the string `key` by `1`. If `key` does not exist in the d... | null |
Python3 Solution Using Counter and Cache (Runtime Beating 98%, Memory Beating 100%) | all-oone-data-structure | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe prompt told us that "each function must run in O(1) average time complexity", which suggests that we must be using a hashmap to achieve this. What is tricky though, is that ordinary dictionaries are not really suited for the getMaxKey... | 1 | Design a data structure to store the strings' count with the ability to return the strings with minimum and maximum counts.
Implement the `AllOne` class:
* `AllOne()` Initializes the object of the data structure.
* `inc(String key)` Increments the count of the string `key` by `1`. If `key` does not exist in the d... | null |
DoubleLinkedList | all-oone-data-structure | 0 | 1 | # Intuition\nUsing Doublelinklist to achieve O(1) retrieval. Using hashmap to help with O(1) add or remove list nodes.\n\n# Approach\ncreate dummy head and tail for better implementation of the double linkedlist. (avoid edge cases when adding or removing nodes). Head is the smallest and tail is the largest, hence using... | 0 | Design a data structure to store the strings' count with the ability to return the strings with minimum and maximum counts.
Implement the `AllOne` class:
* `AllOne()` Initializes the object of the data structure.
* `inc(String key)` Increments the count of the string `key` by `1`. If `key` does not exist in the d... | null |
LeetCodeLegends | all-oone-data-structure | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe need constant time search so a hash table must be involved.\nWe need constant time access to a min and a max key. Heap maybe?\nWe need be able to move elements to max/min locatoin in constant time when they are incremented/decremented ... | 0 | Design a data structure to store the strings' count with the ability to return the strings with minimum and maximum counts.
Implement the `AllOne` class:
* `AllOne()` Initializes the object of the data structure.
* `inc(String key)` Increments the count of the string `key` by `1`. If `key` does not exist in the d... | null |
Hack It Up | all-oone-data-structure | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | Design a data structure to store the strings' count with the ability to return the strings with minimum and maximum counts.
Implement the `AllOne` class:
* `AllOne()` Initializes the object of the data structure.
* `inc(String key)` Increments the count of the string `key` by `1`. If `key` does not exist in the d... | null |
Very easy and simple using Hashing Technique + beat 97% both in Time and Space | all-oone-data-structure | 0 | 1 | # Intuition\nThis code implements an "All O(1) Data Structure" using two dictionaries, self.keys and self.count, to manage keys and their counts efficiently. The keys dictionary is organized by counts, and the count dictionary tracks the count of each key. The inc and dec methods adjust counts and update dictionaries, ... | 0 | Design a data structure to store the strings' count with the ability to return the strings with minimum and maximum counts.
Implement the `AllOne` class:
* `AllOne()` Initializes the object of the data structure.
* `inc(String key)` Increments the count of the string `key` by `1`. If `key` does not exist in the d... | null |
Python3 O(1) amortized | dictionary + arraylist only - no linked list or deque | 64 ms (beats 93%) | all-oone-data-structure | 1 | 1 | \n\n# Code\n```\nclass AllOne:\n\n def __init__(self):\n self.key_count = {}\n self.count_key = {}\n\n def inc(self, key: str) -> None:\n count = self.key_count.get(key, 0)\n new_count = count + 1\n\n # Update key_count dictionary\n self.key_count[key] = new_count\n\n ... | 0 | Design a data structure to store the strings' count with the ability to return the strings with minimum and maximum counts.
Implement the `AllOne` class:
* `AllOne()` Initializes the object of the data structure.
* `inc(String key)` Increments the count of the string `key` by `1`. If `key` does not exist in the d... | null |
python code with comments | all-oone-data-structure | 0 | 1 | \n\n# Code\n```\nfrom sortedcontainers import SortedDict\nfrom collections import defaultdict\n\nclass AllOne:\n def __init__(self):\n # Dictionary to store the frequency of each word\n self.word_to_count = {}\n # Sorted dictionary to map a count to a set of words with that count\n self.c... | 0 | Design a data structure to store the strings' count with the ability to return the strings with minimum and maximum counts.
Implement the `AllOne` class:
* `AllOne()` Initializes the object of the data structure.
* `inc(String key)` Increments the count of the string `key` by `1`. If `key` does not exist in the d... | null |
Using two dictionaries - O(1) average | all-oone-data-structure | 0 | 1 | # Intuition\nThe idea is to keep track of the minimum occurrence number of greater than 1. This keeps the retrievals O(1)-average.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nUsed two dictionaries: d_count[i]: retains the set of words with i occurrence\n d_word[key]: ret... | 0 | Design a data structure to store the strings' count with the ability to return the strings with minimum and maximum counts.
Implement the `AllOne` class:
* `AllOne()` Initializes the object of the data structure.
* `inc(String key)` Increments the count of the string `key` by `1`. If `key` does not exist in the d... | null |
Functions to add/delete in linkedlist to keep things simple | all-oone-data-structure | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nCreating quick handy functions for addition and deletion of a node from the frequency doubly linked list makes the solution very handy\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexi... | 0 | Design a data structure to store the strings' count with the ability to return the strings with minimum and maximum counts.
Implement the `AllOne` class:
* `AllOne()` Initializes the object of the data structure.
* `inc(String key)` Increments the count of the string `key` by `1`. If `key` does not exist in the d... | null |
Python DFS, easy understanding | minimum-genetic-mutation | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTreat the problem as a graph traversal problem, such that you have to traverse from start to end with the help of bank.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nCollect all edges b/n 2 bank strings having on... | 1 | A gene string can be represented by an 8-character long string, with choices from `'A'`, `'C'`, `'G'`, and `'T'`.
Suppose we need to investigate a mutation from a gene string `startGene` to a gene string `endGene` where one mutation is defined as one single character changed in the gene string.
* For example, `"AAC... | null |
433: Solution with step by step explanation | minimum-genetic-mutation | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. The first line of the method converts the list of strings bank into a set for faster lookup.\n\n2. The second line checks if the end string is not present in the bank set. If it\'s not present, then there\'s no way to rea... | 8 | A gene string can be represented by an 8-character long string, with choices from `'A'`, `'C'`, `'G'`, and `'T'`.
Suppose we need to investigate a mutation from a gene string `startGene` to a gene string `endGene` where one mutation is defined as one single character changed in the gene string.
* For example, `"AAC... | null |
Faster Than 90% Python Sol | number-of-segments-in-a-string | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | Given a string `s`, return _the number of segments in the string_.
A **segment** is defined to be a contiguous sequence of **non-space characters**.
**Example 1:**
**Input:** s = "Hello, my name is John "
**Output:** 5
**Explanation:** The five segments are \[ "Hello, ", "my ", "name ", "is ", "John "\]
**Exam... | null |
😎Brute Force to Efficient Method 100% beat🤩 | Java | C++ | Python | JavaScript | number-of-segments-in-a-string | 1 | 1 | 1. Java\n2. C++\n3. Python\n4. JavaScript\n\n\n# Java\n## Java 1st Method :- Brute force\n\n1. We split the input string s into an array of words using the split method with a space as the delimiter. This will give us an array of substrings separated by spaces.\n2. We initialize a variable count to keep track of the nu... | 12 | Given a string `s`, return _the number of segments in the string_.
A **segment** is defined to be a contiguous sequence of **non-space characters**.
**Example 1:**
**Input:** s = "Hello, my name is John "
**Output:** 5
**Explanation:** The five segments are \[ "Hello, ", "my ", "name ", "is ", "John "\]
**Exam... | null |
EASY TO UNDERSTAND NOOB CODE | number-of-segments-in-a-string | 0 | 1 | # Intuition\n- Using split() function store the strings into a list \n# Approach\n- Splits the input string into segments, filters out the empty segments, and returns the count of non-empty segments. \n- This approach ensures that multiple consecutive spaces are treated as a single delimiter, and empty segments (result... | 2 | Given a string `s`, return _the number of segments in the string_.
A **segment** is defined to be a contiguous sequence of **non-space characters**.
**Example 1:**
**Input:** s = "Hello, my name is John "
**Output:** 5
**Explanation:** The five segments are \[ "Hello, ", "my ", "name ", "is ", "John "\]
**Exam... | null |
Python 91% Faster - Simple Solution | number-of-segments-in-a-string | 0 | 1 | An easy to follow solution\n\n```\nclass Solution:\n def countSegments(self, s: str) -> int:\n \n #create a list based on a space split\n slist = list(s.split(" "))\n \n #return the len of list minus empty item\n return(len(slist)-slist.count(""))\n``` | 1 | Given a string `s`, return _the number of segments in the string_.
A **segment** is defined to be a contiguous sequence of **non-space characters**.
**Example 1:**
**Input:** s = "Hello, my name is John "
**Output:** 5
**Explanation:** The five segments are \[ "Hello, ", "my ", "name ", "is ", "John "\]
**Exam... | null |
434: Space 90.14%, Solution with step by step explanation | number-of-segments-in-a-string | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Initialize the count variable to 0. This variable will keep track of the number of segments in the input string.\n```\ncount = 0\n```\n2. Iterate over each character in the input string using a for loop and the range func... | 7 | Given a string `s`, return _the number of segments in the string_.
A **segment** is defined to be a contiguous sequence of **non-space characters**.
**Example 1:**
**Input:** s = "Hello, my name is John "
**Output:** 5
**Explanation:** The five segments are \[ "Hello, ", "my ", "name ", "is ", "John "\]
**Exam... | null |
Python || easy to understand | number-of-segments-in-a-string | 0 | 1 | easy simple solution\n\n\tcount = 0\n for i in (s.strip(\' \').split(\' \')):\n if i != \'\':\n count += 1\n \n return count | 5 | Given a string `s`, return _the number of segments in the string_.
A **segment** is defined to be a contiguous sequence of **non-space characters**.
**Example 1:**
**Input:** s = "Hello, my name is John "
**Output:** 5
**Explanation:** The five segments are \[ "Hello, ", "my ", "name ", "is ", "John "\]
**Exam... | null |
✅ One Line Solution || Easy to Understand || RegExp || Javascript || Python3 || Go | number-of-segments-in-a-string | 0 | 1 | # Intuition\nUse RegExp to find all matched string and return the length of result.\n\n# Approach\nUse ([^\\s]+) for matching strings that not contain spaces.\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(... | 1 | Given a string `s`, return _the number of segments in the string_.
A **segment** is defined to be a contiguous sequence of **non-space characters**.
**Example 1:**
**Input:** s = "Hello, my name is John "
**Output:** 5
**Explanation:** The five segments are \[ "Hello, ", "my ", "name ", "is ", "John "\]
**Exam... | null |
Different one-liners in Python | number-of-segments-in-a-string | 0 | 1 | \n# Code\n```\nclass Solution:\n def countSegments(self, s: str) -> int:\n return len(s.split())\n```\n```\nclass Solution:\n def countSegments(self, s: str) -> int:\n return len([word for word in s.split() if word])\n```\n```\nclass Solution:\n def countSegments(self, s: str) -> int:\n re... | 2 | Given a string `s`, return _the number of segments in the string_.
A **segment** is defined to be a contiguous sequence of **non-space characters**.
**Example 1:**
**Input:** s = "Hello, my name is John "
**Output:** 5
**Explanation:** The five segments are \[ "Hello, ", "my ", "name ", "is ", "John "\]
**Exam... | null |
Number of segments in a string | number-of-segments-in-a-string | 0 | 1 | # 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)$$ --... | 4 | Given a string `s`, return _the number of segments in the string_.
A **segment** is defined to be a contiguous sequence of **non-space characters**.
**Example 1:**
**Input:** s = "Hello, my name is John "
**Output:** 5
**Explanation:** The five segments are \[ "Hello, ", "my ", "name ", "is ", "John "\]
**Exam... | null |
Number of Segments in a String | number-of-segments-in-a-string | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | Given a string `s`, return _the number of segments in the string_.
A **segment** is defined to be a contiguous sequence of **non-space characters**.
**Example 1:**
**Input:** s = "Hello, my name is John "
**Output:** 5
**Explanation:** The five segments are \[ "Hello, ", "my ", "name ", "is ", "John "\]
**Exam... | null |
Simple python answer | number-of-segments-in-a-string | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | Given a string `s`, return _the number of segments in the string_.
A **segment** is defined to be a contiguous sequence of **non-space characters**.
**Example 1:**
**Input:** s = "Hello, my name is John "
**Output:** 5
**Explanation:** The five segments are \[ "Hello, ", "my ", "name ", "is ", "John "\]
**Exam... | null |
A python3 solution | number-of-segments-in-a-string | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | Given a string `s`, return _the number of segments in the string_.
A **segment** is defined to be a contiguous sequence of **non-space characters**.
**Example 1:**
**Input:** s = "Hello, my name is John "
**Output:** 5
**Explanation:** The five segments are \[ "Hello, ", "my ", "name ", "is ", "John "\]
**Exam... | null |
One-liner | 4 ms | Beats 98.29% of users with Python | number-of-segments-in-a-string | 0 | 1 | # Approach\n<!-- Describe your approach to solving the problem. -->\nCreate a list from the string using split function (delimiter space). And return the length.\n\n# Complexity\n- Time complexity: *O(n), where n is the length of the string*\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n\n- Space complexity... | 0 | Given a string `s`, return _the number of segments in the string_.
A **segment** is defined to be a contiguous sequence of **non-space characters**.
**Example 1:**
**Input:** s = "Hello, my name is John "
**Output:** 5
**Explanation:** The five segments are \[ "Hello, ", "my ", "name ", "is ", "John "\]
**Exam... | null |
Single Line Code Faster than 93% of Solutions | number-of-segments-in-a-string | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSplit the given string with respect to " " and return the count of non empty strings.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n-> Split the string with respect to " " and convert it into list.\n-> This list ... | 0 | Given a string `s`, return _the number of segments in the string_.
A **segment** is defined to be a contiguous sequence of **non-space characters**.
**Example 1:**
**Input:** s = "Hello, my name is John "
**Output:** 5
**Explanation:** The five segments are \[ "Hello, ", "my ", "name ", "is ", "John "\]
**Exam... | null |
One Line Solution | number-of-segments-in-a-string | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | Given a string `s`, return _the number of segments in the string_.
A **segment** is defined to be a contiguous sequence of **non-space characters**.
**Example 1:**
**Input:** s = "Hello, my name is John "
**Output:** 5
**Explanation:** The five segments are \[ "Hello, ", "my ", "name ", "is ", "John "\]
**Exam... | null |
Simple intuition | number-of-segments-in-a-string | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIf you pad the string with a space in the beginning, then each segment starts after a space\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nUse a helper variable called `prev_char`. Initialize it to `\' \'`. Then ... | 0 | Given a string `s`, return _the number of segments in the string_.
A **segment** is defined to be a contiguous sequence of **non-space characters**.
**Example 1:**
**Input:** s = "Hello, my name is John "
**Output:** 5
**Explanation:** The five segments are \[ "Hello, ", "my ", "name ", "is ", "John "\]
**Exam... | null |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.