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 |
|---|---|---|---|---|---|---|---|
Depth-First Search✅ | O( n^2)✅ | Python(Step by step explanation)✅ | subtree-of-another-tree | 0 | 1 | # Intuition\nThe problem is to determine if one binary tree (subRoot) is a subtree of another binary tree (root). A subtree of a tree T is a tree consisting of a node in T and all of its descendants. The intuition is to perform a recursive comparison for each node in the root tree. If any subtree of the root tree match... | 9 | Given the roots of two binary trees `root` and `subRoot`, return `true` if there is a subtree of `root` with the same structure and node values of `subRoot` and `false` otherwise.
A subtree of a binary tree `tree` is a tree that consists of a node in `tree` and all of this node's descendants. The tree `tree` could als... | Which approach is better here- recursive or iterative? If recursive approach is better, can you write recursive function with its parameters? Two trees s and t are said to be identical if their root values are same and their left and right subtrees are identical. Can you write this in form of recursive formulae? Recurs... |
Depth-First Search✅ | O( n^2)✅ | Python(Step by step explanation)✅ | subtree-of-another-tree | 0 | 1 | # Intuition\nThe problem is to determine if one binary tree (subRoot) is a subtree of another binary tree (root). A subtree of a tree T is a tree consisting of a node in T and all of its descendants. The intuition is to perform a recursive comparison for each node in the root tree. If any subtree of the root tree match... | 9 | Given the roots of two binary trees `root` and `subRoot`, return `true` if there is a subtree of `root` with the same structure and node values of `subRoot` and `false` otherwise.
A subtree of a binary tree `tree` is a tree that consists of a node in `tree` and all of this node's descendants. The tree `tree` could als... | Which approach is better here- recursive or iterative? If recursive approach is better, can you write recursive function with its parameters? Two trees s and t are said to be identical if their root values are same and their left and right subtrees are identical. Can you write this in form of recursive formulae? Recurs... |
Unique O(|s| + |t|) solution using Eytzinger layout | subtree-of-another-tree | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe base idea is to encode whether left or right matches a part of the subtree by storing the Eytzinger value of that node.\nWe can then check if we would match an even bigger part of the subtree by including the current node. To check if... | 0 | Given the roots of two binary trees `root` and `subRoot`, return `true` if there is a subtree of `root` with the same structure and node values of `subRoot` and `false` otherwise.
A subtree of a binary tree `tree` is a tree that consists of a node in `tree` and all of this node's descendants. The tree `tree` could als... | Which approach is better here- recursive or iterative? If recursive approach is better, can you write recursive function with its parameters? Two trees s and t are said to be identical if their root values are same and their left and right subtrees are identical. Can you write this in form of recursive formulae? Recurs... |
Unique O(|s| + |t|) solution using Eytzinger layout | subtree-of-another-tree | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe base idea is to encode whether left or right matches a part of the subtree by storing the Eytzinger value of that node.\nWe can then check if we would match an even bigger part of the subtree by including the current node. To check if... | 0 | Given the roots of two binary trees `root` and `subRoot`, return `true` if there is a subtree of `root` with the same structure and node values of `subRoot` and `false` otherwise.
A subtree of a binary tree `tree` is a tree that consists of a node in `tree` and all of this node's descendants. The tree `tree` could als... | Which approach is better here- recursive or iterative? If recursive approach is better, can you write recursive function with its parameters? Two trees s and t are said to be identical if their root values are same and their left and right subtrees are identical. Can you write this in form of recursive formulae? Recurs... |
Solution | subtree-of-another-tree | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n\tbool isSame(TreeNode* p, TreeNode* q){\n\t\tif(p == NULL || q == NULL){\n\t\t\treturn (p == q);\n\t\t} \n\t\treturn (p->val == q->val) && isSame(p->left, q->left) && isSame(p->right, q->right);\n\t}\n\tbool isSubtree(TreeNode* root, TreeNode* subRoot) {\n\t\tif(root == NULL){\... | 2 | Given the roots of two binary trees `root` and `subRoot`, return `true` if there is a subtree of `root` with the same structure and node values of `subRoot` and `false` otherwise.
A subtree of a binary tree `tree` is a tree that consists of a node in `tree` and all of this node's descendants. The tree `tree` could als... | Which approach is better here- recursive or iterative? If recursive approach is better, can you write recursive function with its parameters? Two trees s and t are said to be identical if their root values are same and their left and right subtrees are identical. Can you write this in form of recursive formulae? Recurs... |
Solution | subtree-of-another-tree | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n\tbool isSame(TreeNode* p, TreeNode* q){\n\t\tif(p == NULL || q == NULL){\n\t\t\treturn (p == q);\n\t\t} \n\t\treturn (p->val == q->val) && isSame(p->left, q->left) && isSame(p->right, q->right);\n\t}\n\tbool isSubtree(TreeNode* root, TreeNode* subRoot) {\n\t\tif(root == NULL){\... | 2 | Given the roots of two binary trees `root` and `subRoot`, return `true` if there is a subtree of `root` with the same structure and node values of `subRoot` and `false` otherwise.
A subtree of a binary tree `tree` is a tree that consists of a node in `tree` and all of this node's descendants. The tree `tree` could als... | Which approach is better here- recursive or iterative? If recursive approach is better, can you write recursive function with its parameters? Two trees s and t are said to be identical if their root values are same and their left and right subtrees are identical. Can you write this in form of recursive formulae? Recurs... |
Python Easy to Understand | subtree-of-another-tree | 0 | 1 | ```\nclass Solution:\n def isSubtree(self, s: TreeNode, t: TreeNode) -> bool:\n if not s: \n return False\n if self.isSameTree(s, t): \n return True\n return self.isSubtree(s.left, t) or self.isSubtree(s.right, t)\n\n def isSameTree(self, p: TreeNode, q: TreeNode) -> boo... | 94 | Given the roots of two binary trees `root` and `subRoot`, return `true` if there is a subtree of `root` with the same structure and node values of `subRoot` and `false` otherwise.
A subtree of a binary tree `tree` is a tree that consists of a node in `tree` and all of this node's descendants. The tree `tree` could als... | Which approach is better here- recursive or iterative? If recursive approach is better, can you write recursive function with its parameters? Two trees s and t are said to be identical if their root values are same and their left and right subtrees are identical. Can you write this in form of recursive formulae? Recurs... |
Python Easy to Understand | subtree-of-another-tree | 0 | 1 | ```\nclass Solution:\n def isSubtree(self, s: TreeNode, t: TreeNode) -> bool:\n if not s: \n return False\n if self.isSameTree(s, t): \n return True\n return self.isSubtree(s.left, t) or self.isSubtree(s.right, t)\n\n def isSameTree(self, p: TreeNode, q: TreeNode) -> boo... | 94 | Given the roots of two binary trees `root` and `subRoot`, return `true` if there is a subtree of `root` with the same structure and node values of `subRoot` and `false` otherwise.
A subtree of a binary tree `tree` is a tree that consists of a node in `tree` and all of this node's descendants. The tree `tree` could als... | Which approach is better here- recursive or iterative? If recursive approach is better, can you write recursive function with its parameters? Two trees s and t are said to be identical if their root values are same and their left and right subtrees are identical. Can you write this in form of recursive formulae? Recurs... |
Python 98% speed with comments | subtree-of-another-tree | 0 | 1 | So this is just the implementation of the 1. solution in the problem.\nWe make a recursive `traverse_tree` function that will return a string representation of a tree, and then we just need to convert both trees to strings and check if tree `t` is substring of tree `s`\n```\nclass Solution:\n def isSubtree(self, s: ... | 58 | Given the roots of two binary trees `root` and `subRoot`, return `true` if there is a subtree of `root` with the same structure and node values of `subRoot` and `false` otherwise.
A subtree of a binary tree `tree` is a tree that consists of a node in `tree` and all of this node's descendants. The tree `tree` could als... | Which approach is better here- recursive or iterative? If recursive approach is better, can you write recursive function with its parameters? Two trees s and t are said to be identical if their root values are same and their left and right subtrees are identical. Can you write this in form of recursive formulae? Recurs... |
Python 98% speed with comments | subtree-of-another-tree | 0 | 1 | So this is just the implementation of the 1. solution in the problem.\nWe make a recursive `traverse_tree` function that will return a string representation of a tree, and then we just need to convert both trees to strings and check if tree `t` is substring of tree `s`\n```\nclass Solution:\n def isSubtree(self, s: ... | 58 | Given the roots of two binary trees `root` and `subRoot`, return `true` if there is a subtree of `root` with the same structure and node values of `subRoot` and `false` otherwise.
A subtree of a binary tree `tree` is a tree that consists of a node in `tree` and all of this node's descendants. The tree `tree` could als... | Which approach is better here- recursive or iterative? If recursive approach is better, can you write recursive function with its parameters? Two trees s and t are said to be identical if their root values are same and their left and right subtrees are identical. Can you write this in form of recursive formulae? Recurs... |
Python DFS + Recursive comparison [faster than 99%] | subtree-of-another-tree | 0 | 1 | * First, find all possible starting nodes, which are equal to subRoot\n\t* consider the tree: [1,1] and subRoot [1]\n\t* we would find starting nodes as two \'1\', the second one == SubRoot\n\n* for every possible starting node, compare the subtrees\n\n `def isSubtree_helper(self, root1, root2):`\n \n ... | 0 | Given the roots of two binary trees `root` and `subRoot`, return `true` if there is a subtree of `root` with the same structure and node values of `subRoot` and `false` otherwise.
A subtree of a binary tree `tree` is a tree that consists of a node in `tree` and all of this node's descendants. The tree `tree` could als... | Which approach is better here- recursive or iterative? If recursive approach is better, can you write recursive function with its parameters? Two trees s and t are said to be identical if their root values are same and their left and right subtrees are identical. Can you write this in form of recursive formulae? Recurs... |
Python DFS + Recursive comparison [faster than 99%] | subtree-of-another-tree | 0 | 1 | * First, find all possible starting nodes, which are equal to subRoot\n\t* consider the tree: [1,1] and subRoot [1]\n\t* we would find starting nodes as two \'1\', the second one == SubRoot\n\n* for every possible starting node, compare the subtrees\n\n `def isSubtree_helper(self, root1, root2):`\n \n ... | 0 | Given the roots of two binary trees `root` and `subRoot`, return `true` if there is a subtree of `root` with the same structure and node values of `subRoot` and `false` otherwise.
A subtree of a binary tree `tree` is a tree that consists of a node in `tree` and all of this node's descendants. The tree `tree` could als... | Which approach is better here- recursive or iterative? If recursive approach is better, can you write recursive function with its parameters? Two trees s and t are said to be identical if their root values are same and their left and right subtrees are identical. Can you write this in form of recursive formulae? Recurs... |
572: Solution with step by step explanation | subtree-of-another-tree | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Define a function checkEqual to check if two trees are equal in value and structure. The function takes two tree nodes root1 and root2 as input and returns a boolean value.\n\n2. Within the checkEqual function, check if b... | 5 | Given the roots of two binary trees `root` and `subRoot`, return `true` if there is a subtree of `root` with the same structure and node values of `subRoot` and `false` otherwise.
A subtree of a binary tree `tree` is a tree that consists of a node in `tree` and all of this node's descendants. The tree `tree` could als... | Which approach is better here- recursive or iterative? If recursive approach is better, can you write recursive function with its parameters? Two trees s and t are said to be identical if their root values are same and their left and right subtrees are identical. Can you write this in form of recursive formulae? Recurs... |
572: Solution with step by step explanation | subtree-of-another-tree | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Define a function checkEqual to check if two trees are equal in value and structure. The function takes two tree nodes root1 and root2 as input and returns a boolean value.\n\n2. Within the checkEqual function, check if b... | 5 | Given the roots of two binary trees `root` and `subRoot`, return `true` if there is a subtree of `root` with the same structure and node values of `subRoot` and `false` otherwise.
A subtree of a binary tree `tree` is a tree that consists of a node in `tree` and all of this node's descendants. The tree `tree` could als... | Which approach is better here- recursive or iterative? If recursive approach is better, can you write recursive function with its parameters? Two trees s and t are said to be identical if their root values are same and their left and right subtrees are identical. Can you write this in form of recursive formulae? Recurs... |
🔥Python||Python3|| esay soultion to understand🔥 | distribute-candies | 0 | 1 | # Approach\nMy approach was to loop over the candys, each candy I see I will add to the result counter and save it in a hashset. If we see a candy that already was seen then we skip it, else we add it. we do it until we reached n/2 candys or we got to the end of the list.\n\n# Complexity\n- Time complexity:\nO(n) becua... | 1 | Alice has `n` candies, where the `ith` candy is of type `candyType[i]`. Alice noticed that she started to gain weight, so she visited a doctor.
The doctor advised Alice to only eat `n / 2` of the candies she has (`n` is always even). Alice likes her candies very much, and she wants to eat the maximum number of differe... | To maximize the number of kinds of candies, we should try to distribute candies such that sister will gain all kinds. What is the upper limit of the number of kinds of candies sister will gain? Remember candies are to distributed equally. Which data structure is the most suitable for finding the number of kinds of cand... |
575: Time 92.8%, Solution with step by step explanation | distribute-candies | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Initialize a set to store unique candy types.\n2. Traverse through the input array and add each candy type to the set.\n3. Find the length of the input array and divide it by 2 to get the maximum number of candies Alice c... | 5 | Alice has `n` candies, where the `ith` candy is of type `candyType[i]`. Alice noticed that she started to gain weight, so she visited a doctor.
The doctor advised Alice to only eat `n / 2` of the candies she has (`n` is always even). Alice likes her candies very much, and she wants to eat the maximum number of differe... | To maximize the number of kinds of candies, we should try to distribute candies such that sister will gain all kinds. What is the upper limit of the number of kinds of candies sister will gain? Remember candies are to distributed equally. Which data structure is the most suitable for finding the number of kinds of cand... |
Python 3-lines ✅✅✅ || Faster than 98.93% || Simple + Explained | distribute-candies | 0 | 1 | **Note:** In the tags, I put Ordered Set there but the set doesn\'t actually have any order.\n## Please \uD83D\uDC4D\uD83C\uDFFB\uD83D\uDC4D\uD83C\uDFFB\uD83D\uDC4D\uD83C\uDFFB\uD83D\uDC4D\uD83C\uDFFB\uD83D\uDC4D\uD83C\uDFFB\uD83D\uDC4D\uD83C\uDFFB if u like!!\n# Code\n```\nclass Solution:\n def distributeCandies(se... | 2 | Alice has `n` candies, where the `ith` candy is of type `candyType[i]`. Alice noticed that she started to gain weight, so she visited a doctor.
The doctor advised Alice to only eat `n / 2` of the candies she has (`n` is always even). Alice likes her candies very much, and she wants to eat the maximum number of differe... | To maximize the number of kinds of candies, we should try to distribute candies such that sister will gain all kinds. What is the upper limit of the number of kinds of candies sister will gain? Remember candies are to distributed equally. Which data structure is the most suitable for finding the number of kinds of cand... |
✔Beats 100% TC || Simple if else || Easily Understandable Python Solution | distribute-candies | 0 | 1 | # Approach\n - Converts the array ```candyType``` to a set which gonna contain only discrete values\n - if ```eat <= len(dis_candyType)``` it means there are enough options available (i.e. Here every candy Alice eats is of different type)\n - elif ```eat > len(dis_candyType)``` not enough options available \n (i.e. Her... | 3 | Alice has `n` candies, where the `ith` candy is of type `candyType[i]`. Alice noticed that she started to gain weight, so she visited a doctor.
The doctor advised Alice to only eat `n / 2` of the candies she has (`n` is always even). Alice likes her candies very much, and she wants to eat the maximum number of differe... | To maximize the number of kinds of candies, we should try to distribute candies such that sister will gain all kinds. What is the upper limit of the number of kinds of candies sister will gain? Remember candies are to distributed equally. Which data structure is the most suitable for finding the number of kinds of cand... |
Python | Easy Solution✅ | distribute-candies | 0 | 1 | ```\ndef distributeCandies(self, candyType: List[int]) -> int:\n # half length of candyType list\n candy_len = int(len(candyType)/2)\n # no of of unique candies \n unique_candy_len = len(set(candyType))\n return min(candy_len,unique_candy_len)\n``` | 9 | Alice has `n` candies, where the `ith` candy is of type `candyType[i]`. Alice noticed that she started to gain weight, so she visited a doctor.
The doctor advised Alice to only eat `n / 2` of the candies she has (`n` is always even). Alice likes her candies very much, and she wants to eat the maximum number of differe... | To maximize the number of kinds of candies, we should try to distribute candies such that sister will gain all kinds. What is the upper limit of the number of kinds of candies sister will gain? Remember candies are to distributed equally. Which data structure is the most suitable for finding the number of kinds of cand... |
single line python code : | distribute-candies | 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. -->\nwe take the given array and make it a set and compare it with half of the length of the given array and take the least value among them \n\n# Complexity\n- Time comple... | 2 | Alice has `n` candies, where the `ith` candy is of type `candyType[i]`. Alice noticed that she started to gain weight, so she visited a doctor.
The doctor advised Alice to only eat `n / 2` of the candies she has (`n` is always even). Alice likes her candies very much, and she wants to eat the maximum number of differe... | To maximize the number of kinds of candies, we should try to distribute candies such that sister will gain all kinds. What is the upper limit of the number of kinds of candies sister will gain? Remember candies are to distributed equally. Which data structure is the most suitable for finding the number of kinds of cand... |
Solution | distribute-candies | 1 | 1 | ```C++ []\nclass Solution {\n public:\n int distributeCandies(vector<int>& candies) {\n bitset<200001> bitset;\n\n for (const int candy : candies)\n bitset.set(candy + 100000);\n\n return min(candies.size() / 2, bitset.count());\n }\n};\n```\n\n```Python3 []\nclass Solution:\n def distributeCandies(s... | 2 | Alice has `n` candies, where the `ith` candy is of type `candyType[i]`. Alice noticed that she started to gain weight, so she visited a doctor.
The doctor advised Alice to only eat `n / 2` of the candies she has (`n` is always even). Alice likes her candies very much, and she wants to eat the maximum number of differe... | To maximize the number of kinds of candies, we should try to distribute candies such that sister will gain all kinds. What is the upper limit of the number of kinds of candies sister will gain? Remember candies are to distributed equally. Which data structure is the most suitable for finding the number of kinds of cand... |
Solution | out-of-boundary-paths | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int dp[51][51][51];\n long mod = 1e9+7;\n \n int dfs(int m,int n,int mvs,int row,int col){\n if(row >= m || col >= n || row < 0 || col < 0)\n return 1;\n if(!mvs)\n return 0;\n if(dp[row][col][mvs] != -1)\n return d... | 1 | There is an `m x n` grid with a ball. The ball is initially at the position `[startRow, startColumn]`. You are allowed to move the ball to one of the four adjacent cells in the grid (possibly out of the grid crossing the grid boundary). You can apply **at most** `maxMove` moves to the ball.
Given the five integers `m`... | Is traversing every path feasible? There are many possible paths for a small matrix. Try to optimize it. Can we use some space to store the number of paths and update them after every move? One obvious thing: the ball will go out of the boundary only by crossing it. Also, there is only one possible way the ball can go ... |
Solution | out-of-boundary-paths | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int dp[51][51][51];\n long mod = 1e9+7;\n \n int dfs(int m,int n,int mvs,int row,int col){\n if(row >= m || col >= n || row < 0 || col < 0)\n return 1;\n if(!mvs)\n return 0;\n if(dp[row][col][mvs] != -1)\n return d... | 1 | There is an `m x n` grid with a ball. The ball is initially at the position `[startRow, startColumn]`. You are allowed to move the ball to one of the four adjacent cells in the grid (possibly out of the grid crossing the grid boundary). You can apply **at most** `maxMove` moves to the ball.
Given the five integers `m`... | Is traversing every path feasible? There are many possible paths for a small matrix. Try to optimize it. Can we use some space to store the number of paths and update them after every move? One obvious thing: the ball will go out of the boundary only by crossing it. Also, there is only one possible way the ball can go ... |
576: Solution with step by step explanation | out-of-boundary-paths | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Define the variable MOD as 10**9 + 7.\n2. Initialize a 3D list dp of dimensions m x n x (maxMove+1) with all elements as 0.\n3. Set dp[startRow][startColumn][0] to 1 since we start from the given startRow and startColumn.... | 2 | There is an `m x n` grid with a ball. The ball is initially at the position `[startRow, startColumn]`. You are allowed to move the ball to one of the four adjacent cells in the grid (possibly out of the grid crossing the grid boundary). You can apply **at most** `maxMove` moves to the ball.
Given the five integers `m`... | Is traversing every path feasible? There are many possible paths for a small matrix. Try to optimize it. Can we use some space to store the number of paths and update them after every move? One obvious thing: the ball will go out of the boundary only by crossing it. Also, there is only one possible way the ball can go ... |
576: Solution with step by step explanation | out-of-boundary-paths | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Define the variable MOD as 10**9 + 7.\n2. Initialize a 3D list dp of dimensions m x n x (maxMove+1) with all elements as 0.\n3. Set dp[startRow][startColumn][0] to 1 since we start from the given startRow and startColumn.... | 2 | There is an `m x n` grid with a ball. The ball is initially at the position `[startRow, startColumn]`. You are allowed to move the ball to one of the four adjacent cells in the grid (possibly out of the grid crossing the grid boundary). You can apply **at most** `maxMove` moves to the ball.
Given the five integers `m`... | Is traversing every path feasible? There are many possible paths for a small matrix. Try to optimize it. Can we use some space to store the number of paths and update them after every move? One obvious thing: the ball will go out of the boundary only by crossing it. Also, there is only one possible way the ball can go ... |
Python3 || recursion , one grid w/ explanation || T/M: 89%/49% | out-of-boundary-paths | 0 | 1 | ```\nclass Solution: # The plan is to accrete the number of paths from the starting cell, which\n # is the sum of (a) the number of adjacent positions that are off the grid\n # and (b) the number of paths from the adjacent cells in the grid within \n ... | 5 | There is an `m x n` grid with a ball. The ball is initially at the position `[startRow, startColumn]`. You are allowed to move the ball to one of the four adjacent cells in the grid (possibly out of the grid crossing the grid boundary). You can apply **at most** `maxMove` moves to the ball.
Given the five integers `m`... | Is traversing every path feasible? There are many possible paths for a small matrix. Try to optimize it. Can we use some space to store the number of paths and update them after every move? One obvious thing: the ball will go out of the boundary only by crossing it. Also, there is only one possible way the ball can go ... |
Python3 || recursion , one grid w/ explanation || T/M: 89%/49% | out-of-boundary-paths | 0 | 1 | ```\nclass Solution: # The plan is to accrete the number of paths from the starting cell, which\n # is the sum of (a) the number of adjacent positions that are off the grid\n # and (b) the number of paths from the adjacent cells in the grid within \n ... | 5 | There is an `m x n` grid with a ball. The ball is initially at the position `[startRow, startColumn]`. You are allowed to move the ball to one of the four adjacent cells in the grid (possibly out of the grid crossing the grid boundary). You can apply **at most** `maxMove` moves to the ball.
Given the five integers `m`... | Is traversing every path feasible? There are many possible paths for a small matrix. Try to optimize it. Can we use some space to store the number of paths and update them after every move? One obvious thing: the ball will go out of the boundary only by crossing it. Also, there is only one possible way the ball can go ... |
Just sort the array | Python | shortest-unsorted-continuous-subarray | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIf a number is not in it\'s poisition then we need to start the sorting from there. This is form left to right, same thing for right to left. \n# Approach\n<!-- Describe your approach to solving the problem. -->\nJust sort the array, comp... | 1 | Given an integer array `nums`, you need to find one **continuous subarray** that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order.
Return _the shortest such subarray and output its length_.
**Example 1:**
**Input:** nums = \[2,6,4,8,10,9,15\]
**Output:** 5
**E... | null |
Solution | shortest-unsorted-continuous-subarray | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n\n bool outOfOrder(vector<int> &nums, int i){\n if(nums.size() == 1) return 0;\n int x = nums[i];\n\n if(i == 0) return x > nums[1];\n if(i == nums.size()-1) return x < nums[i-1];\n\n return x > nums[i+1] or x < nums[i-1];\n }\n int findU... | 2 | Given an integer array `nums`, you need to find one **continuous subarray** that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order.
Return _the shortest such subarray and output its length_.
**Example 1:**
**Input:** nums = \[2,6,4,8,10,9,15\]
**Output:** 5
**E... | null |
581: Solution with step by step explanation | shortest-unsorted-continuous-subarray | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Make a sorted copy of the input array nums and store it in the variable sorted_nums.\n2. Initialize two variables start and end to invalid indices. We will use these to keep track of the start and end of the shortest unso... | 4 | Given an integer array `nums`, you need to find one **continuous subarray** that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order.
Return _the shortest such subarray and output its length_.
**Example 1:**
**Input:** nums = \[2,6,4,8,10,9,15\]
**Output:** 5
**E... | null |
simplest solution | shortest-unsorted-continuous-subarray | 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)$$ --... | 2 | Given an integer array `nums`, you need to find one **continuous subarray** that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order.
Return _the shortest such subarray and output its length_.
**Example 1:**
**Input:** nums = \[2,6,4,8,10,9,15\]
**Output:** 5
**E... | null |
O(n) CPP + O(nlogn) Python With Explanation From Scratch | shortest-unsorted-continuous-subarray | 0 | 1 | ## Explanation\n```\n nums = 1 3 2 4 7 6 8 9\nsorted_nums = 1 2 3 4 6 7 8 9 -- This is an increasing array, where every nums[i]\nis greater than all of its left side values.\n\n In nums = 1 3 2 4 7 6 8 9\n 0 1 2 3 4 5 6 7 (indexes)\n\n 1. nums[0] & n... | 3 | Given an integer array `nums`, you need to find one **continuous subarray** that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order.
Return _the shortest such subarray and output its length_.
**Example 1:**
**Input:** nums = \[2,6,4,8,10,9,15\]
**Output:** 5
**E... | null |
Python Simple Two Approaches | shortest-unsorted-continuous-subarray | 0 | 1 | 1. ***Sorting***\nThe algorithm goes like this:\n\t1. Create a sorted clone of the original list. \n\t2. Compare the elements at same index in both the list. Once you encounter a mismatch, use that to find the lower and upper bounds of the unsorted subarray.\n\n\n```\nclass Solution:\n def findUnsortedSubarray(self... | 12 | Given an integer array `nums`, you need to find one **continuous subarray** that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order.
Return _the shortest such subarray and output its length_.
**Example 1:**
**Input:** nums = \[2,6,4,8,10,9,15\]
**Output:** 5
**E... | null |
Python 3 solution using a sort and two pointers | shortest-unsorted-continuous-subarray | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe want to know: where does nums first differ from a sorted list of the same elements, when comparing from the left and from the right?\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Make a sorted copy of **nums**... | 1 | Given an integer array `nums`, you need to find one **continuous subarray** that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order.
Return _the shortest such subarray and output its length_.
**Example 1:**
**Input:** nums = \[2,6,4,8,10,9,15\]
**Output:** 5
**E... | null |
Python (99.64 %beats) || Tabulation || DP || Using LCS | delete-operation-for-two-strings | 0 | 1 | \n> **First Solve Leetcode Ques No. ->> 1143 - Longest Common Subsequence** \n\n[Navigate to Leetcode problem 1143. ](https://leetcode.com/problems/longest-common-subsequence/solutions/4122297/python-96-beats-tabulation-dp-memorization-optimize-way-2d-matrix-way/)\n\n# Code\n```\nclass Solution:\n def minDistance(s... | 1 | Given two strings `word1` and `word2`, return _the minimum number of **steps** required to make_ `word1` _and_ `word2` _the same_.
In one **step**, you can delete exactly one character in either string.
**Example 1:**
**Input:** word1 = "sea ", word2 = "eat "
**Output:** 2
**Explanation:** You need one step to mak... | null |
Solution | delete-operation-for-two-strings | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int lengthLcs(string text1, string text2) {\n short int l1 = text1.size()+1;\n short int l2 = text2.size()+1;\n short int count[l2][l1], i, j;\n for(i=0; i<l1; i++) count[0][i]=0;\n for(i=0; i<l2; i++) count[i][0]=0;\n for(i=1; i<l2; i+... | 1 | Given two strings `word1` and `word2`, return _the minimum number of **steps** required to make_ `word1` _and_ `word2` _the same_.
In one **step**, you can delete exactly one character in either string.
**Example 1:**
**Input:** word1 = "sea ", word2 = "eat "
**Output:** 2
**Explanation:** You need one step to mak... | null |
Python || easy solu|| using DP bottomup | delete-operation-for-two-strings | 0 | 1 | ```\nclass Solution:\n def minDistance(self, word1: str, word2: str) -> int:\n m=len(word1)\n n=len(word2)\n dp=[]\n for i in range (m+1):\n dp.append([0]*(n+1))\n for i in range (m+1):\n dp[i][0]=i\n for i in range (n+1):\n dp[0][i]=i\n ... | 1 | Given two strings `word1` and `word2`, return _the minimum number of **steps** required to make_ `word1` _and_ `word2` _the same_.
In one **step**, you can delete exactly one character in either string.
**Example 1:**
**Input:** word1 = "sea ", word2 = "eat "
**Output:** 2
**Explanation:** You need one step to mak... | null |
583: Solution with step by step explanation | delete-operation-for-two-strings | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Get the lengths of the two input strings and store them in variables m and n.\n2. Initialize a dynamic programming table dp of size (m + 1) x (n + 1) with all entries initialized to 0.\n3. Use a nested loop to iterate ove... | 2 | Given two strings `word1` and `word2`, return _the minimum number of **steps** required to make_ `word1` _and_ `word2` _the same_.
In one **step**, you can delete exactly one character in either string.
**Example 1:**
**Input:** word1 = "sea ", word2 = "eat "
**Output:** 2
**Explanation:** You need one step to mak... | null |
Python || 98.80% Faster || DP || Space Optimization || LCS | delete-operation-for-two-strings | 0 | 1 | ```\n#Tabulation(Top-Down)\n#Time Complexity: O(m*n)\n#Space Complexity: O(m*n)\nclass Solution1:\n def minDistance(self, word1: str, word2: str) -> int:\n m,n=len(word1),len(word2)\n dp=[[0 for j in range(n+1)] for i in range(m+1)]\n for i in range(1,m+1):\n for j in range(1,n+1):\n ... | 1 | Given two strings `word1` and `word2`, return _the minimum number of **steps** required to make_ `word1` _and_ `word2` _the same_.
In one **step**, you can delete exactly one character in either string.
**Example 1:**
**Input:** word1 = "sea ", word2 = "eat "
**Output:** 2
**Explanation:** You need one step to mak... | null |
Python DP 2 approaches using LCS ✅ | delete-operation-for-two-strings | 0 | 1 | The prerequisite for this problem is [Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence/solution/). Please check and solve that problem first before you solve this. Once you have mastered LCS, this problem is easy.\n\n>eg: Input strings: **s1**="sea", **s2**="eat"\n\n**LCS** for above... | 51 | Given two strings `word1` and `word2`, return _the minimum number of **steps** required to make_ `word1` _and_ `word2` _the same_.
In one **step**, you can delete exactly one character in either string.
**Example 1:**
**Input:** word1 = "sea ", word2 = "eat "
**Output:** 2
**Explanation:** You need one step to mak... | null |
Simple DP || Python || Exact LCS Code | delete-operation-for-two-strings | 0 | 1 | # Intuition\nThe intuition is pretty similar to finding Longest Common subsequence and then delete all those characters which are extra and count all delete operations as number of steps required.\n\n# Approach\nWe need to make word1 == word2 but by deleting characters.\nFind the longest Common Subsequence and store it... | 1 | Given two strings `word1` and `word2`, return _the minimum number of **steps** required to make_ `word1` _and_ `word2` _the same_.
In one **step**, you can delete exactly one character in either string.
**Example 1:**
**Input:** word1 = "sea ", word2 = "eat "
**Output:** 2
**Explanation:** You need one step to mak... | null |
[Python 3] 5 Lines with top down DP, faster than 81.75% | delete-operation-for-two-strings | 0 | 1 | **Appreciate if you could upvote this solution**\n\n```\ndef minDistance(self, word1: str, word2: str) -> int:\n\tmin_distance_dp = [[i for i in range(len(word2)+1)]] + [[i] + [math.inf] * (len(word2)) for i in range(1, len(word1)+1)]\n\n\tfor i in range(len(word1)):\n\t\tfor j in range(len(word2)):\n\t\t\tmin_distance... | 29 | Given two strings `word1` and `word2`, return _the minimum number of **steps** required to make_ `word1` _and_ `word2` _the same_.
In one **step**, you can delete exactly one character in either string.
**Example 1:**
**Input:** word1 = "sea ", word2 = "eat "
**Output:** 2
**Explanation:** You need one step to mak... | null |
✔️ PYTHON || EXPLAINED || ;] | delete-operation-for-two-strings | 0 | 1 | **UPVOTE IF HELPFuuL**\n\nFrom both strings ```word1``` and ```word2``` only those haracter will be deleted which are not present in the other string.\n\nFor this we find ```LCS``` i.e. Longest Common Subsequence.\n\nWe create a matrix of size ```(m+1)*(n+1)``` and make first row and column equal to zero.\n\n* If ```i`... | 24 | Given two strings `word1` and `word2`, return _the minimum number of **steps** required to make_ `word1` _and_ `word2` _the same_.
In one **step**, you can delete exactly one character in either string.
**Example 1:**
**Input:** word1 = "sea ", word2 = "eat "
**Output:** 2
**Explanation:** You need one step to mak... | null |
✔️ Python Simple and Easy Way to Solve | 99% Faster 🔥 | erect-the-fence | 0 | 1 | **\uD83D\uDD3C IF YOU FIND THIS POST HELPFUL PLEASE UPVOTE \uD83D\uDC4D**\n```\nimport itertools\n\n# Monotone Chain Algorithm\nclass Solution(object):\n def outerTrees(self, points):\n\t\n def ccw(A, B, C):\n return (B[0]-A[0])*(C[1]-A[1]) - (B[1]-A[1])*(C[0]-A[0])\n\n if len(points) <= 1:\... | 4 | You are given an array `trees` where `trees[i] = [xi, yi]` represents the location of a tree in the garden.
Fence the entire garden using the minimum length of rope, as it is expensive. The garden is well-fenced only if **all the trees are enclosed**.
Return _the coordinates of trees that are exactly located on the f... | null |
Solution | erect-the-fence | 1 | 1 | ```C++ []\nstruct pt {\n int x, y;\n pt(int x, int y): x(x), y(y){}\n};\nint orientation(pt a, pt b, pt c) {\n int v = a.x*(b.y-c.y)+b.x*(c.y-a.y)+c.x*(a.y-b.y);\n if (v < 0) return -1;\n if (v > 0) return +1;\n return 0;\n}\nbool cw(pt a, pt b, pt c, bool include_collinear) {\n int o = orientation... | 1 | You are given an array `trees` where `trees[i] = [xi, yi]` represents the location of a tree in the garden.
Fence the entire garden using the minimum length of rope, as it is expensive. The garden is well-fenced only if **all the trees are enclosed**.
Return _the coordinates of trees that are exactly located on the f... | null |
Python = full explanation ! | erect-the-fence | 0 | 1 | Python3 Solve\nUsing convex hull algorithm with helper function.\n\nSummary:\n\nFind upper hull of polygon.\nFind lower hull of polygon.\nCombine both upper and lower hulls to form full polygon.\n\n---\n\nExplanation:\n\nHelper Function = comparing slopes:\n- Create Helper function to compare slopes of 2 different line... | 1 | You are given an array `trees` where `trees[i] = [xi, yi]` represents the location of a tree in the garden.
Fence the entire garden using the minimum length of rope, as it is expensive. The garden is well-fenced only if **all the trees are enclosed**.
Return _the coordinates of trees that are exactly located on the f... | null |
#easy-python3 #Erect the Fence🔥🔥🔥🔥🔥🔥🔥🔥🔥 | erect-the-fence | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nUsing convex hull algorithm also known as Gift wraping algorithm\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nusing distance formula inbetween the points (y3-y2)/(y2-y1)=(x3-x2)/(x2-x1)\nif value is >0 then it i... | 1 | You are given an array `trees` where `trees[i] = [xi, yi]` represents the location of a tree in the garden.
Fence the entire garden using the minimum length of rope, as it is expensive. The garden is well-fenced only if **all the trees are enclosed**.
Return _the coordinates of trees that are exactly located on the f... | null |
587: Solution with step by step explanation | erect-the-fence | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Create an empty list hull to store the points on the hull.\n2. Sort the trees list by x-coordinate first, and then y-coordinate (in case of ties).\n3. Define a helper function cross that takes three points as input and re... | 2 | You are given an array `trees` where `trees[i] = [xi, yi]` represents the location of a tree in the garden.
Fence the entire garden using the minimum length of rope, as it is expensive. The garden is well-fenced only if **all the trees are enclosed**.
Return _the coordinates of trees that are exactly located on the f... | null |
Very Easy || 100% || Fully Explained || Java, C++, Python, Python3 || Iterative & Recursive(DFS) | n-ary-tree-preorder-traversal | 1 | 1 | # **Java Solution (Iterative Approach):**\n```\nclass Solution {\n public List<Integer> preorder(Node root) {\n // To store the output array...\n List<Integer> output = new ArrayList<Integer>();\n // Base case: if the tree is empty...\n if (root == null) return output;\n // Create ... | 126 | Given the `root` of an n-ary tree, return _the preorder 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,5,6,2... | null |
Very Easy || 100% || Fully Explained || Java, C++, Python, Python3 || Iterative & Recursive(DFS) | n-ary-tree-preorder-traversal | 1 | 1 | # **Java Solution (Iterative Approach):**\n```\nclass Solution {\n public List<Integer> preorder(Node root) {\n // To store the output array...\n List<Integer> output = new ArrayList<Integer>();\n // Base case: if the tree is empty...\n if (root == null) return output;\n // Create ... | 126 | You are given an integer array `nums` of length `n` which represents a permutation of all the integers in the range `[0, n - 1]`.
The number of **global inversions** is the number of the different pairs `(i, j)` where:
* `0 <= i < j < n`
* `nums[i] > nums[j]`
The number of **local inversions** is the number of i... | null |
easiest solution 59ms | n-ary-tree-preorder-traversal | 0 | 1 | # Approach\n<!-- Describe your approach to solving the problem. -->\n- meet root first.\n- now meet children sub-tree and append value.\n- return ans.\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. $$... | 4 | Given the `root` of an n-ary tree, return _the preorder 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,5,6,2... | null |
easiest solution 59ms | n-ary-tree-preorder-traversal | 0 | 1 | # Approach\n<!-- Describe your approach to solving the problem. -->\n- meet root first.\n- now meet children sub-tree and append value.\n- return ans.\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. $$... | 4 | You are given an integer array `nums` of length `n` which represents a permutation of all the integers in the range `[0, n - 1]`.
The number of **global inversions** is the number of the different pairs `(i, j)` where:
* `0 <= i < j < n`
* `nums[i] > nums[j]`
The number of **local inversions** is the number of i... | null |
Python || PreOrder Traversal || Easy To Understand 🔥 | n-ary-tree-preorder-traversal | 0 | 1 | # Code\n```\nclass Solution:\n def preorder(self, root: \'Node\') -> List[int]:\n arr = []\n def order(root):\n if root is None: return None\n arr.append(root.val)\n for i in root.children:\n order(i) \n order(root)\n return arr\n``` | 3 | Given the `root` of an n-ary tree, return _the preorder 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,5,6,2... | null |
Python || PreOrder Traversal || Easy To Understand 🔥 | n-ary-tree-preorder-traversal | 0 | 1 | # Code\n```\nclass Solution:\n def preorder(self, root: \'Node\') -> List[int]:\n arr = []\n def order(root):\n if root is None: return None\n arr.append(root.val)\n for i in root.children:\n order(i) \n order(root)\n return arr\n``` | 3 | You are given an integer array `nums` of length `n` which represents a permutation of all the integers in the range `[0, n - 1]`.
The number of **global inversions** is the number of the different pairs `(i, j)` where:
* `0 <= i < j < n`
* `nums[i] > nums[j]`
The number of **local inversions** is the number of i... | null |
BEATS 90% of Submissions, EASY approach | n-ary-tree-preorder-traversal | 0 | 1 | # Approach\n\nRecursive solution with a while loop\n\n# Complexity\n- Time complexity: Beats 90%\n\n- Space complexity: Beats 55%\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n"""\n# Definition for a Node.\nclass Node:\n def __init__(self, val=None, children=None):\n self.val = val\n... | 4 | Given the `root` of an n-ary tree, return _the preorder 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,5,6,2... | null |
BEATS 90% of Submissions, EASY approach | n-ary-tree-preorder-traversal | 0 | 1 | # Approach\n\nRecursive solution with a while loop\n\n# Complexity\n- Time complexity: Beats 90%\n\n- Space complexity: Beats 55%\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n"""\n# Definition for a Node.\nclass Node:\n def __init__(self, val=None, children=None):\n self.val = val\n... | 4 | You are given an integer array `nums` of length `n` which represents a permutation of all the integers in the range `[0, n - 1]`.
The number of **global inversions** is the number of the different pairs `(i, j)` where:
* `0 <= i < j < n`
* `nums[i] > nums[j]`
The number of **local inversions** is the number of i... | null |
python3, simple DFS | n-ary-tree-preorder-traversal | 0 | 1 | ERROR: type should be string, got "https://leetcode.com/submissions/detail/869247259/ \\nRuntime: 48 ms, faster than 95.05% of Python3 online submissions for N-ary Tree Preorder Traversal. \\nMemory Usage: 16 MB, less than 97.63% of Python3 online submissions for N-ary Tree Preorder Traversal. \\n```\\n\"\"\"\\n# Definition for a Node.\\nclass Node:\\n def __init__(self, val=None, children=None):\\n self.val = val\\n self.children = children\\n\"\"\"\\nclass Solution:\\n def preorder(self, root: \\'Node\\') -> List[int]:\\n if not root: return\\n vals, l = [], [root]\\n while l:\\n node = l.pop(0)\\n vals.append(node.val)\\n l = node.children + l\\n return vals\\n```" | 6 | Given the `root` of an n-ary tree, return _the preorder 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,5,6,2... | null |
python3, simple DFS | n-ary-tree-preorder-traversal | 0 | 1 | ERROR: type should be string, got "https://leetcode.com/submissions/detail/869247259/ \\nRuntime: 48 ms, faster than 95.05% of Python3 online submissions for N-ary Tree Preorder Traversal. \\nMemory Usage: 16 MB, less than 97.63% of Python3 online submissions for N-ary Tree Preorder Traversal. \\n```\\n\"\"\"\\n# Definition for a Node.\\nclass Node:\\n def __init__(self, val=None, children=None):\\n self.val = val\\n self.children = children\\n\"\"\"\\nclass Solution:\\n def preorder(self, root: \\'Node\\') -> List[int]:\\n if not root: return\\n vals, l = [], [root]\\n while l:\\n node = l.pop(0)\\n vals.append(node.val)\\n l = node.children + l\\n return vals\\n```" | 6 | You are given an integer array `nums` of length `n` which represents a permutation of all the integers in the range `[0, n - 1]`.
The number of **global inversions** is the number of the different pairs `(i, j)` where:
* `0 <= i < j < n`
* `nums[i] > nums[j]`
The number of **local inversions** is the number of i... | null |
✔️ Python 2 Easy Way To Solve | 96% Faster | Recursive and Iterative Approach | n-ary-tree-preorder-traversal | 0 | 1 | **IF YOU FIND THIS POST HELPFUL PLEASE UPVOTE \uD83D\uDC4D**\n\n**Recursion:**\n\n```\nclass Solution:\n def preorder(self, root: \'Node\') -> List[int]:\n \n def dfs(root, output):\n if not root:\n return None\n output.append(root.val)\n for child in roo... | 20 | Given the `root` of an n-ary tree, return _the preorder 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,5,6,2... | null |
✔️ Python 2 Easy Way To Solve | 96% Faster | Recursive and Iterative Approach | n-ary-tree-preorder-traversal | 0 | 1 | **IF YOU FIND THIS POST HELPFUL PLEASE UPVOTE \uD83D\uDC4D**\n\n**Recursion:**\n\n```\nclass Solution:\n def preorder(self, root: \'Node\') -> List[int]:\n \n def dfs(root, output):\n if not root:\n return None\n output.append(root.val)\n for child in roo... | 20 | You are given an integer array `nums` of length `n` which represents a permutation of all the integers in the range `[0, n - 1]`.
The number of **global inversions** is the number of the different pairs `(i, j)` where:
* `0 <= i < j < n`
* `nums[i] > nums[j]`
The number of **local inversions** is the number of i... | null |
Python || PostOrder Traversal || Easy To Understand 🔥 | n-ary-tree-postorder-traversal | 0 | 1 | # Code\n```\nclass Solution:\n def postorder(self, root: \'Node\') -> List[int]:\n arr = []\n def order(root):\n if root is None: return None\n for i in root.children:\n order(i) \n arr.append(root.val)\n order(root)\n return arr\n``` | 1 | Given the `root` of an n-ary tree, return _the postorder 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:** \[5,6,3,2,... | null |
Solution | n-ary-tree-postorder-traversal | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n void post(Node* root,vector<int> &v)\n {\n if(root==NULL)\n return;\n for(auto temp : root->children)\n {\n post(temp,v); \n }\n v.push_back(root->val);\n }\n vector<int> postorder(Node* root) {\n vector<int> v... | 2 | Given the `root` of an n-ary tree, return _the postorder 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:** \[5,6,3,2,... | null |
Python3 easiest solution 87% fast | n-ary-tree-postorder-traversal | 0 | 1 | # Approach\n<!-- Describe your approach to solving the problem. -->\n- traverse left to right children sub tree.\n- then append root.\n- return ans\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(n... | 3 | Given the `root` of an n-ary tree, return _the postorder 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:** \[5,6,3,2,... | null |
590: Time 94.11%, Solution with step by step explanation | n-ary-tree-postorder-traversal | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. If the root is None, return an empty list.\n\n2. Initialize a stack called stack with the root node as its only element.\n\n3. Initialize an empty list called res to store the result.\n\n4. While the stack is not empty, p... | 3 | Given the `root` of an n-ary tree, return _the postorder 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:** \[5,6,3,2,... | null |
Both iterative and recursive | Faster than 95% | Easy to Understand | Simple | Python | n-ary-tree-postorder-traversal | 0 | 1 | ```\n def iterative(self, root):\n if not root: return []\n stack = [root]\n out = []\n while len(stack):\n top = stack.pop()\n out.append(top.val)\n stack.extend(top.children or [])\n return out[::-1]\n \n \n```\n\t\t\t\n\t\t\t\n \n ... | 33 | Given the `root` of an n-ary tree, return _the postorder 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:** \[5,6,3,2,... | null |
Python3 || easy method uses | n-ary-tree-postorder-traversal | 0 | 1 | \t\ttraversal = list()\n \n if root:\n for child in root.children:\n traversal+=self.postorder(child)\n \n traversal.append(root.val)\n \n return traversal | 4 | Given the `root` of an n-ary tree, return _the postorder 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:** \[5,6,3,2,... | null |
Solution | tag-validator | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n bool isValid(string code) {\n int i = 0;\n return validTag(code, i) && i == code.size();\n }\nprivate:\n bool validTag(string s, int& i) {\n int j = i;\n string tag = parseTagName(s, j);\n if (tag.empty()) return false;\n if (!val... | 1 | Given a string representing a code snippet, implement a tag validator to parse the code and return whether it is valid.
A code snippet is valid if all the following rules hold:
1. The code must be wrapped in a **valid closed tag**. Otherwise, the code is invalid.
2. A **closed tag** (not necessarily valid) has exac... | null |
591: Solution with step by step explanation | tag-validator | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. The function starts by checking if the input code starts and ends with \'<\' and \'>\', respectively. If it does not, it returns False since code must contain at least one tag.\n\n2. The function initializes two variables... | 3 | Given a string representing a code snippet, implement a tag validator to parse the code and return whether it is valid.
A code snippet is valid if all the following rules hold:
1. The code must be wrapped in a **valid closed tag**. Otherwise, the code is invalid.
2. A **closed tag** (not necessarily valid) has exac... | null |
py3 solution beats 50%+ | tag-validator | 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 representing a code snippet, implement a tag validator to parse the code and return whether it is valid.
A code snippet is valid if all the following rules hold:
1. The code must be wrapped in a **valid closed tag**. Otherwise, the code is invalid.
2. A **closed tag** (not necessarily valid) has exac... | null |
Using Regex to parse & validate code | tag-validator | 0 | 1 | # Approach\n<!-- Describe your approach to solving the problem. -->\nUse regex to parse & validate `code`\n# Code\n```\nfrom ast import Tuple\nimport re\nfrom typing import List\n\ndebug = False\n\nopen_or_close_tag_regex = re.compile(r"<[A-Za-z]*>|<\\/[A-Za-z]*>")\nopen_tag_regex = re.compile(r"<[A-Za-z]*>")\nclose_ta... | 0 | Given a string representing a code snippet, implement a tag validator to parse the code and return whether it is valid.
A code snippet is valid if all the following rules hold:
1. The code must be wrapped in a **valid closed tag**. Otherwise, the code is invalid.
2. A **closed tag** (not necessarily valid) has exac... | null |
py | tag-validator | 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 representing a code snippet, implement a tag validator to parse the code and return whether it is valid.
A code snippet is valid if all the following rules hold:
1. The code must be wrapped in a **valid closed tag**. Otherwise, the code is invalid.
2. A **closed tag** (not necessarily valid) has exac... | null |
Two Python Solution:(1) linear paring with stack;(2)BNF parsing with pyparsing | tag-validator | 0 | 1 | Solution 1:\n```\nclass Solution:\n def isValid(self, code: str) -> bool:\n if code[0] != \'<\' or code[-1] != \'>\': return False\n i, n = 0, len(code)\n stk = []\n while i < n:\n if code[i] == \'<\':\n if i != 0 and code[i: i + 9] == \'<. If your final result is an integer, change it to the format of a fractio... | null |
✅Easy Brute Force Solution✅ | fraction-addition-and-subtraction | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe code is designed to add fractions together and simplify the result to its simplest form. It handles both positive and negative fractions, and its goal is to perform this addition and provide the result as a simplified fraction.\n# App... | 1 | Given a string `expression` representing an expression of fraction addition and subtraction, return the calculation result in string format.
The final result should be an [irreducible fraction](https://en.wikipedia.org/wiki/Irreducible_fraction). If your final result is an integer, change it to the format of a fractio... | null |
Solution | fraction-addition-and-subtraction | 1 | 1 | ```C++ []\nclass Solution {\n public:\n string fractionAddition(string expression) {\n istringstream iss(expression);\n char _;\n int a;\n int b;\n int A = 0;\n int B = 1;\n\n while (iss >> a >> _ >> b) {\n A = A * b + a * B;\n B *= b;\n const int g = abs(__gcd(A, B));\n A /= g... | 1 | Given a string `expression` representing an expression of fraction addition and subtraction, return the calculation result in string format.
The final result should be an [irreducible fraction](https://en.wikipedia.org/wiki/Irreducible_fraction). If your final result is an integer, change it to the format of a fractio... | null |
Python Elegant & Short | RegEx | Two lines | fraction-addition-and-subtraction | 0 | 1 | \timport re\n\tfrom fractions import Fraction\n\n\n\tclass Solution:\n\t\t"""\n\t\tTime: O(n)\n\t\tMemory: O(n)\n\t\t"""\n\n\t\tFRACTION_PATTERN = r\'([+-]?[^+-]+)\'\n\t\t\n\t\t# First solution\n\t\tdef fractionAddition(self, expression: str) -> str:\n\t\t\tresult_fraction = Fraction(0)\n\n\t\t\tfor exp in re.findall... | 4 | Given a string `expression` representing an expression of fraction addition and subtraction, return the calculation result in string format.
The final result should be an [irreducible fraction](https://en.wikipedia.org/wiki/Irreducible_fraction). If your final result is an integer, change it to the format of a fractio... | null |
592: Space 98%, Solution with step by step explanation | fraction-addition-and-subtraction | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Split the expression string into individual fractions using the re.findall function and regular expression pattern [+-]?\\d+/\\d+. This will match any fraction in the expression, including negative fractions and fractions... | 5 | Given a string `expression` representing an expression of fraction addition and subtraction, return the calculation result in string format.
The final result should be an [irreducible fraction](https://en.wikipedia.org/wiki/Irreducible_fraction). If your final result is an integer, change it to the format of a fractio... | null |
Python two solutions👀. Short and Concise✅. Library and No Library🔥 | fraction-addition-and-subtraction | 0 | 1 | **1. A short solution using [fractions](https://docs.python.org/3/library/fractions.html) from python stdlib**\n```\nfrom fractions import Fraction\nclass Solution:\n def fractionAddition(self, expression: str) -> str:\n ans = Fraction(eval(expression))\n return \'/\'.join(map(str, ans.limit_denominato... | 2 | Given a string `expression` representing an expression of fraction addition and subtraction, return the calculation result in string format.
The final result should be an [irreducible fraction](https://en.wikipedia.org/wiki/Irreducible_fraction). If your final result is an integer, change it to the format of a fractio... | null |
[Python] Simple Parsing - with example | fraction-addition-and-subtraction | 0 | 1 | ```html5\n<b>Time Complexity: O(n + log(N·D))</b> where n = expression.length and (N, D) are the unreduced numerator and common denominator ∵ while-loop and gcd\n<b>Space Complexity: O(n)</b> ∵ num and den both scale with n and ∵ when the first fraction is positive: expression = \'+\' + expr... | 8 | Given a string `expression` representing an expression of fraction addition and subtraction, return the calculation result in string format.
The final result should be an [irreducible fraction](https://en.wikipedia.org/wiki/Irreducible_fraction). If your final result is an integer, change it to the format of a fractio... | null |
[ Python ] ✅✅ Simple Python Solution | Basic Approach🥳✌👍 | fraction-addition-and-subtraction | 0 | 1 | # If You like the Solution, Don\'t Forget To UpVote Me, Please UpVote! \uD83D\uDD3C\uD83D\uDE4F\n# Runtime: 1836 ms, faster than 5.56% of Python3 online submissions for Fraction Addition and Subtraction.\n# Memory Usage: 16.4 MB, less than 39.58% of Python3 online submissions for Fraction Addition and Subtraction.\n\n\... | 0 | Given a string `expression` representing an expression of fraction addition and subtraction, return the calculation result in string format.
The final result should be an [irreducible fraction](https://en.wikipedia.org/wiki/Irreducible_fraction). If your final result is an integer, change it to the format of a fractio... | null |
Simple | Easy to understand | Python solution | NO Regular Expression 💯 ✅ | fraction-addition-and-subtraction | 0 | 1 | # Intuition\nlet\'s think about a brute force idea first. we need to calcualte a minimal common multiple of all the denominators. Then we can add all the numerators and divide the result by the common denominator. reduce them by overall gcd. this is a very naive idea. we can do better. But let me implement it first.\n\... | 0 | Given a string `expression` representing an expression of fraction addition and subtraction, return the calculation result in string format.
The final result should be an [irreducible fraction](https://en.wikipedia.org/wiki/Irreducible_fraction). If your final result is an integer, change it to the format of a fractio... | null |
Python 3 solution || 100% working!!! | valid-square | 0 | 1 | # Intuition and Approach\nI tried to find all the possible distances between points(there are 6 combinations). I used dist function from math module to find the distance between two points. Since the dist function takes only tuples as input , I coverted lists to tuples.\n\nAnd since points are not given in order ...I m... | 2 | Given the coordinates of four points in 2D space `p1`, `p2`, `p3` and `p4`, return `true` _if the four points construct a square_.
The coordinate of a point `pi` is represented as `[xi, yi]`. The input is **not** given in any order.
A **valid square** has four equal sides with positive length and four equal angles (9... | null |
Beats 93% 🚀💥 PYTHON CODE | valid-square | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def validSquare(self, p1: List[int], p2: List[int], p3: List[int], p4: List[int]) -> bool:\n res=0\n def l(a,b):\n len=(((a[0]-b[0])**2)+((a[1]-b[1])**2))\n return len\n d=[l(p1,p2),l(p1,p3),l(p1,p4),l(p2,p3),l(p2,p4),l(p3,p4)]\n d.... | 1 | Given the coordinates of four points in 2D space `p1`, `p2`, `p3` and `p4`, return `true` _if the four points construct a square_.
The coordinate of a point `pi` is represented as `[xi, yi]`. The input is **not** given in any order.
A **valid square** has four equal sides with positive length and four equal angles (9... | null |
python easy to understand 3 lines code beats 95% | valid-square | 0 | 1 | please upvote if u like\n```\nclass Solution:\n def validSquare(self, p1: List[int], p2: List[int], p3: List[int], p4: List[int]) -> bool:\n def dist(point1,point2):\n return (point1[0]-point2[0])**2+(point1[1]-point2[1])**2\n \n D=[\n dist(p1,p2),\n dist(p1,p3),\n ... | 18 | Given the coordinates of four points in 2D space `p1`, `p2`, `p3` and `p4`, return `true` _if the four points construct a square_.
The coordinate of a point `pi` is represented as `[xi, yi]`. The input is **not** given in any order.
A **valid square** has four equal sides with positive length and four equal angles (9... | null |
593: Solution with step by step explanation | valid-square | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nDefine a function called dist that takes two points as input and returns the square of their Euclidean distance.\n\nThe input points are lists of two integers.\nThe function uses the formula (p1[0] - p2[0])**2 + (p1[1] - p2[... | 3 | Given the coordinates of four points in 2D space `p1`, `p2`, `p3` and `p4`, return `true` _if the four points construct a square_.
The coordinate of a point `pi` is represented as `[xi, yi]`. The input is **not** given in any order.
A **valid square** has four equal sides with positive length and four equal angles (9... | null |
Solution | valid-square | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int helper(vector<int> &A,vector<int> &B){\n return (pow(A[0]-B[0],2)+pow(A[1]-B[1],2));\n }\n bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) {\n int a=helper(p1,p2);\n int b=helper(p1,p3);\n int c=helper(p... | 3 | Given the coordinates of four points in 2D space `p1`, `p2`, `p3` and `p4`, return `true` _if the four points construct a square_.
The coordinate of a point `pi` is represented as `[xi, yi]`. The input is **not** given in any order.
A **valid square** has four equal sides with positive length and four equal angles (9... | null |
3 triangles ^^ | valid-square | 0 | 1 | \n\nP.S. Actually we need only three triangles to check.\n**c++:**\n```\nclass Solution \n{\n bool f(vector<int>& p1, vector<int>& p2, vector<int>& p3)\n {\n int x = (p1[0]-p2[0])*(p1[0]-p2[0])+(p1... | 1 | Given the coordinates of four points in 2D space `p1`, `p2`, `p3` and `p4`, return `true` _if the four points construct a square_.
The coordinate of a point `pi` is represented as `[xi, yi]`. The input is **not** given in any order.
A **valid square** has four equal sides with positive length and four equal angles (9... | null |
Beats 100% Runtime using count map | longest-harmonious-subsequence | 0 | 1 | # Approach\n<!-- Describe your approach to solving the problem. -->\nCreate a count map holding occurences on elements and loop on keys to check if next consecutive element is present in the map.\nIf present, compare the result with the max variable\n\n# Complexity\n- Time complexity:\nO(N)\n\n- Space complexity:\nO(N)... | 4 | We define a harmonious array as an array where the difference between its maximum value and its minimum value is **exactly** `1`.
Given an integer array `nums`, return _the length of its longest harmonious subsequence among all its possible subsequences_.
A **subsequence** of array is a sequence that can be derived f... | null |
longest harmonious subsequence - python 3 100% beats in runtime.must watch with tc-O(n) and sc- O(n) | longest-harmonious-subsequence | 0 | 1 | # Intuition\r\n<!-- Describe your first thoughts on how to solve this problem. -->\r\nThe code defines a class Solution with a method findLHS that takes in a list nums representing an array. It calculates the length of the longest harmonious subsequence in the array.\r\n\r\n# Approach\r\n<!-- Describe your approach to ... | 2 | We define a harmonious array as an array where the difference between its maximum value and its minimum value is **exactly** `1`.
Given an integer array `nums`, return _the length of its longest harmonious subsequence among all its possible subsequences_.
A **subsequence** of array is a sequence that can be derived f... | null |
Solution | longest-harmonious-subsequence | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int findLHS(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n if (nums.size()==0) return 0;\n int i=0,r=0;\n while(i<nums.size()) {\n int seen=0;\n int low=i;\n int next=i+1;\n while(i<nums.size() &&... | 2 | We define a harmonious array as an array where the difference between its maximum value and its minimum value is **exactly** `1`.
Given an integer array `nums`, return _the length of its longest harmonious subsequence among all its possible subsequences_.
A **subsequence** of array is a sequence that can be derived f... | null |
594: Time 97.52%, Solution with step by step explanation | longest-harmonious-subsequence | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nCreate a Counter object called freq that counts the frequency of each element in nums.\n\nInitialize a variable called max_length to 0.\n\nIterate through each key in freq.\n\nIf key + 1 is also in freq, update max_length to... | 6 | We define a harmonious array as an array where the difference between its maximum value and its minimum value is **exactly** `1`.
Given an integer array `nums`, return _the length of its longest harmonious subsequence among all its possible subsequences_.
A **subsequence** of array is a sequence that can be derived f... | null |
Python. O(n), Cool, easy & clear solution. | longest-harmonious-subsequence | 0 | 1 | \tclass Solution:\n\t\tdef findLHS(self, nums: List[int]) -> int:\n\t\t\ttmp = Counter(nums)\n\t\t\tkeys = tmp.keys()\n\t\t\tmax = 0\n\t\t\tfor num in keys:\n\t\t\t\tif num - 1 in keys:\n\t\t\t\t\tif tmp[num - 1] + tmp[num] > max:\n\t\t\t\t\t\tmax = tmp[num - 1] + tmp[num]\n\t\t\treturn max | 12 | We define a harmonious array as an array where the difference between its maximum value and its minimum value is **exactly** `1`.
Given an integer array `nums`, return _the length of its longest harmonious subsequence among all its possible subsequences_.
A **subsequence** of array is a sequence that can be derived f... | null |
Python using dictionary | longest-harmonious-subsequence | 0 | 1 | # Intuition\nTo solve this problem, you can iterate through the array and use a hash map to keep track of the frequency of each element in the array. Then, iterate through the hash map and for each element check if there is another element in the hash map whose value is one greater than the current element. If so, add ... | 2 | We define a harmonious array as an array where the difference between its maximum value and its minimum value is **exactly** `1`.
Given an integer array `nums`, return _the length of its longest harmonious subsequence among all its possible subsequences_.
A **subsequence** of array is a sequence that can be derived f... | null |
Python3 Beat 93.80 291ms Explained | longest-harmonious-subsequence | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def findLHS(self, nums: List[int]) -> int:\n # You can use a hash table or dictionary to store the frequency of each number. \n # Then, iterate through the keys of the dictionary and check if there is a key \n # whose value is one greater. If there is, compute ... | 2 | We define a harmonious array as an array where the difference between its maximum value and its minimum value is **exactly** `1`.
Given an integer array `nums`, return _the length of its longest harmonious subsequence among all its possible subsequences_.
A **subsequence** of array is a sequence that can be derived f... | null |
Easy Python 3 Solution without using Counter. | longest-harmonious-subsequence | 0 | 1 | ```\nclass Solution:\n def findLHS(self, nums: List[int]) -> int:\n letter = {}\n ans = 0\n for i in nums:\n if i not in letter:\n letter[i] = 1\n else:\n letter[i]+=1\n for i in letter:\n if i+1 in letter.keys():\n ... | 6 | We define a harmonious array as an array where the difference between its maximum value and its minimum value is **exactly** `1`.
Given an integer array `nums`, return _the length of its longest harmonious subsequence among all its possible subsequences_.
A **subsequence** of array is a sequence that can be derived f... | null |
optimal | longest-harmonious-subsequence | 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: O(nlogn)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.... | 0 | We define a harmonious array as an array where the difference between its maximum value and its minimum value is **exactly** `1`.
Given an integer array `nums`, return _the length of its longest harmonious subsequence among all its possible subsequences_.
A **subsequence** of array is a sequence that can be derived f... | null |
My Solution :) | longest-harmonious-subsequence | 0 | 1 | # Intuition\r\n<!-- Describe your first thoughts on how to solve this problem. -->\r\n\r\n# Approach\r\n<!-- Describe your approach to solving the problem. -->\r\n\r\n# Complexity\r\n- Time complexity:\r\nO(n log n)\r\n\r\n- Space complexity:\r\nO(n)\r\n\r\n# Code\r\n```\r\nclass Solution:\r\n def findLHS(self, nums... | 0 | We define a harmonious array as an array where the difference between its maximum value and its minimum value is **exactly** `1`.
Given an integer array `nums`, return _the length of its longest harmonious subsequence among all its possible subsequences_.
A **subsequence** of array is a sequence that can be derived f... | null |
[ C | C++ | Python | Java | C# | JavaScript | Go ] Same Simple Solution | range-addition-ii | 1 | 1 | C:\n```\nint maxCount(int m, int n, int** ops, int opsSize, int* opsColSize){\n int min_row = m;\n int min_col = n;\n for (int i=0; i<opsSize; i++){\n if (ops[i][0]<min_row) min_row=ops[i][0];\n if (ops[i][1]<min_col) min_col=ops[i][1];\n } \n return min_r... | 30 | You are given an `m x n` matrix `M` initialized with all `0`'s and an array of operations `ops`, where `ops[i] = [ai, bi]` means `M[x][y]` should be incremented by one for all `0 <= x < ai` and `0 <= y < bi`.
Count and return _the number of maximum integers in the matrix after performing all the operations_.
**Exampl... | null |
598: Solution with step by step explanation | range-addition-ii | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nInitialize two variables min_y and min_x to m and n, respectively.\n\nIterate through each sublist in ops and for each sublist:\n\nUpdate min_y to be the minimum of its current value and the first element of the sublist.\nUp... | 3 | You are given an `m x n` matrix `M` initialized with all `0`'s and an array of operations `ops`, where `ops[i] = [ai, bi]` means `M[x][y]` should be incremented by one for all `0 <= x < ai` and `0 <= y < bi`.
Count and return _the number of maximum integers in the matrix after performing all the operations_.
**Exampl... | null |
Solution | range-addition-ii | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int maxCount(int m, int n, vector<vector<int>>& ops) {\n for (auto op : ops){\n m = min(m, op[0]);\n n = min(n, op[1]);\n }\n return m * n;\n }\n};\n```\n\n```Python3 []\nclass Solution:\n def maxCount(self, m: int, n: int, ops: ... | 2 | You are given an `m x n` matrix `M` initialized with all `0`'s and an array of operations `ops`, where `ops[i] = [ai, bi]` means `M[x][y]` should be incremented by one for all `0 <= x < ai` and `0 <= y < bi`.
Count and return _the number of maximum integers in the matrix after performing all the operations_.
**Exampl... | null |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.