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/construct-string-from-binary-tree/discuss/2543356/606.-Construct-String-from-Binary-Tree | class Solution:
def tree2str(self, root: Optional[TreeNode]) -> str:
st=[]
def solver(root):
if not(root):
return
st.append(str(root.val))
if root.left==None and root.right==None:
return
st.append('(')
solver(root.left)
st.append(')')
if root.right:
st.append('(')
solver(r... | construct-string-from-binary-tree | 606. Construct String from Binary Tree | 12gaurav | 0 | 6 | construct string from binary tree | 606 | 0.636 | Easy | 10,400 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/2543325/Daily-LeetCoding-Challenge-September-Day-7 | class Solution:
def tree2str(self, root: Optional[TreeNode]) -> str:
if root is None:
return ""
if root.left is None and root.right is None:
return str(root.val)
if root.right is None:
return str(root.val) + "(" + self.tree2str(root.left) + ")"
ret... | construct-string-from-binary-tree | Daily LeetCoding Challenge September, Day 7 | saranshs2021 | 0 | 2 | construct string from binary tree | 606 | 0.636 | Easy | 10,401 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/2543293/Python-one-liner | class Solution:
def tree2str(self, root: Optional[TreeNode]) -> str:
return str(root.val) + (f'({self.tree2str(root.left)})' if root.left or root.right else '') + (f'({self.tree2str(root.right)})' if root.right else '') if root else '' | construct-string-from-binary-tree | Python one-liner | meatcodex | 0 | 11 | construct string from binary tree | 606 | 0.636 | Easy | 10,402 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/2542830/C%2B%2BPython-or-Easy-to-understand-solution-or-2-different-implementations | class Solution:
def tree2str(self, root: Optional[TreeNode]) -> str:
if not root :
return ""
ans = str(root.val)
if root.left :
ans += f"({self.tree2str(root.left)})"
elif root.right :
ans += "()"
if root.right :
ans += f"({s... | construct-string-from-binary-tree | [C++/Python] | Easy to understand solution | 2 different implementations | prakharrai1609 | 0 | 18 | construct string from binary tree | 606 | 0.636 | Easy | 10,403 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/2542634/Self-explanatory-code-in-Python-(Faster-than-92-of-python3-submission) | class Solution:
def tree2str(self, root: Optional[TreeNode]) -> str:
def preOrder(root):
nonlocal s
if(root is None):
return
s+=str(root.val)
s+="("
preOrder(root.left)
s+=")"
if(root.right):
... | construct-string-from-binary-tree | Self explanatory code in Python (Faster than 92% of python3 submission) | ravishk17 | 0 | 8 | construct string from binary tree | 606 | 0.636 | Easy | 10,404 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/2542480/Python-or-Clarifying-the-problem-or-Recursive | class Solution:
def tree2str(self, root: Optional[TreeNode]) -> str:
def stringBuilder(node: Optional[TreeNode] = root) -> str:
stringTree = str(node.val) # first do node
if node.left is not None: # then do node.left
stringTree += f"({stringBui... | construct-string-from-binary-tree | Python | Clarifying the problem | Recursive | sr_vrd | 0 | 18 | construct string from binary tree | 606 | 0.636 | Easy | 10,405 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/2542307/Python-DFS-Recursive-Optimal-Solution | class Solution:
def tree2str(self, root: Optional[TreeNode]) -> str:
if not root:
return ''
if not root.left and not root.right:
return str(root.val)
left = ''
right = ''
if root.left:
left = self.tree2str(root.le... | construct-string-from-binary-tree | Python DFS Recursive Optimal Solution | EdwinJagger | 0 | 9 | construct string from binary tree | 606 | 0.636 | Easy | 10,406 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/2542307/Python-DFS-Recursive-Optimal-Solution | class Solution:
def tree2str(self, root: Optional[TreeNode]) -> str:
if not root:
return ''
if not root.left and not root.right:
return str(root.val)
left = self.tree2str(root.left)
right = self.tree2str(root.right)
return f'{root.va... | construct-string-from-binary-tree | Python DFS Recursive Optimal Solution | EdwinJagger | 0 | 9 | construct string from binary tree | 606 | 0.636 | Easy | 10,407 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/2542298/Python-DFS | class Solution:
def tree2str(self, root: Optional[TreeNode]) -> str:
if not root:
return ""
if root.left:
left = "(" + self.tree2str(root.left) + ")"
else:
left = "()" if root.right else ""
if root.right:
right = "... | construct-string-from-binary-tree | Python, DFS | blue_sky5 | 0 | 19 | construct string from binary tree | 606 | 0.636 | Easy | 10,408 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/2514670/Python-Solution-or-Easy-or-Comments-or-Recursion | class Solution:
def tree2str(self, root: Optional[TreeNode]) -> str:
def tree2strUtil(root):
# initialise as empty string
ans=''
if not root:
return ans
ans+=str(root.val)
# if no child then just return node value
if not... | construct-string-from-binary-tree | Python Solution | Easy | Comments | Recursion | Siddharth_singh | 0 | 25 | construct string from binary tree | 606 | 0.636 | Easy | 10,409 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/2511021/Python-recursive-Easiest | class Solution:
def tree2str(self, root: Optional[TreeNode]) -> str:
@ lru_cache(None)
def dfs(node):
s = ''
if not node:
return ''
if not node.left and not node.right:
return str(node.val)
s+= str(node.val)... | construct-string-from-binary-tree | Python recursive Easiest | Abhi_009 | 0 | 14 | construct string from binary tree | 606 | 0.636 | Easy | 10,410 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/2212266/Python-One-to-One-relationship-explanation-with-example | class Solution:
def tree2str(self, root: Optional[TreeNode]) -> str:
if root:
# Preorder traversal
parent_str = str(root.val)
left_child = self.tree2str(root.left)
right_child = self.tree2str(root.right)
# Building strings
left_child_str = "(" + str(le... | construct-string-from-binary-tree | [Python] One-to-One relationship explanation with example | hotassun | 0 | 41 | construct string from binary tree | 606 | 0.636 | Easy | 10,411 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/1514249/python-simplest-solution | class Solution:
def tree2str(self, root: Optional[TreeNode]) -> str:
def preorder(node):
if not node:
return ""
else:
l = preorder(node.left)
r = preorder(node.right)
if l == '' and r == '':
... | construct-string-from-binary-tree | python simplest solution | byuns9334 | 0 | 227 | construct string from binary tree | 606 | 0.636 | Easy | 10,412 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/1404557/python3-gfg-solution | class Solution:
def tree2str(self, root: Optional[TreeNode]) -> str:
def func(root,string):
if root is None:
return
string.append(str(root.val))
if not root.left and not root.right:
return
string.append('(')
... | construct-string-from-binary-tree | python3 gfg solution | minato_namikaze | 0 | 89 | construct string from binary tree | 606 | 0.636 | Easy | 10,413 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/1391432/Simple-Python-Recursion | class Solution(object):
def tree2str(self, root, s=""):
s += str(root.val)
# If the node has a left child then search
if root.left:
s += "(" + self.tree2str(root.left) + ")"
elif root.right:
s += "()"
# If the node has a right child then search
if ... | construct-string-from-binary-tree | Simple Python Recursion | Cesarades | 0 | 112 | construct string from binary tree | 606 | 0.636 | Easy | 10,414 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/1360588/Fast-time-and-little-memory-Python-Solution | class Solution:
def tree2str(self, root: TreeNode) -> str:
if root.left == None and root.right == None:
return str(root.val)
else:
r= 0
right = ''
left = ''
if root.left:
left = self.tree2str(root.left)
if root.... | construct-string-from-binary-tree | Fast time and little memory Python Solution | angelg2399 | 0 | 45 | construct string from binary tree | 606 | 0.636 | Easy | 10,415 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/1340054/Python-simple-recursion | class Solution:
def tree2str(self, root: TreeNode) -> str:
def rec(node: TreeNode) -> str:
out = str(node.val)
if node.left:
out += f"({rec(node.left)})"
elif node.right:
out += "()"
if node.right:
... | construct-string-from-binary-tree | Python simple recursion | MihailP | 0 | 100 | construct string from binary tree | 606 | 0.636 | Easy | 10,416 |
https://leetcode.com/problems/find-duplicate-file-in-system/discuss/2595019/LeetCode-The-Hard-Way-Explained-Line-By-Line | class Solution:
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
m = defaultdict(list)
for p in paths:
# 1. split the string by ' '
path = p.split()
# the first string is the directory path
# the rest of them are just file names with conte... | find-duplicate-file-in-system | 🔥 [LeetCode The Hard Way] 🔥 Explained Line By Line | wingkwong | 28 | 1,200 | find duplicate file in system | 609 | 0.678 | Medium | 10,417 |
https://leetcode.com/problems/find-duplicate-file-in-system/discuss/1215957/PythonPython3-solution-using-dictonary | class Solution:
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
dic = {}
duplicateFiles=[]
for filePath in paths:
fileNames = filePath.split() #Split the path to filenames
directoryPath = fileNames[0] #To take only the directory from the given filePath
... | find-duplicate-file-in-system | Python/Python3 solution using dictonary | prasanthksp1009 | 11 | 458 | find duplicate file in system | 609 | 0.678 | Medium | 10,418 |
https://leetcode.com/problems/find-duplicate-file-in-system/discuss/2597091/Python-Simple-Python-Solution-Using-Dictionary-or-HashMap | class Solution:
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
result = []
directory = {}
for path in paths:
path = path.split()
dir_name = path[0]
for file in path[1:]:
start_index = file.index('(')
end_index = file.index(')')
content = file[start_index + 1 : end_inde... | find-duplicate-file-in-system | [ Python ] ✅✅ Simple Python Solution Using Dictionary | HashMap🥳✌👍 | ASHOK_KUMAR_MEGHVANSHI | 2 | 39 | find duplicate file in system | 609 | 0.678 | Medium | 10,419 |
https://leetcode.com/problems/find-duplicate-file-in-system/discuss/1510732/python-simple-solution | class Solution:
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
h = defaultdict(list)
for path in paths:
files = path.split(' ')
dr = files[0]
for file in files[1:]:
i = file.index('(')
content = file[i:][1:-1]
... | find-duplicate-file-in-system | python simple solution | byuns9334 | 2 | 149 | find duplicate file in system | 609 | 0.678 | Medium | 10,420 |
https://leetcode.com/problems/find-duplicate-file-in-system/discuss/2598209/pythonic | class Solution:
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
data = defaultdict(list)
for path in paths:
directory, files = path.split(" ", 1)
for file in files.split():
file_content = file[file.index("("):-1]
data[file_content... | find-duplicate-file-in-system | pythonic | ckvb18 | 1 | 14 | find duplicate file in system | 609 | 0.678 | Medium | 10,421 |
https://leetcode.com/problems/find-duplicate-file-in-system/discuss/2597725/pretty-short-python-solution-using-hashmap | class Solution:
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
d=defaultdict(list)
for i in paths:
dirs=i.split()
files=[dirs[k] for k in range(1,len(dirs))]
for j in range(len(files)):
val=files[j].split('(')
d[val[-... | find-duplicate-file-in-system | pretty short python solution using hashmap | benon | 1 | 28 | find duplicate file in system | 609 | 0.678 | Medium | 10,422 |
https://leetcode.com/problems/find-duplicate-file-in-system/discuss/2595003/python3-or-easy-or-explained-or-easy-understanding-or-path | class Solution:
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
dup_dir = {}
for path in paths: # iterating through the paths
path = path.split() # spliting the path to get individual paths and content
for p in path[1:]:
... | find-duplicate-file-in-system | python3 | easy | explained | easy-understanding | path | H-R-S | 1 | 42 | find duplicate file in system | 609 | 0.678 | Medium | 10,423 |
https://leetcode.com/problems/find-duplicate-file-in-system/discuss/1217842/Python-Solution | class Solution:
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
duplicates = {}
for path in paths:
directory, *files = path.split(" ")
for file in files:
idx = file.index('(')
content = file[idx + 1: -1]
directory_... | find-duplicate-file-in-system | Python Solution | mariandanaila01 | 1 | 74 | find duplicate file in system | 609 | 0.678 | Medium | 10,424 |
https://leetcode.com/problems/find-duplicate-file-in-system/discuss/1146563/Python-using-hashmap-faster-than-99.50 | class Solution:
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
file_dict = collections.defaultdict(list)
res = list()
for path in paths:
parent_dir, *files = path.split(' ')
for file in files:
file_name, content = file.split('('... | find-duplicate-file-in-system | Python using hashmap faster than 99.50% | keewook2 | 1 | 184 | find duplicate file in system | 609 | 0.678 | Medium | 10,425 |
https://leetcode.com/problems/find-duplicate-file-in-system/discuss/2808339/simple-python-solution | class Solution:
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
contentManager = {}
for path in paths:
temp = path.split(" ")
root = temp[0]
for contents in temp[1:]:
content = contents.split("(")
c = content[1][:-1]
... | find-duplicate-file-in-system | simple python solution | chj2788 | 0 | 4 | find duplicate file in system | 609 | 0.678 | Medium | 10,426 |
https://leetcode.com/problems/find-duplicate-file-in-system/discuss/2599112/Python-or-Compact-solution-with-hashmap-and-tuple-unpacking | class Solution:
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
pathsDict = defaultdict(list)
for directory in paths:
path, *files = directory.split()
for file in files:
name, content = file.split(".txt(")
pathsDict[content].app... | find-duplicate-file-in-system | Python | Compact solution with hashmap and tuple unpacking | sr_vrd | 0 | 2 | find duplicate file in system | 609 | 0.678 | Medium | 10,427 |
https://leetcode.com/problems/find-duplicate-file-in-system/discuss/2597875/CAN-ANYONE-OPTIMIZE-MY-APPROACHHELP!!!(PYTHON3) | class Solution:
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
p,x,y,z=[],[],[],[]
for i in paths:
p.append(i.split(' '))
for j in range(len(p)):
for k in range(len(p[j])):
for w in range(len(p[j][k])):
if p[j][k][w]=... | find-duplicate-file-in-system | CAN ANYONE OPTIMIZE MY APPROACH??HELP!!!(PYTHON3) | DG-Problemsolver | 0 | 6 | find duplicate file in system | 609 | 0.678 | Medium | 10,428 |
https://leetcode.com/problems/find-duplicate-file-in-system/discuss/2597735/Clean-solution-in-Python-3-using-regex | class Solution:
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
d = collections.defaultdict(list)
p = re.compile(r"(\w+\.\w+)+\((\w+)\)")
for path in paths:
folder, files = path.split(" ", 1)
for m in p.finditer(files):
d[m.group(2)] += f... | find-duplicate-file-in-system | Clean solution in Python 3, using regex | mousun224 | 0 | 9 | find duplicate file in system | 609 | 0.678 | Medium | 10,429 |
https://leetcode.com/problems/find-duplicate-file-in-system/discuss/2597724/Python-Solution-or-Clean-Code-Simple-Logic-or-Split-and-Hash-Based | class Solution:
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
store = defaultdict(list)
for path in paths:
division = path.split(" ")
currDir = division[0]
for i in range(1,len(division)):
fileName, fileContent... | find-duplicate-file-in-system | Python Solution | Clean Code - Simple Logic | Split and Hash Based | Gautam_ProMax | 0 | 4 | find duplicate file in system | 609 | 0.678 | Medium | 10,430 |
https://leetcode.com/problems/find-duplicate-file-in-system/discuss/2597661/Daily-Challenge-Easy-Solution | class Solution:
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
d={}//to keep the file count according to data,file name as value and file content as key to reference them
for i in paths:
L=i.split()//to get components of the directory
for j in range(1,len(L)):
... | find-duplicate-file-in-system | Daily Challenge Easy Solution | soumya262003 | 0 | 2 | find duplicate file in system | 609 | 0.678 | Medium | 10,431 |
https://leetcode.com/problems/find-duplicate-file-in-system/discuss/2596369/Python-short-straightforward-solution | class Solution:
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
all_files = defaultdict(list)
for item in paths:
path, *files = item.split(' ')
for file in files:
name, content = file.split('(', 2)
all_files[content[:-1]].append(f... | find-duplicate-file-in-system | Python short straightforward solution | meatcodex | 0 | 4 | find duplicate file in system | 609 | 0.678 | Medium | 10,432 |
https://leetcode.com/problems/find-duplicate-file-in-system/discuss/2596332/Python-Solution | class Solution:
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
dictionary = defaultdict(list)
output = []
for path in paths:
s = path.split()
directory = s[0]
for i in range(1, len(s)):
index = s[i].inde... | find-duplicate-file-in-system | Python Solution | mansoorafzal | 0 | 5 | find duplicate file in system | 609 | 0.678 | Medium | 10,433 |
https://leetcode.com/problems/find-duplicate-file-in-system/discuss/2596229/Python-Readable-Python-Solution-and-Follow-Up | class Solution:
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
# make a default dict
duplicates = collections.defaultdict(list)
# go over the files
for path in paths:
# split the path at the space
path = path.split... | find-duplicate-file-in-system | [Python] - Readable Python Solution and Follow-Up | Lucew | 0 | 19 | find duplicate file in system | 609 | 0.678 | Medium | 10,434 |
https://leetcode.com/problems/find-duplicate-file-in-system/discuss/2596101/Simple-%22python%22-Solution | class Solution:
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
c = defaultdict(list)
for path in paths:
path = path.split()
root = path[0]
for f in path[1:]:
file_name,_,content = f.partition("(")
c[content].append(ro... | find-duplicate-file-in-system | Simple "python" Solution | anandchauhan8791 | 0 | 12 | find duplicate file in system | 609 | 0.678 | Medium | 10,435 |
https://leetcode.com/problems/find-duplicate-file-in-system/discuss/2595895/GolangPython-O-(N*K)-time-or-O-(N*K)-space | class Solution:
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
contents = {}
for p in paths:
path,files = p.split(" ",1)
files = files.split()
for f in files:
file_name,content = f.split("(")
content = content[:-1]
... | find-duplicate-file-in-system | Golang/Python O (N*K) time | O (N*K) space | vtalantsev | 0 | 11 | find duplicate file in system | 609 | 0.678 | Medium | 10,436 |
https://leetcode.com/problems/find-duplicate-file-in-system/discuss/2595867/Python-3-or-98.14-fasteror-Using-Dictionary!! | class Solution:
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
# edge case
if not paths : return []
fileName = collections.defaultdict( list )
result = []
for path in paths :
filePath = [ p for p in path.split... | find-duplicate-file-in-system | Python 3 | 98.14% faster✔️| Using Dictionary!! | athrvb | 0 | 18 | find duplicate file in system | 609 | 0.678 | Medium | 10,437 |
https://leetcode.com/problems/find-duplicate-file-in-system/discuss/2595648/Python-or-Hashmap-or-Beats-96-Time-or-O(n-*-k) | class Solution:
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
index = defaultdict(list) # content : path
for p in paths:
root, *files = p.split(' ')
for f in files:
obr = int(f.index('('))
filename = f[:obr]
... | find-duplicate-file-in-system | Python | Hashmap | Beats 96% Time | O(n * k) | dos_77 | 0 | 6 | find duplicate file in system | 609 | 0.678 | Medium | 10,438 |
https://leetcode.com/problems/find-duplicate-file-in-system/discuss/2595459/pythonor-Simple-String-Manipulation-or-without-any-inbuilt-or-faster-than-87.6-or-self-understable | class Solution:
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
mp={}
for i in range(0,len(paths)):
temp=paths[i].split(" ")
path=temp[0]
for j in range(1,len(temp)):
content=temp[j].split("(")
if content[1][:-1] not i... | find-duplicate-file-in-system | python| Simple String Manipulation | without any inbuilt | faster than 87.6 | self understable | Mom94 | 0 | 7 | find duplicate file in system | 609 | 0.678 | Medium | 10,439 |
https://leetcode.com/problems/find-duplicate-file-in-system/discuss/2595194/SIMPLE-PYTHON3-SOLUTION-5-lines | class Solution:
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
hmap = defaultdict(list)
for s in map(str.split, paths):
for v, k in map(lambda x: x.split('('), s[1:]):
hmap[k].append(f'{s[0]}/{v}')
return filter(lambda x: len(x) > 1, hmap.values()) | find-duplicate-file-in-system | ✅✔ SIMPLE PYTHON3 SOLUTION ✅✔5 lines | rajukommula | 0 | 9 | find duplicate file in system | 609 | 0.678 | Medium | 10,440 |
https://leetcode.com/problems/find-duplicate-file-in-system/discuss/2595064/Python-or-Hash-or-Eazy-Pizzzzzzzy-or | class Solution:
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
h = {}
ans = {}
for i in paths:
x = i.split(' ')
dr = x[0]
for j in range(1,len(x)):
f_n,cont = x[j].split('(')
if cont[0:-1] in... | find-duplicate-file-in-system | Python | Hash | Eazy Pizzzzzzzy | | Brillianttyagi | 0 | 11 | find duplicate file in system | 609 | 0.678 | Medium | 10,441 |
https://leetcode.com/problems/find-duplicate-file-in-system/discuss/2594972/python-hashmap | class Solution:
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
contents = defaultdict(list)
for p in paths:
pp = p.split(' ')
for f in pp[1:]:
fp = f.split('(')
contents[fp[1]] += [pp[0]+'/'+fp[0]]
return [v for v in cont... | find-duplicate-file-in-system | python hashmap | li87o | 0 | 13 | find duplicate file in system | 609 | 0.678 | Medium | 10,442 |
https://leetcode.com/problems/find-duplicate-file-in-system/discuss/2594955/Python-Solution | class Solution:
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
result = defaultdict(list)
for path in paths:
root = path.split(' ')
for item in root[1:]:
txt, key = item.split('(')
result[key].append(f'{root[0]}/{txt}')
r... | find-duplicate-file-in-system | Python Solution | hgalytoby | 0 | 8 | find duplicate file in system | 609 | 0.678 | Medium | 10,443 |
https://leetcode.com/problems/find-duplicate-file-in-system/discuss/2594903/Python-or-Hashmap-or-Beginner-Friendly-or-With-Comments | class Solution:
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
hm = {}
ans = []
for path in paths:
## spliting path to root and files
temp_arr = path.split()
if len(temp_arr) <= 1: ## if no file in the path, continue
continue
... | find-duplicate-file-in-system | Python | Hashmap | Beginner Friendly | With Comments | prithuls | 0 | 25 | find duplicate file in system | 609 | 0.678 | Medium | 10,444 |
https://leetcode.com/problems/find-duplicate-file-in-system/discuss/2507262/Easy-to-understand-Python-solution-with-comments | class Solution:
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
duplicates = []
content_map = {} # content: directory
for path in paths:
arr = path.split(' ')
# arr[0] is the directory, arr[1:] are the the files
director... | find-duplicate-file-in-system | Easy to understand Python solution with comments | r00s | 0 | 14 | find duplicate file in system | 609 | 0.678 | Medium | 10,445 |
https://leetcode.com/problems/find-duplicate-file-in-system/discuss/1343892/Python3-solution-using-dictionary | class Solution:
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
d = {}
for i in paths:
for j in i.split(' ')[1:]:
c,e = j.split('(')
d[e[:len(e)-1]] = d.get(e[:len(e)-1],[]) + [i.split(' ')[0]+'/'+c]
return [i for i in d.values() if l... | find-duplicate-file-in-system | Python3 solution using dictionary | EklavyaJoshi | 0 | 71 | find duplicate file in system | 609 | 0.678 | Medium | 10,446 |
https://leetcode.com/problems/find-duplicate-file-in-system/discuss/1215606/Simple-solution-in-Python-using-a-hashmap-to-store-by-the-file-content-as-key | class Solution:
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
hashmap = dict()
for i in paths:
x = i.split(' ')
folder = x[0]
files = x[1:]
for f in files:
y = f.split('(')
txt = y[0]
... | find-duplicate-file-in-system | Simple solution in Python using a hashmap to store by the file content as key | amoghrajesh1999 | 0 | 36 | find duplicate file in system | 609 | 0.678 | Medium | 10,447 |
https://leetcode.com/problems/find-duplicate-file-in-system/discuss/885270/Python3-hash-table | class Solution:
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
mp = {} # mapping from content to file
for path in paths:
files = path.split()
for i in range(1, len(files)):
f, c = files[i].split("(")
mp.setdefault(c[:-1], []).... | find-duplicate-file-in-system | [Python3] hash table | ye15 | 0 | 94 | find duplicate file in system | 609 | 0.678 | Medium | 10,448 |
https://leetcode.com/problems/valid-triangle-number/discuss/884373/Python3-O(N2)-time-solution | class Solution:
def triangleNumber(self, nums: List[int]) -> int:
nums.sort()
ans = 0
for i in range(len(nums)):
lo, hi = 0, i-1
while lo < hi:
if nums[lo] + nums[hi] > nums[i]:
ans += hi - lo
hi -= 1
... | valid-triangle-number | [Python3] O(N^2) time solution | ye15 | 3 | 222 | valid triangle number | 611 | 0.504 | Medium | 10,449 |
https://leetcode.com/problems/valid-triangle-number/discuss/884373/Python3-O(N2)-time-solution | class Solution:
def triangleNumber(self, nums: List[int]) -> int:
nums.sort()
ans = 0
for i in range(len(nums)):
k = i+2
for j in range(i+1, len(nums)):
while k < len(nums) and nums[i] + nums[j] > nums[k]: k += 1
if j < k: ans += k-1-j
... | valid-triangle-number | [Python3] O(N^2) time solution | ye15 | 3 | 222 | valid triangle number | 611 | 0.504 | Medium | 10,450 |
https://leetcode.com/problems/valid-triangle-number/discuss/354606/Two-Solutions-in-Python-3-(Bisect-and-Linear-Scan) | class Solution:
def triangleNumber(self, T: List[int]) -> int:
L, t, _ = len(T), 0, T.sort()
for i in range(L-2):
k = i + 2
for j in range(i+1,L-1):
M = T[i] + T[j] - 1
if M < T[j]: continue
k = bisect.bisect_right(T, M, k)
t += min(k, L) - (j + 1)
return t | valid-triangle-number | Two Solutions in Python 3 (Bisect and Linear Scan) | junaidmansuri | 3 | 1,100 | valid triangle number | 611 | 0.504 | Medium | 10,451 |
https://leetcode.com/problems/valid-triangle-number/discuss/354606/Two-Solutions-in-Python-3-(Bisect-and-Linear-Scan) | class Solution:
def triangleNumber(self, T: List[int]) -> int:
L, t, _ = len(T), 0, T.sort()
for i in range(L-2):
k = i + 2
for j in range(i+1,L-1):
M = T[i] + T[j] - 1
if M < T[j]: continue
while k < L and T[k] <= M: k += 1
t += min(k, L) - (j + 1)
return t
... | valid-triangle-number | Two Solutions in Python 3 (Bisect and Linear Scan) | junaidmansuri | 3 | 1,100 | valid triangle number | 611 | 0.504 | Medium | 10,452 |
https://leetcode.com/problems/valid-triangle-number/discuss/2080621/Python-or-Very-Simple-or-Binary-Search-with-explanation | class Solution(object):
'''
As we know inorder to check if a triangle is valid or not, if its sides are given:=>
A triangle is a valid triangle, If and only If, the sum of any two sides of a triangle is
greater than the third side. For Example, let A, B and C are three sides of a triangle.
... | valid-triangle-number | Python | Very Simple | Binary Search with explanation | __Asrar | 2 | 235 | valid triangle number | 611 | 0.504 | Medium | 10,453 |
https://leetcode.com/problems/valid-triangle-number/discuss/2709531/Python3-or-binary-search | class Solution:
def triangleNumber(self, nums: List[int]) -> int:
n=len(nums)
ans=0
nums.sort()
for i in range(n):
for j in range(i+1,n):
s2s=nums[i]+nums[j]
ind=bisect.bisect_left(nums,s2s)
ans+=max(0,ind-j-1)
retur... | valid-triangle-number | [Python3] | binary-search | swapnilsingh421 | 1 | 56 | valid triangle number | 611 | 0.504 | Medium | 10,454 |
https://leetcode.com/problems/valid-triangle-number/discuss/2589193/Python3-or-Utilized-Binary-Search-With-Two-Pointer-Technique-But-Still-TLE | class Solution:
#Time-Complexity: O(n^2 * logn)
#Space-Complexity: O(1)
def triangleNumber(self, nums: List[int]) -> int:
#Approach: Use two pointer technique to fixate the first two triangle sides.
#For the 3rd side, we will consider the elements after the rightmost index position
#... | valid-triangle-number | Python3 | Utilized Binary Search With Two Pointer Technique But Still TLE ?? | JOON1234 | 0 | 39 | valid triangle number | 611 | 0.504 | Medium | 10,455 |
https://leetcode.com/problems/valid-triangle-number/discuss/2289349/Python-easy-to-read-and-understand-or-sort | class Solution:
def triangleNumber(self, nums: List[int]) -> int:
nums.sort()
n = len(nums)
k = n-1
ans = 0
while k >= 2:
i, j = 0, k-1
while i < j:
if nums[i]+nums[j] > nums[k]:
ans += (j-i)
j -=... | valid-triangle-number | Python easy to read and understand | sort | sanial2001 | 0 | 76 | valid triangle number | 611 | 0.504 | Medium | 10,456 |
https://leetcode.com/problems/valid-triangle-number/discuss/1340999/python-easy-solution | class Solution:
def triangleNumber(self, nums: List[int]) -> int:
count=0
nums.sort()
for i in range (2,len(nums)):
left=0
right=i-1
while(left<right):
if (nums[left]+nums[right]>nums[i]):
count=count+(right-left)
... | valid-triangle-number | python easy solution | minato_namikaze | 0 | 101 | valid triangle number | 611 | 0.504 | Medium | 10,457 |
https://leetcode.com/problems/valid-triangle-number/discuss/1340209/Valid-Triangle-Number-or-Time-Limit-Exceeded-at-case-220-or-Python3 | class Solution:
def triangleNumber(self, nums: List[int]) -> int:
r = 0
if len(nums) < 3:
return r
nums.sort()
for i in range(len(nums)-2):
for j in range(i+1,len(nums)-1):
for k in range(len(nums)-1, j-1, -1):
... | valid-triangle-number | Valid Triangle Number | Time Limit Exceeded at case 220 | Python3 | timhradil | 0 | 124 | valid triangle number | 611 | 0.504 | Medium | 10,458 |
https://leetcode.com/problems/valid-triangle-number/discuss/523516/Python3-simple-O(n2)-time-and-O(logn)-space-solution | class Solution:
def triangleNumber(self, nums: List[int]) -> int:
nums.sort()
count=0
for i in range(len(nums)-2):
k=i+2
for j in range(i+1,len(nums)-1):
if nums[i]==0: continue
while k<len(nums) and nums[i] + nums[j] > nums[k]:
... | valid-triangle-number | Python3 simple O(n^2) time and O(logn) space solution | jb07 | 0 | 183 | valid triangle number | 611 | 0.504 | Medium | 10,459 |
https://leetcode.com/problems/merge-two-binary-trees/discuss/1342175/Elegant-Python-Iterative-and-Recursive-solutions | class Solution:
def mergeTrees(self, root1: TreeNode, root2: TreeNode) -> TreeNode:
if not root1: return root2
if not root2: return root1
queue = deque([(root1, root2)])
while queue:
current_root1, current_root2 = queue.pop()
if current_root1.left and current_... | merge-two-binary-trees | Elegant Python Iterative & Recursive solutions | soma28 | 9 | 453 | merge two binary trees | 617 | 0.786 | Easy | 10,460 |
https://leetcode.com/problems/merge-two-binary-trees/discuss/1342175/Elegant-Python-Iterative-and-Recursive-solutions | class Solution:
def mergeTrees(self, root1: TreeNode, root2: TreeNode) -> TreeNode:
if not root1: return root2
if not root2: return root1
root1.val += root2.val
root1.left = self.mergeTrees(root1.left, root2.left)
root1.right = self.mergeTrees(root1.right, root2.right)
... | merge-two-binary-trees | Elegant Python Iterative & Recursive solutions | soma28 | 9 | 453 | merge two binary trees | 617 | 0.786 | Easy | 10,461 |
https://leetcode.com/problems/merge-two-binary-trees/discuss/1715708/Python-Easy-Recursive-Solution | class Solution:
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
# base case
if root1 is None and root2 is None:
return None
# catching the values of root nodes, if root absert, assign 0
v1 = root1.val if root1 else 0
... | merge-two-binary-trees | Python Easy Recursive Solution | dos_77 | 2 | 110 | merge two binary trees | 617 | 0.786 | Easy | 10,462 |
https://leetcode.com/problems/merge-two-binary-trees/discuss/2306451/easy-to-understand-python3-solution-faster-than-97 | class Solution:
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
return Solution.mergeHelper(self, root1, root2)
def mergeHelper(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
if not root1 and not root... | merge-two-binary-trees | easy to understand python3 solution, faster than 97% | tinasgong | 1 | 51 | merge two binary trees | 617 | 0.786 | Easy | 10,463 |
https://leetcode.com/problems/merge-two-binary-trees/discuss/2065075/Python-Simple-readable-easy-to-understand-recursive-solution-(Faster-than-93) | class Solution:
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
if not root1:
return root2
if not root2:
return root1
root1.val += root2.val
root1.left = self.mergeTrees(root1.left, root2.left)
... | merge-two-binary-trees | [Python] Simple, readable, easy to understand, recursive solution (Faster than 93%) | FedMartinez | 1 | 67 | merge two binary trees | 617 | 0.786 | Easy | 10,464 |
https://leetcode.com/problems/merge-two-binary-trees/discuss/1763544/Python-Simple-Python-Solution-Using-DFS-With-the-help-of-Recursion | class Solution:
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
def PrintTree(node1,node2):
if node1 == None and node2 == None:
return
if node1 != None and node2 == None:
return node1
if node1 == None and node2 != None:
return node2
node1... | merge-two-binary-trees | [ Python ] ✔✔ Simple Python Solution Using DFS With the help of Recursion ✌🔥🔥 | ASHOK_KUMAR_MEGHVANSHI | 1 | 140 | merge two binary trees | 617 | 0.786 | Easy | 10,465 |
https://leetcode.com/problems/merge-two-binary-trees/discuss/1483904/Python3-2-line | class Solution:
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
if not root1 or not root2: return root1 or root2
return TreeNode(root1.val + root2.val, self.mergeTrees(root1.left, root2.left), self.mergeTrees(root1.right, root2.right)) | merge-two-binary-trees | [Python3] 2-line | ye15 | 1 | 62 | merge two binary trees | 617 | 0.786 | Easy | 10,466 |
https://leetcode.com/problems/merge-two-binary-trees/discuss/1279267/5-lines-in-python | class Solution:
def mergeTrees(self, root1: TreeNode, root2: TreeNode) -> TreeNode:
if None in (root1, root2):
return root1 or root2
new_root = TreeNode(root1.val + root2.val)
new_root.left = self.mergeTrees(root1.left, root2.left)
new_root.right = self.mergeTrees(root1.r... | merge-two-binary-trees | 5 lines in python | mousun224 | 1 | 179 | merge two binary trees | 617 | 0.786 | Easy | 10,467 |
https://leetcode.com/problems/merge-two-binary-trees/discuss/2713910/Python-97-*-98-Faster-and-Simple-code | class Solution:
def check(self, node1, node2):
if node1.left and node2.left:
node1.left.val += node2.left.val
self.check(node1.left, node2.left)
elif node1.left is None and node2.left:
node1.left = node2.left
if node1.right and node2.right:
nod... | merge-two-binary-trees | [Python] 97% * 98% Faster and Simple code | jiarow | 0 | 7 | merge two binary trees | 617 | 0.786 | Easy | 10,468 |
https://leetcode.com/problems/merge-two-binary-trees/discuss/2677106/Python3-DFS-8-lines | class Solution:
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
if not root1 and not root2:
return
val1 = root1.val if root1 else 0
val2 = root2.val if root2 else 0
root = TreeNode(val1+val2)
root.left = self.merge... | merge-two-binary-trees | Python3 DFS 8 lines | honeybadgerofdoom | 0 | 5 | merge two binary trees | 617 | 0.786 | Easy | 10,469 |
https://leetcode.com/problems/merge-two-binary-trees/discuss/2670826/Python-3-Easy-Solution-DFS | class Solution:
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
def trev(n1, n2):
if n1:
n2.val+=n1.val
if n1.left and n2.left:
trev(n1.left, n2.left)
if n1.right and n2.right:
... | merge-two-binary-trees | [Python 3] Easy Solution [DFS] | user2667O | 0 | 17 | merge two binary trees | 617 | 0.786 | Easy | 10,470 |
https://leetcode.com/problems/merge-two-binary-trees/discuss/2643385/Python-O(N)-O(1) | class Solution:
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
def build(root1, root2):
if not root1:
return root2
if not root2:
return root1
return TreeN... | merge-two-binary-trees | Python - O(N), O(1) | Teecha13 | 0 | 6 | merge two binary trees | 617 | 0.786 | Easy | 10,471 |
https://leetcode.com/problems/merge-two-binary-trees/discuss/2643025/Python3-dfs-solution | class Solution:
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
def dfs(left,right):
if not left and not right:
return None
leftVal = left.val if left else 0
rightVal = right.... | merge-two-binary-trees | Python3 dfs solution | gurucharandandyala | 0 | 25 | merge two binary trees | 617 | 0.786 | Easy | 10,472 |
https://leetcode.com/problems/merge-two-binary-trees/discuss/2558465/Python-90-Faster | class Solution:
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
if not root1:
return root2
if not root2:
return root1
def solve(node,head = None):
if not node and not head:
return
... | merge-two-binary-trees | Python 90% Faster | Abhi_009 | 0 | 47 | merge two binary trees | 617 | 0.786 | Easy | 10,473 |
https://leetcode.com/problems/merge-two-binary-trees/discuss/2449138/Python-Accurate-DFS-Solution | class Solution:
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
def dfs(node1, node2):
if not node1:
return node2
if not node2:
return node1
node1.val += node2.val
node1.left = d... | merge-two-binary-trees | [Python] Accurate DFS Solution | Buntynara | 0 | 27 | merge two binary trees | 617 | 0.786 | Easy | 10,474 |
https://leetcode.com/problems/merge-two-binary-trees/discuss/2331379/Python-Easy-Solution | class Solution:
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
if root1 and root2:
res = TreeNode(root1.val + root2.val)
res.left= self.mergeTrees(root1.left, root2.left)
res.right = self.mergeTrees(root1.right, root2.rig... | merge-two-binary-trees | ✅Python Easy Solution | Skiper228 | 0 | 79 | merge two binary trees | 617 | 0.786 | Easy | 10,475 |
https://leetcode.com/problems/merge-two-binary-trees/discuss/2217484/Python3-Recursive-and-Iterative-Solutions | class Solution:
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
if not root1 and not root2: return None
if root1 and not root2: return root1
if not root1 and root2: return root2
stack = [(root1, root2)]
while stack:
... | merge-two-binary-trees | ✅Python3 - Recursive and Iterative Solutions | thesauravs | 0 | 25 | merge two binary trees | 617 | 0.786 | Easy | 10,476 |
https://leetcode.com/problems/merge-two-binary-trees/discuss/2217484/Python3-Recursive-and-Iterative-Solutions | class Solution:
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
if not root1 and not root2:
return None
val1 = root1.val if root1 else 0
val2 = root2.val if root2 else 0
if not root1:
root1 = ... | merge-two-binary-trees | ✅Python3 - Recursive and Iterative Solutions | thesauravs | 0 | 25 | merge two binary trees | 617 | 0.786 | Easy | 10,477 |
https://leetcode.com/problems/merge-two-binary-trees/discuss/2111599/Python-Depth-first-Search-Recursion-Solution-(97-Time-78.40-Space) | class Solution:
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
# Depth-first search
def dfs(root, root1, root2):
# Takes a node with self.val and updates it with self.left, self.right
# Base case
... | merge-two-binary-trees | Python Depth-first Search, Recursion Solution (97% Time, 78.40% Space) | tylerpruitt | 0 | 56 | merge two binary trees | 617 | 0.786 | Easy | 10,478 |
https://leetcode.com/problems/merge-two-binary-trees/discuss/1906701/simply-python-dfs | class Solution:
def mergeTrees(self, t1: Optional[TreeNode], t2: Optional[TreeNode]) -> Optional[TreeNode]:
if not t1 and not t2: return None
ans = TreeNode((t1.val if t1 else 0) + (t2.val if t2 else 0))
ans.left = self.mergeTrees(t1 and t1.left, t2 and t2.left)
ans.right = self.mer... | merge-two-binary-trees | simply python dfs | gasohel336 | 0 | 60 | merge two binary trees | 617 | 0.786 | Easy | 10,479 |
https://leetcode.com/problems/merge-two-binary-trees/discuss/1906386/Python3-Recursion-Faster-Than-89.82 | class Solution:
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
def merge(t1, t2):
if t1 is None:
return t2
elif t2 is None:
return t1
t1.val += t2.val
... | merge-two-binary-trees | Python3 Recursion Faster Than 89.82% | Hejita | 0 | 26 | merge two binary trees | 617 | 0.786 | Easy | 10,480 |
https://leetcode.com/problems/merge-two-binary-trees/discuss/1835637/Python-simple-solution-faster-than-99 | class Solution:
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
if root1 and root2:
mergedNode = TreeNode(root1.val + root2.val)
mergedNode.left = self.mergeTrees(root1.left, root2.left)
mergedNode.right = self.mergeTrees(... | merge-two-binary-trees | Python simple solution faster than 99% | mkoseoglu | 0 | 154 | merge two binary trees | 617 | 0.786 | Easy | 10,481 |
https://leetcode.com/problems/merge-two-binary-trees/discuss/1697910/Python-New-Tree-Recursive-approach-simple-and-intuitive | class Solution:
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
if (root1 is None) and (root2 is None):
return None
elif root1 is None:
node = TreeNode(root2.val)
elif root2 is None:
node = TreeNod... | merge-two-binary-trees | Python, New Tree, Recursive approach simple and intuitive | dnz0x | 0 | 38 | merge two binary trees | 617 | 0.786 | Easy | 10,482 |
https://leetcode.com/problems/merge-two-binary-trees/discuss/1638893/Python3-Simple-recursive | class Solution:
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
if not root1 and not root2:
return
if root1 and root2:
root1.val += root2.val
root1.left = self.mergeTrees(root1.left,root2.left)
root1.ri... | merge-two-binary-trees | [Python3] Simple recursive | Rainyforest | 0 | 30 | merge two binary trees | 617 | 0.786 | Easy | 10,483 |
https://leetcode.com/problems/merge-two-binary-trees/discuss/1617788/python-solution | class Solution:
def mergeTrees(self, root1, root2):
if root1 and root2:
new_root = TreeNode(root2.val + root1.val)
new_root.left = self.mergeTrees(root2.left, root1.left)
new_root.right = self.mergeTrees(root2.right, root1.right)
return new_root
else:
return root2 or root1 | merge-two-binary-trees | python solution | fatemebesharat766 | 0 | 81 | merge two binary trees | 617 | 0.786 | Easy | 10,484 |
https://leetcode.com/problems/merge-two-binary-trees/discuss/1453496/Python-very-low-space-complexity-solution | class Solution:
def mergeTrees(self, m: Optional[TreeNode], s: Optional[TreeNode]) -> Optional[TreeNode]:
if not m:
return s
elif s:
m.val += s.val
m.left = self.mergeTrees(m.left,s.left)
m.right = self.mergeTrees(m.right,s.right)
return m | merge-two-binary-trees | Python very low space complexity solution | ericghara | 0 | 89 | merge two binary trees | 617 | 0.786 | Easy | 10,485 |
https://leetcode.com/problems/merge-two-binary-trees/discuss/1418096/Python3-clean-bfs-solution | class Solution:
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
if root1 == None:
return root2
if root2 == None:
return root1
queue1 = collections.deque([root1])
queue2 = collections.deque([root2])
... | merge-two-binary-trees | [Python3] clean bfs solution | maosipov11 | 0 | 136 | merge two binary trees | 617 | 0.786 | Easy | 10,486 |
https://leetcode.com/problems/merge-two-binary-trees/discuss/1418096/Python3-clean-bfs-solution | class Solution:
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
if not root1:
return root2
if not root2:
return root1
queue1 = collections.deque([root1])
queue2 = collections.deque([root2])
... | merge-two-binary-trees | [Python3] clean bfs solution | maosipov11 | 0 | 136 | merge two binary trees | 617 | 0.786 | Easy | 10,487 |
https://leetcode.com/problems/merge-two-binary-trees/discuss/1376816/Python3-faster-than-92.39. | class Solution:
def mergeTrees(self, root1: TreeNode, root2: TreeNode) -> TreeNode:
if root1 is None:
return root2
if root2 is None:
return root1
root1.aval += root2.aval
root1.left = self.mergeTrees(root1.left, root2.left)
root1.right = self.mergeTre... | merge-two-binary-trees | Python3 - faster than 92.39%. | CC_CheeseCake | 0 | 168 | merge two binary trees | 617 | 0.786 | Easy | 10,488 |
https://leetcode.com/problems/merge-two-binary-trees/discuss/1243890/Python3-DFS | class Solution:
def mergeTrees(self, root1: TreeNode, root2: TreeNode) -> TreeNode:
if root1 == root2 == None:
return None
if root1 and root2 == None:
return root1
if root2 and root1 == None:
return root2
if root1 and root2:
... | merge-two-binary-trees | Python3 DFS | rahulkp220 | 0 | 140 | merge two binary trees | 617 | 0.786 | Easy | 10,489 |
https://leetcode.com/problems/task-scheduler/discuss/2667200/Python-O(n)-time-count-it-directly | class Solution:
def leastInterval(self, tasks: List[str], n: int) -> int:
cnt = [0] * 26
for i in tasks: cnt[ord(i) - ord('A')] += 1
mx, mxcnt = max(cnt), 0
for i in cnt:
if i == mx: mxcnt += 1
return max((mx - 1) * (n + 1) + mxcnt, len(tasks)) | task-scheduler | Python O(n) time, count it directly | alex391a | 10 | 1,100 | task scheduler | 621 | 0.558 | Medium | 10,490 |
https://leetcode.com/problems/task-scheduler/discuss/1706018/Python-Simple-Priority-Queue-(max-heap)-explained | class Solution:
def leastInterval(self, tasks: List[str], n: int) -> int:
counter = dict()
for task in tasks:
counter[task] = counter.get(task, 0) + 1
# create a max heap of the frequency
# of the task occuring using the map
hq = list()
for task, ... | task-scheduler | [Python] Simple Priority Queue (max heap) explained | buccatini | 4 | 644 | task scheduler | 621 | 0.558 | Medium | 10,491 |
https://leetcode.com/problems/task-scheduler/discuss/2781024/easy-python-method-with-explanation | class Solution:
def leastInterval(self, tasks: List[str], n: int) -> int:
if n == 0: return len(tasks)
counter = collections.Counter(tasks)
maxCount = 0
maxValue = max(counter.values())
for cha, val in counter.items():
if val == maxValue:
maxCount ... | task-scheduler | easy python method with explanation | rxmoon | 2 | 404 | task scheduler | 621 | 0.558 | Medium | 10,492 |
https://leetcode.com/problems/task-scheduler/discuss/760778/Python-solution-with-easy-to-understand-video-explanation | class Solution:
def leastInterval(self, tasks: List[str], n: int) -> int:
freq = {}
for task in tasks:
if task not in freq:
freq[task] = 1
else:
freq[task] += 1
freq = [value for key, value in freq.items()]
max_freq = m... | task-scheduler | Python solution with easy to understand video explanation | spec_he123 | 2 | 262 | task scheduler | 621 | 0.558 | Medium | 10,493 |
https://leetcode.com/problems/task-scheduler/discuss/1963201/Python-Solution-or-O(n)-or-minHeap-or-queue | class Solution:
def leastInterval(self, tasks: List[str], n: int) -> int:
count = Counter(tasks)
maxHeap = [-cnt for cnt in count.values()]
heapq.heapify(maxHeap)
time = 0
q = deque() # pairs of [-cnt, idleTime]
while maxHeap or q:
time += 1
if not maxHeap:
time = q[0][1]
else:
c... | task-scheduler | Python Solution | O(n) | minHeap | queue | nikhitamore | 1 | 291 | task scheduler | 621 | 0.558 | Medium | 10,494 |
https://leetcode.com/problems/task-scheduler/discuss/1372688/Python-Counter-Solution-(-%2B-a-confusing-3-line-solution) | class Solution:
def leastInterval(self, tasks: List[str], n: int) -> int:
# Least interval is len(tasks) + idle time
tasks_c = collections.Counter(tasks).most_common()
# there is no idle time after the last task, so we subtract 1 from max frequency
idle_time = (tasks_c[0][1]-1) * ... | task-scheduler | Python Counter Solution ( + a confusing 3 line solution) | dimucc | 1 | 249 | task scheduler | 621 | 0.558 | Medium | 10,495 |
https://leetcode.com/problems/task-scheduler/discuss/1372688/Python-Counter-Solution-(-%2B-a-confusing-3-line-solution) | class Solution:
def leastInterval(self, tasks: List[str], n: int) -> int:
tasks_c = collections.Counter(tasks).most_common()
for i in range(len(tasks_c)): idle_time = (tasks_c[0][1]-1) * n if i == 0 else max(idle_time - min(tasks_c[i][1], tasks_c[0][1] - 1), 0)
return len(tasks) + idle_time | task-scheduler | Python Counter Solution ( + a confusing 3 line solution) | dimucc | 1 | 249 | task scheduler | 621 | 0.558 | Medium | 10,496 |
https://leetcode.com/problems/task-scheduler/discuss/760935/python-job-queue-and-waiting-queue | class Solution:
def leastInterval(self, tasks: [str], n: int) -> int:
from collections import Counter
import heapq
jobq = [-v for v in Counter(tasks).values()]
waitingq = []
heapq.heapify(jobq)
ret = 0
while jobq or waitingq:
while waitingq and ret... | task-scheduler | python job queue and waiting queue | hangyu1130 | 1 | 303 | task scheduler | 621 | 0.558 | Medium | 10,497 |
https://leetcode.com/problems/task-scheduler/discuss/2828676/Detailed-scheduling-explanation-Python | class Solution:
def leastInterval(self, tasks: List[str], n: int) -> int:
freq = Counter(tasks).values()
m = max(freq)
c = sum(x == m for x in freq)
r = max(len(tasks), (n + 1) * (m - 1) + c)
return r | task-scheduler | Detailed scheduling explanation [Python] | constantstranger | 0 | 1 | task scheduler | 621 | 0.558 | Medium | 10,498 |
https://leetcode.com/problems/task-scheduler/discuss/2791529/Cool-boi-solution | class Solution:
def leastInterval(self, tasks: List[str], n: int) -> int:
#choose the one with the highest frequency
count = defaultdict(lambda : 0)
for t in tasks:
count[t] += 1
max_h = []
for key in count.keys():
heapq.heappush(max_h,(count[... | task-scheduler | Cool boi solution | ariboi27 | 0 | 8 | task scheduler | 621 | 0.558 | Medium | 10,499 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.