post_href stringlengths 57 213 | python_solutions stringlengths 71 22.3k | slug stringlengths 3 77 | post_title stringlengths 1 100 | user stringlengths 3 29 | upvotes int64 -20 1.2k | views int64 0 60.9k | problem_title stringlengths 3 77 | number int64 1 2.48k | acceptance float64 0.14 0.91 | difficulty stringclasses 3
values | __index_level_0__ int64 0 34k |
|---|---|---|---|---|---|---|---|---|---|---|---|
https://leetcode.com/problems/erect-the-fence/discuss/2828749/Python3-Easy-Solution(Convex-hull-or-graham's-scan-Algorithm) | class Solution:
def outerTrees(self, trees: List[List[int]]) -> List[List[int]]:
def clockwise(p1,p2,p3):
x1,y1=p1
x2,y2=p2
x3,y3=p3
return ((y3-y2)*(x2-x1)-(y2-y1)*(x3-x2))
trees.sort()
upper=[]
lower=[]
for t in t... | erect-the-fence | Python3 Easy Solution(Convex hull | graham's scan Algorithm) | Motaharozzaman1996 | 0 | 136 | erect the fence | 587 | 0.523 | Hard | 10,200 |
https://leetcode.com/problems/n-ary-tree-preorder-traversal/discuss/2496970/Very-Easy-oror-100-oror-Fully-Explained-oror-Java-C%2B%2B-Python-Python3-oror-Iterative-and-Recursive(DFS) | class Solution(object):
def preorder(self, root):
# To store the output result...
output = []
self.traverse(root, output)
return output
def traverse(self, root, output):
# Base case: If root is none...
if root is None: return
# Append the value of the root... | n-ary-tree-preorder-traversal | Very Easy || 100% || Fully Explained || Java, C++, Python, Python3 || Iterative & Recursive(DFS) | PratikSen07 | 28 | 1,600 | n ary tree preorder traversal | 589 | 0.762 | Easy | 10,201 |
https://leetcode.com/problems/n-ary-tree-preorder-traversal/discuss/2696811/Python-2-Easy-Way-To-Solve-or-96-Faster-or-Recursive-and-Iterative-Approach | class Solution:
def preorder(self, root: 'Node') -> List[int]:
def dfs(root, output):
if not root:
return None
output.append(root.val)
for child in root.children:
dfs(child, output)
return output
return... | n-ary-tree-preorder-traversal | ✔️ Python 2 Easy Way To Solve | 96% Faster | Recursive and Iterative Approach | pniraj657 | 12 | 839 | n ary tree preorder traversal | 589 | 0.762 | Easy | 10,202 |
https://leetcode.com/problems/n-ary-tree-preorder-traversal/discuss/2696811/Python-2-Easy-Way-To-Solve-or-96-Faster-or-Recursive-and-Iterative-Approach | class Solution:
def preorder(self, root: 'Node') -> List[int]:
if not root:
return []
stack = [root]
output = []
while stack:
top = stack.pop()
output.append(top.val)
stack.extend(reversed(top.children))
return... | n-ary-tree-preorder-traversal | ✔️ Python 2 Easy Way To Solve | 96% Faster | Recursive and Iterative Approach | pniraj657 | 12 | 839 | n ary tree preorder traversal | 589 | 0.762 | Easy | 10,203 |
https://leetcode.com/problems/n-ary-tree-preorder-traversal/discuss/1329639/Elegant-Python-Iterative-DFS | class Solution:
def preorder(self, root: 'Node') -> List[int]:
# Handle edge case.
if not root: return
pre_order = []
queue = [root]
while queue:
current_node = queue.pop()
pre_order.append(current_node.val)
for i... | n-ary-tree-preorder-traversal | Elegant Python Iterative DFS | soma28 | 3 | 181 | n ary tree preorder traversal | 589 | 0.762 | Easy | 10,204 |
https://leetcode.com/problems/n-ary-tree-preorder-traversal/discuss/2232012/Python3-simple-recursive-solution | class Solution:
def preorder(self, root: 'Node') -> List[int]:
result = []
def traverse(root):
if not root:
return
result.append(root.val)
for node in root.children:
traverse(node)
traverse(root)
re... | n-ary-tree-preorder-traversal | 📌 Python3 simple recursive solution | Dark_wolf_jss | 2 | 81 | n ary tree preorder traversal | 589 | 0.762 | Easy | 10,205 |
https://leetcode.com/problems/n-ary-tree-preorder-traversal/discuss/2183036/Python-Simple-Python-Solution-Using-Recursion | class Solution:
def preorder(self, root: 'Node') -> List[int]:
result = []
def PreOrderTraversal(node):
if node == None:
return None
result.append(node.val)
for next_node in node.children:
PreOrderTraversal(next_node)
PreOrderTraversal(root)
return result | n-ary-tree-preorder-traversal | [ Python ] ✅✅ Simple Python Solution Using Recursion 🥳✌👍 | ASHOK_KUMAR_MEGHVANSHI | 2 | 164 | n ary tree preorder traversal | 589 | 0.762 | Easy | 10,206 |
https://leetcode.com/problems/n-ary-tree-preorder-traversal/discuss/1167950/Python-Iterative-Recursive-and-One-Liners | class Solution:
def preorder(self, root: 'Node') -> List[int]:
order, queue = [], root and [root]
while queue:
order.append((node := queue.pop()).val)
queue.extend(filter(bool, reversed(node.children)))
return order | n-ary-tree-preorder-traversal | [Python] Iterative, Recursive, and One-Liners | kingjonathan310 | 2 | 144 | n ary tree preorder traversal | 589 | 0.762 | Easy | 10,207 |
https://leetcode.com/problems/n-ary-tree-preorder-traversal/discuss/1167950/Python-Iterative-Recursive-and-One-Liners | class Solution:
def __init__(self):
self._preorder = []
def dfs(self, node: 'Node') -> None:
if node: self._preorder.append(node.val), *map(self.dfs, node.children)
def preorder(self, root: 'Node') -> List[int]:
return self.dfs(root) or self._preorder | n-ary-tree-preorder-traversal | [Python] Iterative, Recursive, and One-Liners | kingjonathan310 | 2 | 144 | n ary tree preorder traversal | 589 | 0.762 | Easy | 10,208 |
https://leetcode.com/problems/n-ary-tree-preorder-traversal/discuss/1167950/Python-Iterative-Recursive-and-One-Liners | class Solution:
def preorder(self, root: 'Node') -> List[int]:
return [root.val, *chain(*(self.preorder(c) for c in root.children if c))] if root else [] | n-ary-tree-preorder-traversal | [Python] Iterative, Recursive, and One-Liners | kingjonathan310 | 2 | 144 | n ary tree preorder traversal | 589 | 0.762 | Easy | 10,209 |
https://leetcode.com/problems/n-ary-tree-preorder-traversal/discuss/1167950/Python-Iterative-Recursive-and-One-Liners | class Solution:
def preorder(self, root: 'Node') -> List[int]:
return [root.val, *chain(*map(self.preorder, filter(bool, root.children)))] if root else [] | n-ary-tree-preorder-traversal | [Python] Iterative, Recursive, and One-Liners | kingjonathan310 | 2 | 144 | n ary tree preorder traversal | 589 | 0.762 | Easy | 10,210 |
https://leetcode.com/problems/n-ary-tree-preorder-traversal/discuss/355757/Python3-iterative-soln | class Solution:
def preorder(self, root: 'Node') -> List[int]:
ans = []
stack = [root]
while stack:
node = stack.pop()
if node:
ans.append(node.val)
stack.extend(node.children[::-1])
return ans | n-ary-tree-preorder-traversal | [Python3] iterative soln | ye15 | 2 | 130 | n ary tree preorder traversal | 589 | 0.762 | Easy | 10,211 |
https://leetcode.com/problems/n-ary-tree-preorder-traversal/discuss/355757/Python3-iterative-soln | class Solution:
def preorder(self, root: 'Node') -> List[int]:
def dfs(node):
"""Populate ans via dfs."""
if not node: return
ans.append(node.val)
for child in node.children: dfs(child)
ans = []
dfs(root)
retu... | n-ary-tree-preorder-traversal | [Python3] iterative soln | ye15 | 2 | 130 | n ary tree preorder traversal | 589 | 0.762 | Easy | 10,212 |
https://leetcode.com/problems/n-ary-tree-preorder-traversal/discuss/2361074/Python-shortcleaneasy-solution-with-recursion | class Solution:
def preorder(self, root: 'Node') -> List[int]:
if root:
res = [root.val]
if root.children:
for c in root.children:
res.extend(self.preorder(c))
return res | n-ary-tree-preorder-traversal | Python short/clean/easy solution with recursion | Mazarin | 1 | 131 | n ary tree preorder traversal | 589 | 0.762 | Easy | 10,213 |
https://leetcode.com/problems/n-ary-tree-preorder-traversal/discuss/2297365/Python3-simple-DFS-recursive-solution | class Solution:
def preorder(self, root: 'Node') -> List[int]:
result = []
def traverse(root):
if not root:
return
result.append(root.val)
for node in root.children:
traverse(node)
traverse(root)
re... | n-ary-tree-preorder-traversal | 📌 Python3 simple DFS recursive solution | Dark_wolf_jss | 1 | 59 | n ary tree preorder traversal | 589 | 0.762 | Easy | 10,214 |
https://leetcode.com/problems/n-ary-tree-preorder-traversal/discuss/1403181/Two-simple-solutions-or-With-and-without-stack-or-With-and-without-recursion | class Solution:
def preorder(self, root: 'Node') -> List[int]:
if not root:
return []
ans = []
stack = [root]
while stack:
temp = stack.pop()
ans.append(temp.val)
stack.extend(temp.children[::-1]) #as stack works in ... | n-ary-tree-preorder-traversal | Two simple solutions | With and without stack | With and without recursion | shraddhapp | 1 | 174 | n ary tree preorder traversal | 589 | 0.762 | Easy | 10,215 |
https://leetcode.com/problems/n-ary-tree-preorder-traversal/discuss/1403181/Two-simple-solutions-or-With-and-without-stack-or-With-and-without-recursion | class Solution:
def preorder(self, root: 'Node') -> List[int]:
ans = []
if not root:
return ans
def preorder(node):
ans.append(node.val)
for i in node.children: #recurssion on each child
preorder(i)
... | n-ary-tree-preorder-traversal | Two simple solutions | With and without stack | With and without recursion | shraddhapp | 1 | 174 | n ary tree preorder traversal | 589 | 0.762 | Easy | 10,216 |
https://leetcode.com/problems/n-ary-tree-preorder-traversal/discuss/1382083/Easy-Recursive-Python-Solution | class Solution:
def __init__(self):
self.ord = []
self.queue = []
def traversal(self, root):
if root:
self.ord.append(root.val)
self.queue = root.children + self.queue
while self.queue:
a = self.queue.pop(0)
self.trave... | n-ary-tree-preorder-traversal | Easy, Recursive Python Solution | the_sky_high | 1 | 241 | n ary tree preorder traversal | 589 | 0.762 | Easy | 10,217 |
https://leetcode.com/problems/n-ary-tree-preorder-traversal/discuss/2843447/Easy-Python3-iterative-approach-DFS-Stack-92-speed-81-memory | class Solution:
def preorder(self, root: 'Node') -> List[int]:
if not root: return []
stack, visited = [root], []
while stack:
node = stack.pop()
visited.append(node.val)
for child in reversed(node.children):
stack.append(child)
ret... | n-ary-tree-preorder-traversal | Easy Python3 iterative approach -- DFS -- Stack -- 92% speed, 81% memory | JacobSulewski | 0 | 1 | n ary tree preorder traversal | 589 | 0.762 | Easy | 10,218 |
https://leetcode.com/problems/n-ary-tree-preorder-traversal/discuss/2804227/Can-anyone-tell-me-Why-is-this-solution-not-working | class Solution:
def __init__(self):
self.ls = []
def preorder(self, root: 'Node') -> List[int]:
self.ls = []
self.preorder(root)
return self.ls
def preorder(self,root):
if root is None: return
self.ls.append(root.val)
ch = ro... | n-ary-tree-preorder-traversal | Can anyone tell me, Why is this solution not working | SJ1602 | 0 | 4 | n ary tree preorder traversal | 589 | 0.762 | Easy | 10,219 |
https://leetcode.com/problems/n-ary-tree-preorder-traversal/discuss/2800282/N-ary-Tree-Preorder-Traversal-or-Python-Solution-using-recursion | class Solution:
def preorder(self, root: 'Node') -> List[int]:
def dfs(root, result):
if not root:
return None
result.append(root.val)
for child in root.children:
dfs(child, result)
return result
return dfs(root... | n-ary-tree-preorder-traversal | N-ary Tree Preorder Traversal | Python Solution using recursion | nishanrahman1994 | 0 | 3 | n ary tree preorder traversal | 589 | 0.762 | Easy | 10,220 |
https://leetcode.com/problems/n-ary-tree-preorder-traversal/discuss/2727245/Python3-Iterative-with-Stack | class Solution:
def preorder(self, root: 'Node') -> List[int]:
output = []
if not root: return output
stack = [root]
while stack:
node = stack.pop()
output.append(node.val)
for c in reversed(node.children):
stack.append(c)
... | n-ary-tree-preorder-traversal | Python3 Iterative with Stack | Mbarberry | 0 | 3 | n ary tree preorder traversal | 589 | 0.762 | Easy | 10,221 |
https://leetcode.com/problems/n-ary-tree-preorder-traversal/discuss/2688647/python3-%2B-iterative-%2B-faster-then-85.59-(59ms)-and-memory-less-then-97.71 | class Solution:
def preorder(self, root: 'Node') -> List[int]:
if not root:
return
stack = [root]
op = []
while len(stack)>0:
temp = stack.pop()
op.append(temp.val)
stack.extend(temp.children[::-1])
return op | n-ary-tree-preorder-traversal | python3 + iterative + faster then 85.59% (59ms) and memory less then 97.71% | sandeepagrawal8875 | 0 | 4 | n ary tree preorder traversal | 589 | 0.762 | Easy | 10,222 |
https://leetcode.com/problems/n-ary-tree-preorder-traversal/discuss/2503004/python3-recursive-or-with-explanation-or-faster-than-97.93-or-easy-to-understand | class Solution:
'''
N-ary tree is a generic tree with multiple nodes (N-ary)
possible at each sub-tree.
Preorder traversal will be top to bottom, left to right.
Hence, we start from root node, adding it to the output list,
and do recursion of traversing each child node until we get to
th... | n-ary-tree-preorder-traversal | python3 recursive | with explanation | faster than 97.93% | easy to understand | shwetachandole | 0 | 38 | n ary tree preorder traversal | 589 | 0.762 | Easy | 10,223 |
https://leetcode.com/problems/n-ary-tree-preorder-traversal/discuss/2420629/JAVA-C%2B%2B-PYTHON3-Easy-and-Fast-Solution | class Solution:
def preorder(self, root: 'Node', ans: list = None) -> List[int]:
if not root: return ans
if ans == None: ans = []
ans.append(root.val)
for child in root.children:
self.preorder(child, ans)
return ans | n-ary-tree-preorder-traversal | [JAVA, C++, PYTHON3] Easy and Fast Solution | WhiteBeardPirate | 0 | 53 | n ary tree preorder traversal | 589 | 0.762 | Easy | 10,224 |
https://leetcode.com/problems/n-ary-tree-preorder-traversal/discuss/2411211/Python-Recursive-99-faster | class Solution:
def preorder(self, root: 'Node') -> List[int]:
output = []
def dfs(node):
if not node:
return None
output.append(node.val)
for nd in node.children:
dfs(nd)
dfs(root)
return outp... | n-ary-tree-preorder-traversal | Python Recursive 99% faster | Abhi_009 | 0 | 96 | n ary tree preorder traversal | 589 | 0.762 | Easy | 10,225 |
https://leetcode.com/problems/n-ary-tree-preorder-traversal/discuss/2351352/Python3-recursive-%2B-iterative%3A-58ms-16MB-solution | class Solution:
def __init__(self):
self.traversal = []
def preorder(self, root: 'Node') -> List[int]:
if not root:
return self.traversal
self.traversal.append(root.val)
if root.children != []:
for child in root.children:
self.preorde... | n-ary-tree-preorder-traversal | Python3 recursive + iterative: 58ms, 16MB solution | yashkadulkar | 0 | 61 | n ary tree preorder traversal | 589 | 0.762 | Easy | 10,226 |
https://leetcode.com/problems/n-ary-tree-preorder-traversal/discuss/2253615/Python3-Solution-with-using-recursive-dfs | class Solution:
def traversal(self, node, res):
if not node:
return
res.append(node.val)
for child in node.children:
self.traversal(child, res)
def preorder(self, root: 'Node') -> List[int]:
res = []
self.traversal(root, ... | n-ary-tree-preorder-traversal | [Python3] Solution with using recursive dfs | maosipov11 | 0 | 34 | n ary tree preorder traversal | 589 | 0.762 | Easy | 10,227 |
https://leetcode.com/problems/n-ary-tree-preorder-traversal/discuss/2130791/Deque-related-problem | class Solution:
def preorder(self, root: 'Node') -> List[int]:
if not root:return []
a = []
q = deque([root]) #in case of deque empty(root=[]) why it is entering in while loop after removing first if statement
while q:
p = q.popleft()
a.append(p.val)
... | n-ary-tree-preorder-traversal | Deque related problem | jayeshvarma | 0 | 50 | n ary tree preorder traversal | 589 | 0.762 | Easy | 10,228 |
https://leetcode.com/problems/n-ary-tree-preorder-traversal/discuss/2091609/Python-oror-Iterative | class Solution:
def preorder(self, root: 'Node') -> List[int]:
if not root: return None
preorder = []
stack = [deque([root])]
while stack:
level = stack.pop()
node = level.popleft()
preorder.append(node.val)
Q = deque([])
for child in node.children:
Q.append(child)
for remain in level:
... | n-ary-tree-preorder-traversal | Python || Iterative | morpheusdurden | 0 | 190 | n ary tree preorder traversal | 589 | 0.762 | Easy | 10,229 |
https://leetcode.com/problems/n-ary-tree-preorder-traversal/discuss/2084259/Python3-DFS-Recursive-Solution | class Solution:
def preorder(self, root: 'Node') -> List[int]:
if not root:
return []
res=[]
def dfs(root):
if not root:
return
res.append(root.val)
for i in root.children:
dfs(i)
dfs(root)
return... | n-ary-tree-preorder-traversal | Python3 DFS Recursive Solution | rohith4pr | 0 | 106 | n ary tree preorder traversal | 589 | 0.762 | Easy | 10,230 |
https://leetcode.com/problems/n-ary-tree-preorder-traversal/discuss/1992076/Simple-Python-Solution | class Solution:
def __init__(self): # initializing the answer list
self.ans=[]
def preorder(self, root: 'Node') -> List[int]:
if not root: # if leaf Node
return
self.ans.append(root.val) # append the root value
for i in root.children: # iterate... | n-ary-tree-preorder-traversal | Simple Python Solution | HimanshuGupta_p1 | 0 | 52 | n ary tree preorder traversal | 589 | 0.762 | Easy | 10,231 |
https://leetcode.com/problems/n-ary-tree-postorder-traversal/discuss/355782/Python3-recursive-and-iterative-implementation | class Solution:
def postorder(self, root: 'Node') -> List[int]:
def dfs(node):
"""Populate ans via post-order traversal."""
if not node: return
for child in node.children: dfs(child)
ans.append(node.val)
ans = []
dfs(root... | n-ary-tree-postorder-traversal | [Python3] recursive & iterative implementation | ye15 | 2 | 129 | n ary tree postorder traversal | 590 | 0.772 | Easy | 10,232 |
https://leetcode.com/problems/n-ary-tree-postorder-traversal/discuss/355782/Python3-recursive-and-iterative-implementation | class Solution:
def postorder(self, root: 'Node') -> List[int]:
ans = []
stack = [root]
while stack:
node = stack.pop()
if node:
ans.append(node.val)
stack.extend(node.children)
return ans[::-1] | n-ary-tree-postorder-traversal | [Python3] recursive & iterative implementation | ye15 | 2 | 129 | n ary tree postorder traversal | 590 | 0.772 | Easy | 10,233 |
https://leetcode.com/problems/n-ary-tree-postorder-traversal/discuss/1440436/Simple-Recursive-solution | class Solution(object):
def postorder(self, root):
"""
:type root: Node
:rtype: List[int]
"""
res=[]
def bfs(root):
if root:
for child in root.children:
bfs(child)
res.append(root.val)
bfs(root)
return res | n-ary-tree-postorder-traversal | Simple Recursive solution | Qyum | 1 | 148 | n ary tree postorder traversal | 590 | 0.772 | Easy | 10,234 |
https://leetcode.com/problems/n-ary-tree-postorder-traversal/discuss/1394904/Python3-Standard-PostOrder-Traversal-greater94.-6-lines-of-code. | class Solution:
def postorder(self, root: 'Node') -> List[int]:
def post(node, ans):
if node:
for ea in node.children:
post(ea, ans)
ans.append(node.val)
return ans
return post(root, []) | n-ary-tree-postorder-traversal | [Python3] Standard PostOrder Traversal >94%. 6 lines of code. | whitehatbuds | 1 | 208 | n ary tree postorder traversal | 590 | 0.772 | Easy | 10,235 |
https://leetcode.com/problems/n-ary-tree-postorder-traversal/discuss/1384155/Python-simple-recursion-based-solution-94.75-faster | class Solution:
# In postorder generally recursion is done at the first step
# and value gets printed at last. The similar logic is
# applied and entries are appended at the last
def postorder(self, root: 'Node') -> List[int]:
traversal = []
def naryPostorder(node):
if node:
... | n-ary-tree-postorder-traversal | Python-simple recursion based solution-94.75% faster | Hariharan-SV | 1 | 109 | n ary tree postorder traversal | 590 | 0.772 | Easy | 10,236 |
https://leetcode.com/problems/n-ary-tree-postorder-traversal/discuss/1382103/Easy-Python-Recursive-Solution-(Faster-than-100) | class Solution:
def __init__(self):
self.ord = []
def traversal(self, root):
for child in root.children:
self.traversal(child)
self.ord.append(child.val)
def postorder(self, root: 'Node') -> List[int]:
if root:
self.traversal(root)
se... | n-ary-tree-postorder-traversal | Easy Python Recursive Solution (Faster than 100%) | the_sky_high | 1 | 109 | n ary tree postorder traversal | 590 | 0.772 | Easy | 10,237 |
https://leetcode.com/problems/n-ary-tree-postorder-traversal/discuss/2482063/Python-recursive-oror-beats-94 | class Solution:
def __init__(self):
self.postOrder = []
def postorder(self, root: 'Node') -> List[int]:
if not root:
return []
if root:
for c in root.children:
self.postorder(c)
self.postOrder.append(root.val)
return self.postOr... | n-ary-tree-postorder-traversal | Python recursive || beats 94% | aruj900 | 0 | 18 | n ary tree postorder traversal | 590 | 0.772 | Easy | 10,238 |
https://leetcode.com/problems/n-ary-tree-postorder-traversal/discuss/2124824/Easy-to-understand-solution(recursive-approach) | class Solution:
def postorder(self, root: 'Node') -> List[int]:
def solve(root, ans):
if root: # only continue when root is not None
for child in root.children: # And recurse for its children
solve(child, ans)
an... | n-ary-tree-postorder-traversal | Easy to understand solution(recursive approach) | writemeom | 0 | 63 | n ary tree postorder traversal | 590 | 0.772 | Easy | 10,239 |
https://leetcode.com/problems/n-ary-tree-postorder-traversal/discuss/2091664/Python-oror-Simple-Iterative-Solution | class Solution:
def postorder(self, root: 'Node') -> List[int]:
if not root: return None
post = []
stack = [root]
while stack:
node = stack.pop()
post.append(node.val)
stack.extend(node.children)
return post[::-1] | n-ary-tree-postorder-traversal | Python || Simple Iterative Solution | morpheusdurden | 0 | 85 | n ary tree postorder traversal | 590 | 0.772 | Easy | 10,240 |
https://leetcode.com/problems/n-ary-tree-postorder-traversal/discuss/2084269/Python3-DFS-Recursive-Solution | class Solution:
def postorder(self, root: 'Node') -> List[int]:
if not root:
return []
res=[]
def dfs(root):
if not root:
return
for i in root.children:
dfs(i)
res.append(root.val)
dfs(root)
retur... | n-ary-tree-postorder-traversal | Python3 DFS Recursive Solution | rohith4pr | 0 | 44 | n ary tree postorder traversal | 590 | 0.772 | Easy | 10,241 |
https://leetcode.com/problems/n-ary-tree-postorder-traversal/discuss/2022357/92-fasteroror-recursion | class Solution:
def postorder(self, root: 'Node') -> List[int]:
l1=[]
def test(root):
nonlocal l1
if not root:
return 0
if root:
for i in root.children:
test(i)
l1.append(i.val)
... | n-ary-tree-postorder-traversal | 92% faster|| recursion | arunava2210 | 0 | 22 | n ary tree postorder traversal | 590 | 0.772 | Easy | 10,242 |
https://leetcode.com/problems/n-ary-tree-postorder-traversal/discuss/1980063/Python-Easy-and-Fast-Solution | class Solution:
def postorder(self, root: 'Node') -> List[int]:
ans = list()
if root :
for node in root.children :
ans += self.postorder(node)
ans.append(root.val)
return ans | n-ary-tree-postorder-traversal | [ Python ] Easy and Fast Solution | crazypuppy | 0 | 40 | n ary tree postorder traversal | 590 | 0.772 | Easy | 10,243 |
https://leetcode.com/problems/n-ary-tree-postorder-traversal/discuss/1743714/Python3-One-liner | class Solution:
def postorder(self, root: 'Node') -> List[int]:
return [element for child in root.children for element in self.postorder(child)]+[root.val] if root else [] | n-ary-tree-postorder-traversal | [Python3] One-liner | Rainyforest | 0 | 69 | n ary tree postorder traversal | 590 | 0.772 | Easy | 10,244 |
https://leetcode.com/problems/n-ary-tree-postorder-traversal/discuss/1655307/Easy-python-solution | class Solution:
def postorder(self, root: 'Node') -> List[int]:
ans = []
if not root:
return None
def post(node, ans):
for child in node.children:
pre(child, ans)
ans.append(node.val)
pre(root,ans)
return ans | n-ary-tree-postorder-traversal | Easy python solution | Jazzyb1999 | 0 | 145 | n ary tree postorder traversal | 590 | 0.772 | Easy | 10,245 |
https://leetcode.com/problems/n-ary-tree-postorder-traversal/discuss/1280931/python3-recursive-approach | class Solution:
def postorder(self, root: 'Node') -> List[int]:
if root is None: return []
vals = []
def n_ary(node):
nonlocal vals
if node is None: return None
for child in node.children:
n_ary(child)... | n-ary-tree-postorder-traversal | python3 recursive approach | certifiedpandaboy | 0 | 83 | n ary tree postorder traversal | 590 | 0.772 | Easy | 10,246 |
https://leetcode.com/problems/n-ary-tree-postorder-traversal/discuss/1034739/Python3-solution-with-three-approaches | class Solution:
def __init__(self):
self.l = []
def postorder(self, root: 'Node') -> List[int]:
if root is None:
return None
for i in root.children:
self.postorder(i)
self.l.append(root.val)
return self.l | n-ary-tree-postorder-traversal | Python3 solution with three approaches | EklavyaJoshi | 0 | 80 | n ary tree postorder traversal | 590 | 0.772 | Easy | 10,247 |
https://leetcode.com/problems/n-ary-tree-postorder-traversal/discuss/1034739/Python3-solution-with-three-approaches | class Solution:
def postorder(self, root: 'Node') -> List[int]:
st = [root]
l = []
while st:
node = st.pop()
if node:
l.append(node.val)
if node.children:
for i in node.children:
st.append(i)
... | n-ary-tree-postorder-traversal | Python3 solution with three approaches | EklavyaJoshi | 0 | 80 | n ary tree postorder traversal | 590 | 0.772 | Easy | 10,248 |
https://leetcode.com/problems/n-ary-tree-postorder-traversal/discuss/1034739/Python3-solution-with-three-approaches | class Solution:
def postorder(self, root: 'Node') -> List[int]:
st = [root]
l = []
while st:
node = st.pop()
if node:
l.insert(0,node.val)
if node.children:
for i in node.children:
st.append(i... | n-ary-tree-postorder-traversal | Python3 solution with three approaches | EklavyaJoshi | 0 | 80 | n ary tree postorder traversal | 590 | 0.772 | Easy | 10,249 |
https://leetcode.com/problems/tag-validator/discuss/449403/Two-Python-Solution%3A(1)-linear-paring-with-stack(2)BNF-parsing-with-pyparsing | class Solution:
def isValid(self, code: str) -> bool:
if code[0] != '<' or code[-1] != '>': return False
i, n = 0, len(code)
stk = []
while i < n:
if code[i] == '<':
if i != 0 and code[i: i + 9] == '<![CDATA[':
if not stk: return False
... | tag-validator | Two Python Solution:(1) linear paring with stack;(2)BNF parsing with pyparsing | cava | 1 | 402 | tag validator | 591 | 0.371 | Hard | 10,250 |
https://leetcode.com/problems/tag-validator/discuss/2546456/Python3-parsing | class Solution:
def isValid(self, code: str) -> bool:
prefix = suffix = False
stack = []
i = 0
while i < len(code):
if code[i:i+2] == '</':
ii = i = i+2
for i in range(i, len(code)):
if code[i] == '>': break
... | tag-validator | [Python3] parsing | ye15 | 0 | 36 | tag validator | 591 | 0.371 | Hard | 10,251 |
https://leetcode.com/problems/fraction-addition-and-subtraction/discuss/1128722/Python-Simple-Parsing-with-example | class Solution:
def fractionAddition(self, exp: str) -> str:
if not exp:
return "0/1"
if exp[0] != '-':
exp = '+' + exp
# Parse the expression to get the numerator and denominator of each fraction
num = []
den = []
po... | fraction-addition-and-subtraction | [Python] Simple Parsing - with example | rowe1227 | 6 | 918 | fraction addition and subtraction | 592 | 0.521 | Medium | 10,252 |
https://leetcode.com/problems/fraction-addition-and-subtraction/discuss/1318891/Python-3-or-Math-Simulation-GCD-or-Explanation | class Solution:
def fractionAddition(self, expression: str) -> str:
pairs, cur, sign, multiple = [], '', 0, 1
for c in expression+'+':
if not sign:
if c == '-': sign = -1
else: sign, cur = 1, cur + c
elif c in '-+':
left, right ... | fraction-addition-and-subtraction | Python 3 | Math, Simulation, GCD | Explanation | idontknoooo | 3 | 438 | fraction addition and subtraction | 592 | 0.521 | Medium | 10,253 |
https://leetcode.com/problems/fraction-addition-and-subtraction/discuss/2430114/Python-two-solutions.-Short-and-Concise.-Library-and-No-Library | class Solution:
def fractionAddition(self, expression: str) -> str:
# get all the fractions separately: e.g: "-1/2+1/2" -> ['-1/2', '1/2']
fractions = re.findall(r'-*\d+/\d+', expression)
# separate the numerators and denominators-> ['-1/2', '1/2'] -> [-1, 1] , [2, 2]
numera... | fraction-addition-and-subtraction | Python two solutions👀. Short and Concise✅. Library and No Library🔥 | blest | 1 | 33 | fraction addition and subtraction | 592 | 0.521 | Medium | 10,254 |
https://leetcode.com/problems/fraction-addition-and-subtraction/discuss/388372/Solution-in-Python-3-(beats-100.00-)-(three-lines) | class Solution:
def fractionAddition(self, f: str) -> str:
f, d = [int(i) for i in (f.replace('/',' ').replace('+',' +').replace('-',' -')).split()], 1
for i in range(1,len(f),2): d *= f[i]
return (lambda x,y: str(x//math.gcd(x,y))+"/"+str(y//math.gcd(x,y)))(sum(d*f[i]//f[i+1] for i in range(0,len(f)... | fraction-addition-and-subtraction | Solution in Python 3 (beats 100.00 %) (three lines) | junaidmansuri | 1 | 556 | fraction addition and subtraction | 592 | 0.521 | Medium | 10,255 |
https://leetcode.com/problems/fraction-addition-and-subtraction/discuss/2661195/Python-or-Rational-class-without-regular-expressions | class Solution:
def fractionAddition(self, expression: str) -> str:
def parse_rational(s):
sign = 1
if s[0] == '-':
s = s[1:]
sign = -1
(n, d) = s.split('/')
return Rational(sign*int(n), int(d))
ans = Rat... | fraction-addition-and-subtraction | Python | Rational class without regular expressions | on_danse_encore_on_rit_encore | 0 | 6 | fraction addition and subtraction | 592 | 0.521 | Medium | 10,256 |
https://leetcode.com/problems/fraction-addition-and-subtraction/discuss/1813436/Python-3-or-GCD-refined-parsingor-No-need-of-sign | class Solution:
def fractionAddition(self, expression: str) -> str:
curr = ""
pair = []
multiple = 1
def gcd(a,b):
while b:
a,b = b, a%b
return abs(a)
for c in expression+'+':
if c in "-+" and curr:
... | fraction-addition-and-subtraction | Python 3 | GCD, refined parsing| No need of sign | rorawat | 0 | 175 | fraction addition and subtraction | 592 | 0.521 | Medium | 10,257 |
https://leetcode.com/problems/fraction-addition-and-subtraction/discuss/1488911/Python-faster-speed-with-deque | class Solution:
def fractionAddition(self, expression: str) -> str:
import math
from collections import deque
if expression[0] != '+' and expression[0] != '-':
expression = '+' + expression
n = len(expression)
indexes = []
exp = deque... | fraction-addition-and-subtraction | Python faster speed with deque | byuns9334 | 0 | 189 | fraction addition and subtraction | 592 | 0.521 | Medium | 10,258 |
https://leetcode.com/problems/fraction-addition-and-subtraction/discuss/885465/Python3-regex | class Solution:
def fractionAddition(self, expression: str) -> str:
n, d = 0, 1
nums = (int(x) for x in re.findall("[+-]?\d+", expression))
for nn, dd in zip(*[iter(nums)]*2):
n = n*dd + nn*d
d *= dd
g = gcd(n, d)
return f"{n//g}/{d//g}" | fraction-addition-and-subtraction | [Python3] regex | ye15 | 0 | 88 | fraction addition and subtraction | 592 | 0.521 | Medium | 10,259 |
https://leetcode.com/problems/valid-square/discuss/931866/python-easy-to-understand-3-lines-code-beats-95 | class Solution:
def validSquare(self, p1: List[int], p2: List[int], p3: List[int], p4: List[int]) -> bool:
def dist(point1,point2):
return (point1[0]-point2[0])**2+(point1[1]-point2[1])**2
D=[
dist(p1,p2),
dist(p1,p3),
dist(p1,p4),
dist(p2,p3)... | valid-square | python easy to understand 3 lines code beats 95% | Mahesh_N_V | 17 | 966 | valid square | 593 | 0.44 | Medium | 10,260 |
https://leetcode.com/problems/valid-square/discuss/2076979/3-triangles | class Solution:
def validSquare(self, p1, p2, p3, p4) :
def f(p1, p2, p3) :
l = sorted([(p1[0]-p2[0])**2+(p1[1]-p2[1])**2, (p1[0]-p3[0])**2+(p1[1]-p3[1])**2, (p3[0]-p2[0])**2+(p3[1]-p2[1])**2])
return l[0]+l[1]==l[2] and l[0]==l[1]!=0
return f(p1,p2,p3) and f(p2,p3,p4) and f(... | valid-square | 3 triangles ^^ | andrii_khlevniuk | 1 | 89 | valid square | 593 | 0.44 | Medium | 10,261 |
https://leetcode.com/problems/valid-square/discuss/1594278/Python-Straightforward-Solution-or-Easy-to-Understand | class Solution:
def dist(self, tup1, tup2):
x1, y1 = tup1
x2, y2 = tup2
return (((x2-x1)**2)+((y2-y1)**2))**0.5
# x**0.5 = math.sqrt(x)
def validSquare(self, p1: List[int], p2: List[int], p3: List[int], p4: List[int]) -> bool:
edges = [(p1, p2), (p1, p3), (p1, p4), (p2, p3), (p2, p4), (p3, p4)]
edges_occ... | valid-square | Python Straightforward Solution | Easy to Understand | leet_satyam | 1 | 153 | valid square | 593 | 0.44 | Medium | 10,262 |
https://leetcode.com/problems/valid-square/discuss/1410108/Python-vector-math-solution-(distance-dot-product) | class Solution:
def validSquare(self, p1: List[int], p2: List[int], p3: List[int], p4: List[int]) -> bool:
points = list(sorted([p1, p2, p3, p4]))
def line(a, b):
return points[b][0] - points[a][0], points[b][1] - points[a][1]
length = lambda line: math.sqrt(line[0]**2 + line[1]*... | valid-square | Python vector math solution (distance, dot-product) | yiseboge | 1 | 78 | valid square | 593 | 0.44 | Medium | 10,263 |
https://leetcode.com/problems/valid-square/discuss/932172/validSquare-or-python3-short-solution | class Solution:
def validSquare(self, p1: List[int], p2: List[int], p3: List[int], p4: List[int]) -> bool:
points, dic = [p1, p2, p3, p4], {}
for i in range(len(points) - 1):
for j in range(i + 1, len(points)):
dic[dis] = dic.get(dis:=(points[i][0] - points[j][0]) ** 2 + ... | valid-square | validSquare | python3 short solution | hangyu1130 | 1 | 77 | valid square | 593 | 0.44 | Medium | 10,264 |
https://leetcode.com/problems/valid-square/discuss/702773/Python3-check-diagonals-Valid-Square | class Solution:
def validSquare(self, p1: List[int], p2: List[int], p3: List[int], p4: List[int]) -> bool:
c = [complex(*p) for p in sorted([p1, p2, p3, p4])]
d1 = c[3] - c[0]
d2 = c[2] - c[1]
return (abs(d1) == abs(d2) > 0 and
d1.real * d2.real + d1.imag * d2.imag ==... | valid-square | Python3 check diagonals - Valid Square | r0bertz | 1 | 224 | valid square | 593 | 0.44 | Medium | 10,265 |
https://leetcode.com/problems/valid-square/discuss/1890050/Using-set-check-set-length-Python3 | class Solution:
def validSquare(self, p1: List[int], p2: List[int], p3: List[int], p4: List[int]) -> bool:
points = set()
points.add(tuple(p1))
points.add(tuple(p2))
points.add(tuple(p3))
points.add(tuple(p4))
if len(points)<4:
return False
def cal... | valid-square | Using set, check set length, Python3 | rexcancode | 0 | 35 | valid square | 593 | 0.44 | Medium | 10,266 |
https://leetcode.com/problems/valid-square/discuss/1800744/Python3-accepted-solution | class Solution:
def validSquare(self, p1: List[int], p2: List[int], p3: List[int], p4: List[int]) -> bool:
d1 = (p1[0]-p2[0])**2 + (p1[1]-p2[1])**2
d2 = (p2[0]-p3[0])**2 + (p2[1]-p3[1])**2
d3 = (p3[0]-p4[0])**2 + (p3[1]-p4[1])**2
d4 = (p4[0]-p1[0])**2 + (p4[1]-p1[1])**2
d5 =... | valid-square | Python3 accepted solution | sreeleetcode19 | 0 | 73 | valid square | 593 | 0.44 | Medium | 10,267 |
https://leetcode.com/problems/valid-square/discuss/1538033/Python3-check-edges | class Solution:
def validSquare(self, p1: List[int], p2: List[int], p3: List[int], p4: List[int]) -> bool:
if p1==p2==p3==p4:return False
Edges = self.calculateEdges(p1,p2,p3,p4)
return self.checkEdges(Edges) and self.checkDiagonal(Edges)
def dist(self,a,b):
... | valid-square | [Python3] check edges | zhanweiting | 0 | 105 | valid square | 593 | 0.44 | Medium | 10,268 |
https://leetcode.com/problems/valid-square/discuss/933979/Python-Easy-4-line-solution-beats-90 | class Solution(object):
def validSquare(self, p1, p2, p3, p4):
def cal(a,b):
return (abs(a[0]-b[0]) + abs(a[1]-b[1]))
res = sorted([ cal(p1,p2),cal(p2,p3),cal(p3,p4),cal(p4,p1),cal(p1,p3),cal(p2,p4)])
return 0<res[0]==res[1]==res[2]==res[3] and res[4]==res[5] | valid-square | [Python] Easy 4 line solution beats 90% | rachitsxn292 | 0 | 208 | valid square | 593 | 0.44 | Medium | 10,269 |
https://leetcode.com/problems/valid-square/discuss/932760/Valid-Square-Solution-by-Hashing-with-Explanation | class Solution:
def validSquare(self, p1: List[int], p2: List[int], p3: List[int], p4: List[int]) -> bool:
d={}
l=[p1,p2,p3,p4]
for i in range(3):
for j in range(i+1,4):
distance=(l[i][0]-l[j][0])**2+(l[i][1]-l[j][1])**2
if d.get(distance,0)!=0:
... | valid-square | Valid Square Solution by Hashing with Explanation | keshavsingh4522 | 0 | 36 | valid square | 593 | 0.44 | Medium | 10,270 |
https://leetcode.com/problems/valid-square/discuss/885289/Python3-frequency-table | class Solution:
def validSquare(self, p1: List[int], p2: List[int], p3: List[int], p4: List[int]) -> bool:
pts = [p1, p2, p3, p4]
freq = {}
for i, (x, y) in enumerate(pts):
for xx, yy in pts[i+1:]:
d2 = (x - xx)**2 + (y - yy)**2
freq[d2] = 1 + freq... | valid-square | [Python3] frequency table | ye15 | 0 | 74 | valid square | 593 | 0.44 | Medium | 10,271 |
https://leetcode.com/problems/valid-square/discuss/827796/Python-3-or-Pythagoras-theorem-or-Explanation | class Solution:
def validSquare(self, p1: List[int], p2: List[int], p3: List[int], p4: List[int]) -> bool:
if not p1 != p2 != p3 != p4: return False # 4 points have to be different
dis = lambda x, y: (y[1]-x[1])**2 + (y[0]-x[0])**2 # lambda function to calc distance**2
points = ... | valid-square | Python 3 | Pythagoras theorem | Explanation | idontknoooo | 0 | 193 | valid square | 593 | 0.44 | Medium | 10,272 |
https://leetcode.com/problems/valid-square/discuss/704376/Simple-Python-Logic-O(1) | class Solution:
# Time: O(1)
# Space: O(1)
def validSquare(self, p1: List[int], p2: List[int], p3: List[int], p4: List[int]) -> bool:
points = [p1, p2, p3, p4]
prev_l = set()
for i, (x, y) in enumerate(points):
others = points[:i] + points[i + 1:]
l = set()
... | valid-square | Simple Python Logic O(1) | whissely | 0 | 115 | valid square | 593 | 0.44 | Medium | 10,273 |
https://leetcode.com/problems/valid-square/discuss/392187/Solution-in-Python-3-(beats-~99)-(With-Explanation)-(two-lines)-(Using-Geometry) | class Solution:
def validSquare(self, p1: List[int], p2: List[int], p3: List[int], p4: List[int]) -> bool:
D = [(i[0]-j[0])**2+(i[1]-j[1])**2 for i,j in itertools.combinations([p1,p2,p3,p4],2)]
return False if not D else [D.count(min(D)),D.count(max(D))] == [4,2]
- Junaid Mansuri
(LeetCode ID)@hotma... | valid-square | Solution in Python 3 (beats ~99%) (With Explanation) (two lines) (Using Geometry) | junaidmansuri | 0 | 299 | valid square | 593 | 0.44 | Medium | 10,274 |
https://leetcode.com/problems/longest-harmonious-subsequence/discuss/1049126/Python.-O(n)-Cool-easy-and-clear-solution. | class Solution:
def findLHS(self, nums: List[int]) -> int:
tmp = Counter(nums)
keys = tmp.keys()
max = 0
for num in keys:
if num - 1 in keys:
if tmp[num - 1] + tmp[num] > max:
max = tmp[num - 1] + tmp[num]
return max | longest-harmonious-subsequence | Python. O(n), Cool, easy & clear solution. | m-d-f | 6 | 494 | longest harmonious subsequence | 594 | 0.532 | Easy | 10,275 |
https://leetcode.com/problems/longest-harmonious-subsequence/discuss/1357135/Easy-Python-Solution(98.82) | class Solution:
def findLHS(self, nums: List[int]) -> int:
s = Counter(nums)
l = 0
for i in s:
if i+1 in s:
l = max(s[i]+s[i+1],l)
return l | longest-harmonious-subsequence | Easy Python Solution(98.82%) | Sneh17029 | 2 | 324 | longest harmonious subsequence | 594 | 0.532 | Easy | 10,276 |
https://leetcode.com/problems/longest-harmonious-subsequence/discuss/1833566/4-Lines-Python-Solution-oror-87-Faster-oror-Memory-less-than-92 | class Solution:
def findLHS(self, nums: List[int]) -> int:
C=Counter(nums) ; mx=0
for i in C:
if i+1 in C: mx=max(C[i]+C[i+1],mx)
return mx | longest-harmonious-subsequence | 4-Lines Python Solution || 87% Faster || Memory less than 92% | Taha-C | 1 | 141 | longest harmonious subsequence | 594 | 0.532 | Easy | 10,277 |
https://leetcode.com/problems/longest-harmonious-subsequence/discuss/1611769/Python-3-O(n)-time-O(n)-space-using-Counter | class Solution:
def findLHS(self, nums: List[int]) -> int:
nums = collections.Counter(nums)
res = 0
for num in nums:
if nums[num + 1]:
res = max(res, nums[num] + nums[num + 1])
return res | longest-harmonious-subsequence | Python 3 O(n) time, O(n) space using Counter | dereky4 | 1 | 233 | longest harmonious subsequence | 594 | 0.532 | Easy | 10,278 |
https://leetcode.com/problems/longest-harmonious-subsequence/discuss/1301165/Python3-dollarolution | class Solution:
def findLHS(self, nums: List[int]) -> int:
d = {}
x, m = 0, 0
for i in nums:
if i not in d:
d[i] = 1
else:
d[i] += 1
for i in d:
if i+1 in d:
x = d[i] + d[i+1]
if m < x... | longest-harmonious-subsequence | Python3 $olution | AakRay | 1 | 141 | longest harmonious subsequence | 594 | 0.532 | Easy | 10,279 |
https://leetcode.com/problems/longest-harmonious-subsequence/discuss/2757857/Python-easy-solution-using-counter | class Solution:
def findLHS(self, nums: List[int]) -> int:
c=Counter(nums)
res=0
for i,j in c.items():
if(i+1 in c):
d=j+c[i+1]
res=max(d,res)
return res | longest-harmonious-subsequence | Python easy solution using counter | Raghunath_Reddy | 0 | 7 | longest harmonious subsequence | 594 | 0.532 | Easy | 10,280 |
https://leetcode.com/problems/longest-harmonious-subsequence/discuss/1957618/Python-Clean-and-Simple-%2B-One-Liner! | class Solution:
def findLHS(self, nums):
res = 0
counter = Counter(nums)
for c in counter:
if c+1 in counter:
res = max(res, counter[c] + counter[c+1])
return res | longest-harmonious-subsequence | Python - Clean and Simple + One-Liner! | domthedeveloper | 0 | 126 | longest harmonious subsequence | 594 | 0.532 | Easy | 10,281 |
https://leetcode.com/problems/longest-harmonious-subsequence/discuss/1957618/Python-Clean-and-Simple-%2B-One-Liner! | class Solution:
def findLHS(self, nums):
c = Counter(nums)
return max((c[v]+c[v+1] for v in c if v+1 in c), default=0) | longest-harmonious-subsequence | Python - Clean and Simple + One-Liner! | domthedeveloper | 0 | 126 | longest harmonious subsequence | 594 | 0.532 | Easy | 10,282 |
https://leetcode.com/problems/longest-harmonious-subsequence/discuss/1957618/Python-Clean-and-Simple-%2B-One-Liner! | class Solution:
def findLHS(self, nums):
return (lambda c : max((c[v]+c[v+1] for v in c if v+1 in c), default=0))(Counter(nums)) | longest-harmonious-subsequence | Python - Clean and Simple + One-Liner! | domthedeveloper | 0 | 126 | longest harmonious subsequence | 594 | 0.532 | Easy | 10,283 |
https://leetcode.com/problems/longest-harmonious-subsequence/discuss/1512491/python-O(n)-time-O(n)-space-solution | class Solution:
def findLHS(self, nums: List[int]) -> int:
count = defaultdict(int)
for n in nums:
count[n] += 1
maxv = 0
for i in count:
a = count.get(i, 0)
b = count.get(i+1, 0)
if a * b > 0:
maxv = max(m... | longest-harmonious-subsequence | python O(n) time, O(n) space solution | byuns9334 | 0 | 78 | longest harmonious subsequence | 594 | 0.532 | Easy | 10,284 |
https://leetcode.com/problems/longest-harmonious-subsequence/discuss/1251307/Python-Counter | class Solution:
def findLHS(self, nums: List[int]) -> int:
counts = collections.Counter(nums)
score = 0
for i in nums:
a, b, c = counts[i+1], counts[i], counts[i-1]
score = max(
score,
a+b if a and b else 0,
... | longest-harmonious-subsequence | Python Counter | dev-josh | 0 | 43 | longest harmonious subsequence | 594 | 0.532 | Easy | 10,285 |
https://leetcode.com/problems/longest-harmonious-subsequence/discuss/1055041/Python-Explanations-of-two-solutions-%3A-O(NlogN)-and-O(N) | class Solution:
def findLHS(self, nums: List[int]) -> int:
nums.sort()
ind = 0
tup = (None, None)
ans = 0
while(ind < len(nums)):
st = None
if tup[0] is not None:
st = tup[0]
while(st <= ind):
if nums... | longest-harmonious-subsequence | [Python] Explanations of two solutions :- O(NlogN) and O(N) | vasu6 | 0 | 84 | longest harmonious subsequence | 594 | 0.532 | Easy | 10,286 |
https://leetcode.com/problems/longest-harmonious-subsequence/discuss/1055041/Python-Explanations-of-two-solutions-%3A-O(NlogN)-and-O(N) | class Solution:
def findLHS(self, nums: List[int]) -> int:
dp = defaultdict(int)
for num in nums:
dp[num] += 1
ans = 0
for num in nums:
ans = max(ans, ((dp[num] + dp[num + 1]) if num + 1 in dp else 0))
return ans | longest-harmonious-subsequence | [Python] Explanations of two solutions :- O(NlogN) and O(N) | vasu6 | 0 | 84 | longest harmonious subsequence | 594 | 0.532 | Easy | 10,287 |
https://leetcode.com/problems/longest-harmonious-subsequence/discuss/1050341/Python-3-Intuitive-solution | class Solution:
def findLHS(self, nums: List[int]) -> int:
mapping = Counter(nums)
cnt = 0
# iterate the list
for num in nums:
# if valid numbers exist
if mapping[num-1] or mapping[num+1]:
# find the max count as of this position
cnt = max(cnt, mapping[nu... | longest-harmonious-subsequence | Python 3, Intuitive solution | geoffrey0104 | 0 | 26 | longest harmonious subsequence | 594 | 0.532 | Easy | 10,288 |
https://leetcode.com/problems/longest-harmonious-subsequence/discuss/1049750/Python3-freq-table | class Solution:
def findLHS(self, nums: List[int]) -> int:
freq = {}
for x in nums: freq[x] = 1 + freq.get(x, 0)
ans = 0
for x in nums:
if x + 1 in freq: ans = max(ans, freq[x] + freq[x+1])
return ans | longest-harmonious-subsequence | [Python3] freq table | ye15 | 0 | 28 | longest harmonious subsequence | 594 | 0.532 | Easy | 10,289 |
https://leetcode.com/problems/longest-harmonious-subsequence/discuss/1049594/PythonPython3-Longest-Harmonious-Subsequence | class Solution:
def findLHS(self, nums: List[int]) -> int:
freq = Counter(nums)
max_len = 0
for elem in freq:
if elem-1 in freq:
possible_max1 = freq[elem] + freq[elem-1]
max_len = possible_max1 if possible_max1 > max_len else max... | longest-harmonious-subsequence | [Python/Python3] Longest Harmonious Subsequence | newborncoder | 0 | 124 | longest harmonious subsequence | 594 | 0.532 | Easy | 10,290 |
https://leetcode.com/problems/longest-harmonious-subsequence/discuss/972979/Python3-solution-Using-hashmap | class Solution:
def findLHS(self, nums: List[int]) -> int:
dicti = defaultdict(list)
for i in range(len(nums)):
dicti[nums[i]].append(i)
lst = sorted(dicti.keys())
index = []
for i in range(len(lst)-1):
if abs(lst[i]-lst[i+1])==1:... | longest-harmonious-subsequence | Python3 solution Using hashmap | swap2001 | 0 | 64 | longest harmonious subsequence | 594 | 0.532 | Easy | 10,291 |
https://leetcode.com/problems/longest-harmonious-subsequence/discuss/959371/Python3-simple-counter | class Solution:
def findLHS(self, nums: List[int]) -> int:
c = Counter(nums)
res = 0
for x in set(nums):
less, big = x - 1, x + 1
if less in c or big in c:
res = max(res, c[less] + c[x], c[big] + c[x])
return res | longest-harmonious-subsequence | Python3 simple counter | ermolushka2 | 0 | 81 | longest harmonious subsequence | 594 | 0.532 | Easy | 10,292 |
https://leetcode.com/problems/range-addition-ii/discuss/1434604/C-or-C%2B%2B-or-Python-or-Java-or-C-or-JavaScript-or-Go-Same-Simple-Solution | class Solution:
def maxCount(self, m: int, n: int, ops: List[List[int]]) -> int:
min_row = m
min_col = n
for i in range(len(ops)):
min_row=min(min_row, ops[i][0])
min_col=min(min_col, ops[i][1])
return min_row*min_col | range-addition-ii | [ C | C++ | Python | Java | C# | JavaScript | Go ] Same Simple Solution | HadaEn | 23 | 2,000 | range addition ii | 598 | 0.551 | Easy | 10,293 |
https://leetcode.com/problems/range-addition-ii/discuss/380432/Solution-in-Python-3-(one-line)-(beats-~98) | class Solution:
def maxCount(self, m: int, n: int, p: List[List[int]]) -> int:
return min([i[0] for i in p])*min(i[1] for i in p) if p else m*n
- Junaid Mansuri
(LeetCode ID)@hotmail.com | range-addition-ii | Solution in Python 3 (one line) (beats ~98%) | junaidmansuri | 4 | 385 | range addition ii | 598 | 0.551 | Easy | 10,294 |
https://leetcode.com/problems/range-addition-ii/discuss/1842230/1-Line-Python-Solution-oror-96-Faster-(68ms)-oror-Memory-less-than-90 | class Solution:
def maxCount(self, m: int, n: int, ops: List[List[int]]) -> int:
return min([x for x,y in ops])*min([y for x,y in ops]) if ops else m*n | range-addition-ii | 1-Line Python Solution || 96% Faster (68ms) || Memory less than 90% | Taha-C | 3 | 142 | range addition ii | 598 | 0.551 | Easy | 10,295 |
https://leetcode.com/problems/range-addition-ii/discuss/374724/Simon's-Note-Python3-Easy-to-understand | class Solution:
def maxCount(self, m: int, n: int, ops: List[List[int]]) -> int:
r_min=m
c_min=n
for i in ops:
if i[0]<r_min:
r_min=i[0]
if i[1]<c_min:
c_min=i[1]
return r_min*c_min | range-addition-ii | [🎈Simon's Note🎈] Python3 Easy to understand | SunTX | 3 | 183 | range addition ii | 598 | 0.551 | Easy | 10,296 |
https://leetcode.com/problems/range-addition-ii/discuss/1217014/Python3-simple-solution | class Solution:
def maxCount(self, m: int, n: int, ops: List[List[int]]) -> int:
for i in ops:
m,n = min(m,i[0]), min(n,i[1])
return m*n | range-addition-ii | Python3 simple solution | EklavyaJoshi | 1 | 78 | range addition ii | 598 | 0.551 | Easy | 10,297 |
https://leetcode.com/problems/range-addition-ii/discuss/1596699/Python-3-faster-than-99 | class Solution:
def maxCount(self, m: int, n: int, ops: List[List[int]]) -> int:
if not ops:
return m * n
ops = list(zip(*ops))
return min(ops[0]) * min(ops[1]) | range-addition-ii | Python 3 faster than 99% | dereky4 | 0 | 179 | range addition ii | 598 | 0.551 | Easy | 10,298 |
https://leetcode.com/problems/range-addition-ii/discuss/1458959/Python3-One-line-solution | class Solution:
def maxCount(self, m: int, n: int, ops: List[List[int]]) -> int:
return min([op[0] for op in ops] + [m])*min([op[1] for op in ops] + [n]) | range-addition-ii | [Python3] One-line solution | terrencetang | 0 | 91 | range addition ii | 598 | 0.551 | Easy | 10,299 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.