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/range-addition-ii/discuss/1435668/python3-solution-with-comment | class Solution:
def maxCount(self, m: int, n: int, ops: List[List[int]]) -> int:
# When there's no operation
if len(ops) == 0:
return m * n
# Minimum of x and y in operations is the lower right corner which experienced max operation intersections
xs, ys = [], []
... | range-addition-ii | python3 solution with comment | yukikitayama | 0 | 48 | range addition ii | 598 | 0.551 | Easy | 10,300 |
https://leetcode.com/problems/range-addition-ii/discuss/1434512/Python-one-line-linear-solution | class Solution:
def maxCount(self, m: int, n: int, ops: List[List[int]]) -> int:
return n*m if not ops else min(ops, key=lambda x:x[1])[1] * min(ops, key=lambda x:x[0])[0] | range-addition-ii | Python one-line, linear solution | inasaaone | 0 | 53 | range addition ii | 598 | 0.551 | Easy | 10,301 |
https://leetcode.com/problems/range-addition-ii/discuss/1434475/Range-Addition-ll-oror-Linear-solution | class Solution:
def maxCount(self, m: int, n: int, ops: List[List[int]]) -> int:
min_a = m
min_b = n
for a, b in ops:
min_a = min(min_a, a)
min_b = min(min_b, b)
return min_a*min_b | range-addition-ii | Range Addition ll || Linear solution | mdAzhar | 0 | 46 | range addition ii | 598 | 0.551 | Easy | 10,302 |
https://leetcode.com/problems/range-addition-ii/discuss/1434429/Python-3 | class Solution:
def maxCount(self, m: int, n: int, ops: List[List[int]]) -> int:
for i, j in ops:
m = min(m, i)
n = min(n, j)
return m * n | range-addition-ii | Python 3 | vlee | 0 | 35 | range addition ii | 598 | 0.551 | Easy | 10,303 |
https://leetcode.com/problems/range-addition-ii/discuss/1302777/Python3-dollarolution-(100-Faster) | class Solution:
def maxCount(self, m: int, n: int, ops: List[List[int]]) -> int:
l, j = 4*(10**4), 4*(10**4)
if ops == []:
return m*n
else:
for x in ops:
if x[0] < l:
l = x[0]
if x[1] < j:
j = x[1... | range-addition-ii | Python3 $olution (100% Faster) | AakRay | 0 | 121 | range addition ii | 598 | 0.551 | Easy | 10,304 |
https://leetcode.com/problems/range-addition-ii/discuss/1271265/python-clean-oror | class Solution:
def maxCount(self, m: int, n: int, ops: List[List[int]]) -> int:
a,b=99999999999999999,99999999999999999
for i in ops:
a=min(a,i[0])
b= min(b,i[1])
return min(a*b,m*n) | range-addition-ii | python clean || | chikushen99 | 0 | 61 | range addition ii | 598 | 0.551 | Easy | 10,305 |
https://leetcode.com/problems/range-addition-ii/discuss/468906/Python3-Question-about-Space | class Solution:
def maxCount(self, m: int, n: int, ops: List[List[int]]) -> int:
for op in ops:
m = min(m,op[0])
n = min(n,op[1])
return m * n | range-addition-ii | Python3 Question about Space | SilverCHN | 0 | 107 | range addition ii | 598 | 0.551 | Easy | 10,306 |
https://leetcode.com/problems/range-addition-ii/discuss/468906/Python3-Question-about-Space | class Solution:
def maxCount(self, m: int, n: int, ops: List[List[int]]) -> int:
if not ops:
return m*n
return min(op[0] for op in ops)*min(op[1] for op in ops) | range-addition-ii | Python3 Question about Space | SilverCHN | 0 | 107 | range addition ii | 598 | 0.551 | Easy | 10,307 |
https://leetcode.com/problems/minimum-index-sum-of-two-lists/discuss/1382036/Easy-Python-Solution-(Memory-Usage-less-than-97) | class Solution:
def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
d = {}
d2 = {}
min_ = 5000
ret = []
for i in range(len(list1)):
d[list1[i]] = i+1
for i in range(len(list2)):
a = d.get(list2[i], 0)
if a:
... | minimum-index-sum-of-two-lists | Easy Python Solution (Memory Usage less than 97%) | the_sky_high | 3 | 454 | minimum index sum of two lists | 599 | 0.529 | Easy | 10,308 |
https://leetcode.com/problems/minimum-index-sum-of-two-lists/discuss/1302980/Python3-dollarolution(Mem-usage-97-better) | class Solution:
def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
list3 = set(list1) & set(list2)
m, v = 3000, []
for i in list3:
s = list1.index(i) + list2.index(i)
if m > s:
v = [i]
m = s
elif ... | minimum-index-sum-of-two-lists | Python3 $olution(Mem usage 97% better) | AakRay | 2 | 302 | minimum index sum of two lists | 599 | 0.529 | Easy | 10,309 |
https://leetcode.com/problems/minimum-index-sum-of-two-lists/discuss/1211206/Python3-simple-solution-using-set | class Solution:
def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
ans = []
min_index = 10**4
for i in set(list1).intersection(set(list2)):
z = list1.index(i) + list2.index(i)
if min_index > z:
min_index = z
ans ... | minimum-index-sum-of-two-lists | Python3 simple solution using set | EklavyaJoshi | 2 | 140 | minimum index sum of two lists | 599 | 0.529 | Easy | 10,310 |
https://leetcode.com/problems/minimum-index-sum-of-two-lists/discuss/2834764/Simple-Python3-solution-using-sets-and-dictionary | class Solution:
def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
set1 = set(list1)
set2 = set(list2)
common = list(set1 & set2)
d = {}
for i in common:
d[i] = list1.index(i) + list2.index(i)
min_index= min(d.values())
... | minimum-index-sum-of-two-lists | Simple Python3 solution using sets and dictionary | kschaitanya2001 | 1 | 25 | minimum index sum of two lists | 599 | 0.529 | Easy | 10,311 |
https://leetcode.com/problems/minimum-index-sum-of-two-lists/discuss/2439706/Python-easy-solution-for-beginners-using-lists-only | class Solution:
def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
res = []
min_ind_sum = -1
if len(list1) < len(list2):
for i in range(len(list1)):
if list1[i] in list2:
ind_sum = i + list2.index(list1[i])
... | minimum-index-sum-of-two-lists | Python easy solution for beginners using lists only | alishak1999 | 1 | 105 | minimum index sum of two lists | 599 | 0.529 | Easy | 10,312 |
https://leetcode.com/problems/minimum-index-sum-of-two-lists/discuss/2833257/Python-O(n) | class Solution:
def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
memo = {}
for idx, s in enumerate(list1):
memo[s] = idx
min_ = float('inf')
ans_memo = {}
for idx, s in enumerate(list2):
if s in memo:
sum_ = me... | minimum-index-sum-of-two-lists | Python O(n) | chingisoinar | 0 | 1 | minimum index sum of two lists | 599 | 0.529 | Easy | 10,313 |
https://leetcode.com/problems/minimum-index-sum-of-two-lists/discuss/2814897/Simple-Python-Solution | class Solution:
def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
index_sum = []
common = []
for i in list1:
if i in list2:
common.append(i)
index_sum.append(list1.index(i) + list2.index(i))
min_index = min(index_su... | minimum-index-sum-of-two-lists | Simple Python Solution | Shagun_Mittal | 0 | 4 | minimum index sum of two lists | 599 | 0.529 | Easy | 10,314 |
https://leetcode.com/problems/minimum-index-sum-of-two-lists/discuss/2811727/Python-Easy | class Solution:
def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
ans = 3001
res = []
for i in range(len(list1)):
for j in range(len(list2)):
if list1[i] == list2[j]:
if (i+j == ans):
res.append(list2[j])
elif (i+j < ans):
ans = i + j
res = []
r... | minimum-index-sum-of-two-lists | Python Easy | lucasschnee | 0 | 7 | minimum index sum of two lists | 599 | 0.529 | Easy | 10,315 |
https://leetcode.com/problems/minimum-index-sum-of-two-lists/discuss/2652204/python-oror-easy-oror-solution | class Solution:
def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
l1 = {j:i for i,j in enumerate(list1)}
l2 = {j:i for i,j in enumerate(list2)}
d={}
for i in l1:
if i in l2:
val = l1[i]+l2[i]
d[i]=val
m =... | minimum-index-sum-of-two-lists | python || easy || solution | anshsharma17 | 0 | 22 | minimum index sum of two lists | 599 | 0.529 | Easy | 10,316 |
https://leetcode.com/problems/minimum-index-sum-of-two-lists/discuss/2645349/Easy-Python-solution | class Solution:
def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
words = []
index = None
# Iterating through each word in list1
for word1 in range(len(list1)):
# Iterating through each word in list2
for word2 in range(len... | minimum-index-sum-of-two-lists | Easy Python solution | thomasravetto | 0 | 14 | minimum index sum of two lists | 599 | 0.529 | Easy | 10,317 |
https://leetcode.com/problems/minimum-index-sum-of-two-lists/discuss/2615973/Python3-brute-force-using-dictionary-with-comments | class Solution:
def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
# define a dictionary of lists with all possible sums of indexes
out = {key:[] for key in range(len(list1)+len(list2))}
# if common strings exist, enter the list of indices and common string in dictionary
... | minimum-index-sum-of-two-lists | Python3 brute force using dictionary with comments | shwetachandole | 0 | 17 | minimum index sum of two lists | 599 | 0.529 | Easy | 10,318 |
https://leetcode.com/problems/minimum-index-sum-of-two-lists/discuss/2432423/The-cleanest-Python-solution | class Solution:
def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
hmap = {val: i for i, val in enumerate(list1)}
min_idx = float("inf")
answer = []
for i, val in enumerate(list2):
if val not in hmap:
continue
if hmap[va... | minimum-index-sum-of-two-lists | The cleanest Python solution | alexvaclav6 | 0 | 36 | minimum index sum of two lists | 599 | 0.529 | Easy | 10,319 |
https://leetcode.com/problems/minimum-index-sum-of-two-lists/discuss/2382179/Python3-solution-O(l1-%2Bl2)-time-complexity-O(l1-*-avg)-space-complexity-79-faster | class Solution:
def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
table = {name: index for (index, name) in enumerate(list1)}
answer = []
minIndex = 2000
for (index, name) in enumerate(list2):
if name in table:
tempSum = i... | minimum-index-sum-of-two-lists | Python3 solution, O(l1 +l2) time complexity, O(l1 * avg) space complexity, 79% faster | matteogianferrari | 0 | 50 | minimum index sum of two lists | 599 | 0.529 | Easy | 10,320 |
https://leetcode.com/problems/minimum-index-sum-of-two-lists/discuss/2362644/Minimum-Index-Sum-of-Two-Lists | class Solution:
def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
dicti={}
lisp=[]
for i in range(len(list1)):
for j in range(len(list2)):
if list1[i]==list2[j]:
dicti[list1[i]]=list1.index(list1[i])+list2.index(li... | minimum-index-sum-of-two-lists | Minimum Index Sum of Two Lists | Faraz369 | 0 | 49 | minimum index sum of two lists | 599 | 0.529 | Easy | 10,321 |
https://leetcode.com/problems/minimum-index-sum-of-two-lists/discuss/2131078/hashmap | class Solution:
def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
hmap = dict()
for i, r in enumerate(list1):
hmap[r] = i
minSum = len(list1) + len(list2)
for i, r in enumerate(list2):
if r not in hmap:
... | minimum-index-sum-of-two-lists | hashmap | andrewnerdimo | 0 | 96 | minimum index sum of two lists | 599 | 0.529 | Easy | 10,322 |
https://leetcode.com/problems/minimum-index-sum-of-two-lists/discuss/2041962/Python-solution | class Solution:
def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
ans = {}
for i in list1:
if i in list2:
ans[i] = list1.index(i) + list2.index(i)
ans_list = []
for k,v in ans.items():
if v == ans[min(ans, key=lambda x:... | minimum-index-sum-of-two-lists | Python solution | StikS32 | 0 | 145 | minimum index sum of two lists | 599 | 0.529 | Easy | 10,323 |
https://leetcode.com/problems/minimum-index-sum-of-two-lists/discuss/2031787/Faster-than-17-of-online-solutions(423ms)-and-memory-less-than-43-than-online-solutions | class Solution:
def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
if(list1==list2[::-1]):
return (list2)
else:
b=[]
d=[]
for i in list1:
if(i in list2):
a=list1.index(i)+list2.index(i)
... | minimum-index-sum-of-two-lists | Faster than 17% of online solutions(423ms) and memory less than 43% than online solutions | Durgavamsi | 0 | 40 | minimum index sum of two lists | 599 | 0.529 | Easy | 10,324 |
https://leetcode.com/problems/minimum-index-sum-of-two-lists/discuss/2021422/Python-4-Lines-Clean-and-Simple! | class Solution:
def findRestaurant(self, list1, list2):
d1, d2 = dict(), dict()
for i,item in enumerate(list1):
d1[item] = i
for j,item in enumerate(list2):
if item in d1:
d2[item] = d1[item]+j
minIndexSum = min(d... | minimum-index-sum-of-two-lists | Python - 4 Lines - Clean and Simple! | domthedeveloper | 0 | 192 | minimum index sum of two lists | 599 | 0.529 | Easy | 10,325 |
https://leetcode.com/problems/minimum-index-sum-of-two-lists/discuss/2021422/Python-4-Lines-Clean-and-Simple! | class Solution:
def findRestaurant(self, list1, list2):
d1 = {n:i for i,n in enumerate(list1)}
d2 = {n:d1[n]+j for j,n in enumerate(list2) if n in d1}
minIndexSum = min(d2.values())
return [k for k,v in d2.items() if v==minIndexSum] | minimum-index-sum-of-two-lists | Python - 4 Lines - Clean and Simple! | domthedeveloper | 0 | 192 | minimum index sum of two lists | 599 | 0.529 | Easy | 10,326 |
https://leetcode.com/problems/minimum-index-sum-of-two-lists/discuss/1851921/Python-easy-to-read-and-understand-or-hashmap-and-heap | class Solution:
def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
d = collections.defaultdict(int)
for i, item in enumerate(list1):
d[item] = i
pq = []
for i, item in enumerate(list2):
if item in d:
heapq.heapp... | minimum-index-sum-of-two-lists | Python easy to read and understand | hashmap and heap | sanial2001 | 0 | 84 | minimum index sum of two lists | 599 | 0.529 | Easy | 10,327 |
https://leetcode.com/problems/minimum-index-sum-of-two-lists/discuss/1595344/Python-HashMap-Solution | class Solution:
def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
dict1 = {}
res = []
mins = len(list1) + len(list2) - 2
for i,j in enumerate(list1):
dict1[j] = i
for i,j in enumerate(list2):
if j in dict1 and i+dict1[j] <= min... | minimum-index-sum-of-two-lists | Python HashMap Solution | jodyzhou | 0 | 81 | minimum index sum of two lists | 599 | 0.529 | Easy | 10,328 |
https://leetcode.com/problems/minimum-index-sum-of-two-lists/discuss/1468983/Easy-to-understand-Play-with-sets | class Solution:
def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
index_add = []
if len(list1) == len(list2) and list1 == list2[::-1]:
return list2[::-1]
intersection = [restaurent for restaurent in list1 if restaurent in list2]
if len(intersectio... | minimum-index-sum-of-two-lists | Easy to understand, Play with sets | AshwinBalaji52 | 0 | 248 | minimum index sum of two lists | 599 | 0.529 | Easy | 10,329 |
https://leetcode.com/problems/minimum-index-sum-of-two-lists/discuss/1424364/Python3-O(2-*-l1-%2B-l2)-Faster-Than-96.68-Memory-Less-Than-87.34 | class Solution:
def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
d1, d2, mn, v = {}, {}, 999999, []
for index, s in enumerate(list1):
d1[s] = index
for index, s in enumerate(list2):
d2[s] = index
if len(list1) < len(list1)... | minimum-index-sum-of-two-lists | Python3 O(2 * l1 + l2), Faster Than 96.68%, Memory Less Than 87.34% | Hejita | 0 | 63 | minimum index sum of two lists | 599 | 0.529 | Easy | 10,330 |
https://leetcode.com/problems/minimum-index-sum-of-two-lists/discuss/1017486/Can-anyone-help-identify-the-expensive-parts-O(n)-I-think-but-v.low-performance | class Solution:
def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
hashmap = {}; least_sum = len(list1+list2) #O(1)
for i in range(0, len(list2)): #O(n)
if list2[i] in list1:
sum_ = i+list1.index(list2[i]) #O(1)
... | minimum-index-sum-of-two-lists | Can anyone help identify the expensive parts? O(n) [I think] but v.low performance | beatrixOngit | 0 | 123 | minimum index sum of two lists | 599 | 0.529 | Easy | 10,331 |
https://leetcode.com/problems/minimum-index-sum-of-two-lists/discuss/793901/Easy-Python-7-line-solution-using-Dictionary | class Solution(object):
def findRestaurant(self, list1, list2):
letter,min_val,result = {},0,[]
for i in list1:
if i in list2:letter[i]=(int(list1.index(i) + list2.index(i)))
min_val=sorted(letter.values())[0]
for key in letter.keys():
if letter[key] == min_va... | minimum-index-sum-of-two-lists | Easy Python 7 line solution using Dictionary | rachitsxn292 | 0 | 311 | minimum index sum of two lists | 599 | 0.529 | Easy | 10,332 |
https://leetcode.com/problems/minimum-index-sum-of-two-lists/discuss/753843/Simple-Python3-solution-with-lists-for-beginners-no-dictionaries-with-detailed-comments | class Solution:
def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
# get lengths of each list
length_list1 = len(list1)
length_list2 = len(list2)
# initialize list index sum as max of lengths
list_index_sum = length_list1+length_list2... | minimum-index-sum-of-two-lists | Simple Python3 solution with lists for beginners, no dictionaries, with detailed comments | BeforeBeing | 0 | 130 | minimum index sum of two lists | 599 | 0.529 | Easy | 10,333 |
https://leetcode.com/problems/minimum-index-sum-of-two-lists/discuss/556621/Python3-solution-using-a-dictionary | class Solution:
def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
result = []
commons = set(list1).intersection(set(list2))
combos = {}
for item in commons:
combos[item] = list1.index(item)+list2.index(item)
for key, value in combos.items(... | minimum-index-sum-of-two-lists | Python3 solution using a dictionary | altareen | 0 | 86 | minimum index sum of two lists | 599 | 0.529 | Easy | 10,334 |
https://leetcode.com/problems/minimum-index-sum-of-two-lists/discuss/544342/Python-O(-m-%2B-n-)-by-dict.-85%2B-w-Hint | class Solution:
def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
restaurant_idx_dict_1 = { rest : idx for idx, rest in enumerate(list1) }
restaurant_idx_dict_2 = { rest : idx for idx, rest in enumerate(list2) }
# itersection set of dict_1 and dict_... | minimum-index-sum-of-two-lists | Python O( m + n ) by dict. 85%+ [w/ Hint] | brianchiang_tw | 0 | 163 | minimum index sum of two lists | 599 | 0.529 | Easy | 10,335 |
https://leetcode.com/problems/minimum-index-sum-of-two-lists/discuss/341713/Solution-in-Python-3 | class Solution:
def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
D = {j:i for i,j in enumerate(list2)}
L = sorted([[i+D[j],j] for i,j in enumerate(list1) if j in D])
return [r[1] for r in L if r[0] == L[0][0]]
- Python 3
- Junaid Mansuri | minimum-index-sum-of-two-lists | Solution in Python 3 | junaidmansuri | 0 | 458 | minimum index sum of two lists | 599 | 0.529 | Easy | 10,336 |
https://leetcode.com/problems/non-negative-integers-without-consecutive-ones/discuss/1361794/Python3-Official-Solution-Explained-Simply-with-Diagrams | class Solution:
def findIntegers(self, n: int) -> int:
# f stores the fibonacci numbers
f = [1, 2]
for i in range(2, 30):
f.append(f[-1]+f[-2])
# last_seen tells us if there was a one right before.
# If that is the case, we are done then and there!
... | non-negative-integers-without-consecutive-ones | [Python3] Official Solution Explained Simply with Diagrams | chaudhary1337 | 100 | 2,500 | non negative integers without consecutive ones | 600 | 0.39 | Hard | 10,337 |
https://leetcode.com/problems/non-negative-integers-without-consecutive-ones/discuss/1364916/Python3-fibonacci | class Solution:
def findIntegers(self, n: int) -> int:
fib = [1, 2]
for _ in range(30): fib.append(fib[-2] + fib[-1])
ans = prev = 0
for i in range(31, -1, -1):
if n & (1<<i):
ans += fib[i]
if prev: return ans
... | non-negative-integers-without-consecutive-ones | [Python3] fibonacci | ye15 | 2 | 108 | non negative integers without consecutive ones | 600 | 0.39 | Hard | 10,338 |
https://leetcode.com/problems/non-negative-integers-without-consecutive-ones/discuss/2500027/Python3-clever-solution-oror-Easy-to-understand | class Solution:
def findIntegers(self, n: int) -> int:
b=(bin(n).replace("0b",""))
dp=[[[[-1 for i in range(2)] for i in range(2)] for i in range(2)] for i in range(30)]
def fun(i,last,tight,leading_zeros):
if i==len(str(b)):
return 1
if dp[i][tight][l... | non-negative-integers-without-consecutive-ones | ✔️ [Python3] clever solution || Easy to understand | explusar | 0 | 56 | non negative integers without consecutive ones | 600 | 0.39 | Hard | 10,339 |
https://leetcode.com/problems/non-negative-integers-without-consecutive-ones/discuss/1362562/Python-fast-and-easy-to-understand-24-ms-faster-than-98.72-of-Python3 | class Solution:
def findIntegers(self, n: int) -> int:
# fn - count of numbers with size of n in bin representation which satisfy the rule
# for example: fn[3] = 5 => *** = {'000', '001', '010', '100', '101'}
# fn[2] = 3 => ** = {'00', '01', '10'}
# fn[6] = {'10****', '0*****'} = fn[... | non-negative-integers-without-consecutive-ones | Python, fast and easy to understand, 24 ms, faster than 98.72% of Python3 | MihailP | 0 | 128 | non negative integers without consecutive ones | 600 | 0.39 | Hard | 10,340 |
https://leetcode.com/problems/can-place-flowers/discuss/380474/Three-Solutions-in-Python-3-(beats-~100) | class Solution:
def canPlaceFlowers(self, f: List[int], n: int) -> bool:
L, i, c, f = len(f)-2, -2, 0, f + [0]
while i < L:
i += 2
if f[i] == 1: continue
if f[i+1] == 0: c += 1
else: i += 1
return n <= c
class Solution:
def canPlaceFlowers(... | can-place-flowers | Three Solutions in Python 3 (beats ~100%) | junaidmansuri | 7 | 1,800 | can place flowers | 605 | 0.329 | Easy | 10,341 |
https://leetcode.com/problems/can-place-flowers/discuss/960678/Python-Simple-Solution | class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
l=[0]+flowerbed+[0]
for i in range(1,len(l)-1):
if sum(l[i-1:i+2])==0:
l[i]=1
n-=1
return n<=0 | can-place-flowers | Python Simple Solution | lokeshsenthilkumar | 5 | 632 | can place flowers | 605 | 0.329 | Easy | 10,342 |
https://leetcode.com/problems/can-place-flowers/discuss/960678/Python-Simple-Solution | class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
flowerbed.append(0)
l=flowerbed
for i in range(len(l)-1):
if l[i-1]+l[i]+l[i+1]==0:
l[i]=1
n-=1
print(l[-1]+l[0]+l[1])
return n<=0 | can-place-flowers | Python Simple Solution | lokeshsenthilkumar | 5 | 632 | can place flowers | 605 | 0.329 | Easy | 10,343 |
https://leetcode.com/problems/can-place-flowers/discuss/2090885/Python3-Greedy-Algorith-O(n)-oror-O(1) | class Solution:
# O(n) || O(1)
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
f = flowerbed
for i in range(len(f)):
leftOk = i == 0 or f[i-1] == 0
rightOk = i == len(f) -1 or f[i+1] == 0
if leftOk and f[i] == 0 and rightOk:
f[... | can-place-flowers | Python3 Greedy Algorith O(n) || O(1) | arshergon | 3 | 164 | can place flowers | 605 | 0.329 | Easy | 10,344 |
https://leetcode.com/problems/can-place-flowers/discuss/2087809/easy-python-code-with-comments | class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
# Logic is simple at each index we check currnt position, prev and next position
# if it contains "1" continue to next iteratinon
# else increase count
# if n is less than or equal to count , return True
helper=[0]+flow... | can-place-flowers | easy python code with comments | Aniket_liar07 | 3 | 106 | can place flowers | 605 | 0.329 | Easy | 10,345 |
https://leetcode.com/problems/can-place-flowers/discuss/2789560/Python-easy-to-understand-solution | class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
canPlant = 0
for i in range(len(flowerbed)):
if canPlant == n:
return True
if flowerbed[i] == 0:
left = (i == 0 or flowerbed[i-1] == 0)
right = (... | can-place-flowers | Python easy to understand solution | ankurkumarpankaj | 2 | 115 | can place flowers | 605 | 0.329 | Easy | 10,346 |
https://leetcode.com/problems/can-place-flowers/discuss/1218250/Python3Easy-Understanding | class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
i, flw = 1, 0
flowerbed.insert(0, 0)
flowerbed.append(0)
while i <= (len(flowerbed) - 2):
if flowerbed[i-1:i+2] == [0,0,0]:
flw += 1
i += 2
... | can-place-flowers | 【Python3】Easy Understanding | qiaochow | 2 | 184 | can place flowers | 605 | 0.329 | Easy | 10,347 |
https://leetcode.com/problems/can-place-flowers/discuss/2224502/Python-Solution | class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
count=0
if len(flowerbed)>1:
for i in range(0,len(flowerbed)):
if i==0:
if flowerbed[i+1]!=1 and flowerbed[i]!=1:
count+=1
flowerbed[i]=1
elif ... | can-place-flowers | Python Solution | SakshiMore22 | 1 | 94 | can place flowers | 605 | 0.329 | Easy | 10,348 |
https://leetcode.com/problems/can-place-flowers/discuss/2224502/Python-Solution | class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
count = 0
flowerbed = [0] + flowerbed + [0]
for i in range(1, len(flowerbed)-1):
if flowerbed[i-1] == flowerbed[i] == flowerbed[i+1] == 0:
flowerbed[i] = 1
count += 1
return count >= n | can-place-flowers | Python Solution | SakshiMore22 | 1 | 94 | can place flowers | 605 | 0.329 | Easy | 10,349 |
https://leetcode.com/problems/can-place-flowers/discuss/1699396/Python-3-O(n)-or-one-pass | class Solution:
def canPlaceFlowers(self, ar: List[int], n: int) -> bool:
size = len(ar)
ans = 0
for i in range(size):
flag = True
if ar[i] == 1:
continue
flag &= not (i - 1 >= 0 and ar[i - 1] == 1)
flag &= not ... | can-place-flowers | [Python 3] O(n) | one-pass | BrijGwala | 1 | 86 | can place flowers | 605 | 0.329 | Easy | 10,350 |
https://leetcode.com/problems/can-place-flowers/discuss/1592376/Python-3-O(n)-solution-with-comments | class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
count = 0 # # of flowers planted
bed_len = len(flowerbed)
i = 0
while i < bed_len:
if (flowerbed[i] == 0 and
(i == 0 or flowerbed[i-1] == 0) and
(i == bed_le... | can-place-flowers | Python 3 O(n) solution with comments | dereky4 | 1 | 255 | can place flowers | 605 | 0.329 | Easy | 10,351 |
https://leetcode.com/problems/can-place-flowers/discuss/2797943/Python-O(N)-time-and-O(1)-space | class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
count = 0
n1 = len(flowerbed)
if n1==1:
if flowerbed[0]==0:
count+=1
return count>=n
if n1==0:
return count==n
for i in range(n1):
... | can-place-flowers | Python O(N) time and O(1) space | Rajeev_varma008 | 0 | 1 | can place flowers | 605 | 0.329 | Easy | 10,352 |
https://leetcode.com/problems/can-place-flowers/discuss/2737624/Python-My-Soln | class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
if (n == 0):
return True
f = [0] + flowerbed + [0]
for i in range(1,len(f) - 1):
if (f[i-1] == 0 and f[i] == 0 and f[i+1] == 0):
f[i] = 1
n -= 1
if (n == 0):
return True
return False | can-place-flowers | Python My Soln | lucasschnee | 0 | 8 | can place flowers | 605 | 0.329 | Easy | 10,353 |
https://leetcode.com/problems/can-place-flowers/discuss/2380608/Python-Very-easy | class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
flowerbed = [0]+flowerbed+[0]
i=1
l=len(flowerbed)
while i < l-1:
if flowerbed[i-1] == flowerbed[i] == flowerbed[i+1]==0:
flowerbed[i] =1
n-=1
if n... | can-place-flowers | [Python] Very easy | sunakshi132 | 0 | 76 | can place flowers | 605 | 0.329 | Easy | 10,354 |
https://leetcode.com/problems/can-place-flowers/discuss/2107666/Python-Simples-Solution-O(n) | class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
adjacent = False
count = spots = 0
for flower in flowerbed:
if not flower:
count += 1
# if flower
else:
# adjacent = (True) that's -> spots beetween flowers
if adjacent:
if count > 2:
spots += ((coun... | can-place-flowers | Python Simples Solution O(n) | R0bertWell | 0 | 88 | can place flowers | 605 | 0.329 | Easy | 10,355 |
https://leetcode.com/problems/can-place-flowers/discuss/2076974/Can-Place-Flowers-o(n) | class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
count = 0
if len(flowerbed) == 1 and flowerbed[0] == 0:
return True
if flowerbed[0] == 0 and flowerbed[1] == 0:
count+=1
flowerbed[0]=1
if flowerbed[len(flowerbed)-2] ... | can-place-flowers | Can Place Flowers o(n) | somendrashekhar2199 | 0 | 50 | can place flowers | 605 | 0.329 | Easy | 10,356 |
https://leetcode.com/problems/can-place-flowers/discuss/1853062/Python-(Simple-Approach-and-Beginner-friendly) | class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
print(len(flowerbed))
if len(flowerbed) <= 2:
if len(flowerbed) == flowerbed.count(0):
n-=1
for i in range(1, len(flowerbed)-1):
print(i, len(flowerbed))
if fl... | can-place-flowers | Python (Simple Approach and Beginner-friendly) | vishvavariya | 0 | 135 | can place flowers | 605 | 0.329 | Easy | 10,357 |
https://leetcode.com/problems/can-place-flowers/discuss/1700009/Easy-Python3-Solution-(Accepted)(Commented) | class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
i = 0 //Initialise pointer
if len(flowerbed)==1: //Edge case
if flowerbed[0] == 0 and n <=1: ... | can-place-flowers | Easy Python3 Solution (Accepted)(Commented) | sdasstriver9 | 0 | 67 | can place flowers | 605 | 0.329 | Easy | 10,358 |
https://leetcode.com/problems/can-place-flowers/discuss/1699587/Python3-Runtime%3A-140-ms-faster-than-100.00-of-submissions | class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
prev = 0
prev_consumed = False
for slot in flowerbed:
if n < 0:
return True
if not slot and not prev:
n -= 1
prev = 1
prev_... | can-place-flowers | [Python3] Runtime: 140 ms, faster than 100.00% of submissions | geka32 | 0 | 57 | can place flowers | 605 | 0.329 | Easy | 10,359 |
https://leetcode.com/problems/can-place-flowers/discuss/1699576/fast-and-memory-efficient-python3 | class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
i, k = 0, 0
while i < len(flowerbed)-1:
if flowerbed[i] == 1:
i += 2
else:
if flowerbed[i+1] == 1:
i += 3
elif flowerb... | can-place-flowers | fast and memory efficient python3 | fatmakahveci | 0 | 24 | can place flowers | 605 | 0.329 | Easy | 10,360 |
https://leetcode.com/problems/can-place-flowers/discuss/1699224/python-simple-checking-all-by-brute-force | class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
to_plant = n
for i,x in enumerate(flowerbed):
if x == 0:
if i == 0:
if i + 1 < len(flowerbed) and flowerbed[i + 1] == 0:
to_plant -= 1
... | can-place-flowers | python simple checking all by brute force | m-s-dwh-bi | 0 | 19 | can place flowers | 605 | 0.329 | Easy | 10,361 |
https://leetcode.com/problems/can-place-flowers/discuss/1699098/Python-Easy-and-Clean-Solution | class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
if n == 0:
return True
for i in range(len(flowerbed)):
if flowerbed[i] == 0 and (i == 0 or flowerbed[i-1] == 0) and (i == len(flowerbed)-1 or flowerbed[i+1] == 0):
n -= 1
flowerbed[i] = 1
if n == 0:
retur... | can-place-flowers | [Python] Easy and Clean Solution ✔ | leet_satyam | 0 | 46 | can place flowers | 605 | 0.329 | Easy | 10,362 |
https://leetcode.com/problems/can-place-flowers/discuss/1698729/Python-3-EASY-Intuitive-Solution | class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
for i in range(len(flowerbed)):
if flowerbed[i] == 0:
# if 0 at first position, next is also 0
if i == 0 and (i+1 == len(flowerbed) or flowerbed[i+1] != 1):
n -= 1... | can-place-flowers | ✅ [Python 3] EASY Intuitive Solution | JawadNoor | 0 | 12 | can place flowers | 605 | 0.329 | Easy | 10,363 |
https://leetcode.com/problems/can-place-flowers/discuss/1698704/Python-Solution-with-comments | class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
# Special Case1: when number of flowers to plant is 0
if n == 0:
return True
# Special Case2: when length of flowerbed is 1
if len(flowerbed) == 1:
if flowerbed[0] == 0:
... | can-place-flowers | Python Solution with comments | pradeep288 | 0 | 43 | can place flowers | 605 | 0.329 | Easy | 10,364 |
https://leetcode.com/problems/can-place-flowers/discuss/1698677/Easy-Understanding-using-Loop-!!-O(N)-!!-Python | class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
nums=flowerbed
if len(nums)==1:
if nums[0]==1 and n>0:
return False
elif nums[0]==1 and n==0:
return True
elif nums[0]==0 and n==1 or n==0:
return True
elif nums[0]==0 and n>1:
return False
f=n
for i... | can-place-flowers | Easy Understanding using Loop !! O(N) !! Python | ASHOK_KUMAR_MEGHVANSHI | 0 | 25 | can place flowers | 605 | 0.329 | Easy | 10,365 |
https://leetcode.com/problems/can-place-flowers/discuss/1698527/A-bit-long-code-but-very-easy-to-understand | class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
if flowerbed.count(1)==0:
if len(flowerbed)%2==0:
return n<=len(flowerbed)//2
else:
return n<=1+(len(flowerbed)//2)
cz=0
lim=0
for i i... | can-place-flowers | A bit long code but very easy to understand | shreyasjain0912 | 0 | 13 | can place flowers | 605 | 0.329 | Easy | 10,366 |
https://leetcode.com/problems/can-place-flowers/discuss/1460353/Python3-Fast-and-Neat-Solution-Faster-Than-98.40 | class Solution:
def canPlaceFlowers(self, f: List[int], n: int) -> bool:
l = len(f)
if n == 0 or (l == 1 and f[0] == 0):
return True
if l >= 2:
if f[0] == f[1] == 0:
f[0], n = 1, n - 1
if f[-1] == f[-2] == 0:
... | can-place-flowers | Python3 Fast & Neat Solution Faster Than 98.40% | Hejita | 0 | 96 | can place flowers | 605 | 0.329 | Easy | 10,367 |
https://leetcode.com/problems/can-place-flowers/discuss/1455683/Python-Gets-the-job-done | class Solution:
def canPlaceFlowers(self, A: List[int], n: int) -> bool:
# Add padding so that first and last elements can be considered differently
A = [0] + A + [0]
A_str = "".join([str(i) for i in A])
# Split into chunks of "running zeros", e.g, "1,0,1,0,0,1" -> ["0... | can-place-flowers | [Python] Gets the job done | dev-josh | 0 | 163 | can place flowers | 605 | 0.329 | Easy | 10,368 |
https://leetcode.com/problems/can-place-flowers/discuss/1406528/python-Get-the-index-of-1-and-calculate-directly | class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
num = len(flowerbed)
if n > (num - sum(flowerbed)):
return False
flower_index = [-2] # head [0,0,1]
flower_index += [i for i,x in enumerate(flowerbed) if x == 1]
flower_index... | can-place-flowers | [python] Get the index of 1 and calculate directly | 221Baker | 0 | 113 | can place flowers | 605 | 0.329 | Easy | 10,369 |
https://leetcode.com/problems/can-place-flowers/discuss/1083705/My-python-3-solution-that-plants-the-flowers-75-faster-(with-comments) | class Solution:
def canPlaceFlowers(self, flowerbed, n):
amount_planted = 0 # used to track how many we have planted if >= amount wanted you can plant that amount
if len(flowerbed) == 1 and flowerbed[0] == 0: # if len is 1 and its empty pot we can plant
amount_planted += 1
else:
# checks ... | can-place-flowers | My python 3 solution that plants the flowers 75% faster (with comments) | Jamie_2345 | 0 | 168 | can place flowers | 605 | 0.329 | Easy | 10,370 |
https://leetcode.com/problems/can-place-flowers/discuss/975094/Simple-Python-Solution | class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
if n > len(flowerbed):
return False
i = 0
count = 0
while(i < len(flowerbed)):
if flowerbed[i] == 0:
if (i == 0 or flowerbed[i-1] == 0) and (i == len(flowerbe... | can-place-flowers | Simple Python Solution | shruti98932 | 0 | 158 | can place flowers | 605 | 0.329 | Easy | 10,371 |
https://leetcode.com/problems/can-place-flowers/discuss/961177/Python3-linear-scan-O(N) | class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
for i, x in enumerate(flowerbed):
if x == 0 and (i == 0 or flowerbed[i-1] == 0) and (i+1 == len(flowerbed) or flowerbed[i+1] == 0):
n -= 1
flowerbed[i] = 1
return n <= 0 | can-place-flowers | [Python3] linear scan O(N) | ye15 | 0 | 45 | can place flowers | 605 | 0.329 | Easy | 10,372 |
https://leetcode.com/problems/can-place-flowers/discuss/586403/Intuitive-but-cumbersome | class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
# 0) Edge cases
if n == 0:
return True
if len(flowerbed) == 1:
return flowerbed[0] == 0 and n == 1
# 1) Deduct n if 000 found and quick when n == 0
... | can-place-flowers | Intuitive but cumbersome | puremonkey2001 | 0 | 40 | can place flowers | 605 | 0.329 | Easy | 10,373 |
https://leetcode.com/problems/can-place-flowers/discuss/486225/Python3-O(n)-one-pass-solution | class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
is_bound = True
count = 0
ret_num = 0
for ele in flowerbed:
if ele == 0:
count += 1
continue
if is_bound:
ret_num += (... | can-place-flowers | Python3 O(n) one pass solution | alanzhu_xiaozhi | 0 | 98 | can place flowers | 605 | 0.329 | Easy | 10,374 |
https://leetcode.com/problems/can-place-flowers/discuss/452479/Python-different-approach-count-number-of-consecutive-0s-180ms-12mb | class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
if n == 0:
return True
# if it's a 1 element list with 0 and n is 1, this breaks our logic below
if len(flowerbed) == 1 and flowerbed[0] == 0 and n == 1:
return True
... | can-place-flowers | Python different approach, count number of consecutive 0s, 180ms 12mb | prmtr | 0 | 129 | can place flowers | 605 | 0.329 | Easy | 10,375 |
https://leetcode.com/problems/can-place-flowers/discuss/1352170/Simple-Python-Greedy-Solution | class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
if len(flowerbed) == 1:
if flowerbed[0] == 0:
n -= 1
else:
for i in range(len(flowerbed)):
if (i == 0 and flowerbed[i] == 0 and flowerbed[i + 1] == 0) or (i == l... | can-place-flowers | Simple Python Greedy Solution | Kenzjk | -1 | 139 | can place flowers | 605 | 0.329 | Easy | 10,376 |
https://leetcode.com/problems/can-place-flowers/discuss/1064020/Python-or-simple-solution-or-beats-85 | class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
l = None
for i in range(len(flowerbed)):
if n>len(flowerbed) or n==0:
return n==0
if not flowerbed[i] and not l and (i==len(flowerbed)-1 or (i<len(flowerbed)-1 and not flowerbed[... | can-place-flowers | Python | simple solution | beats 85% | SlavaHerasymov | -1 | 358 | can place flowers | 605 | 0.329 | Easy | 10,377 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/1112030/Python-recursive-solution-with-string-builder | class Solution:
def tree2str(self, t: TreeNode) -> str:
sb = [] # init string builder
# helper function to create result
def helper(node: TreeNode) -> None:
if not node:
return
sb.append(str(node.val))
if... | construct-string-from-binary-tree | Python recursive solution with string builder | ignat-s | 15 | 1,200 | construct string from binary tree | 606 | 0.636 | Easy | 10,378 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/1112030/Python-recursive-solution-with-string-builder | class Solution:
def tree2str(self, t: TreeNode) -> str:
sb = [] # init string builder
# helper function to create result
def helper(node: TreeNode) -> None:
if not node:
return
sb.append('(') # add 1st wrappin... | construct-string-from-binary-tree | Python recursive solution with string builder | ignat-s | 15 | 1,200 | construct string from binary tree | 606 | 0.636 | Easy | 10,379 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/2542694/Python-97-Faster-oror-With-Explaination | class Solution:
def tree2str(self, root: Optional[TreeNode]) -> str:
# Initialising string with root.val
string = str(root.val)
# If root has a non-empty left subtree
if root.left:
# we traverse it and wrap everything it returns in ()
string += "(" + self.tre... | construct-string-from-binary-tree | Python [97% Faster] || With Explaination ✅ | Khacker | 8 | 659 | construct string from binary tree | 606 | 0.636 | Easy | 10,380 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/2543375/Easy-to-understand-or-DFS-solution-or-Recursive-solution | class Solution:
def tree2str(self, root: Optional[TreeNode]) -> str:
ans=""
def dfs(root):
nonlocal ans
if root is None:
return
ans+=str(root.val)+"("
dfs(root.left)
ans+=")("
dfs(root.right)
if ans[-... | construct-string-from-binary-tree | Easy to understand | DFS solution | Recursive solution | ankush_A2U8C | 2 | 18 | construct string from binary tree | 606 | 0.636 | Easy | 10,381 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/1136969/Python-3-f-string-simple-recursive | class Solution:
def tree2str(self, t: TreeNode) -> str:
if not t:
return ''
if not t.right and not t.left:
return f'{t.val}'
elif not t.right:
return f'{t.val}({self.tree2str(t.left)})'
return f'{t.val}({self.tree2str(t.left)})({self.tree2s... | construct-string-from-binary-tree | Python 3 f-string simple recursive | Spud_Chiang | 2 | 240 | construct string from binary tree | 606 | 0.636 | Easy | 10,382 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/2545124/Neat-and-Easy-Python-Solution | class Solution:
def tree2str(self, root: Optional[TreeNode]) -> str:
res = ""
def helper(node):
nonlocal res
if not node: return None
res += str(node.val)
if node.left or node.right:
res += "("
helper(node.l... | construct-string-from-binary-tree | Neat and Easy Python Solution | zip_demons | 1 | 27 | construct string from binary tree | 606 | 0.636 | Easy | 10,383 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/2543705/Python-Elegant-and-Short-or-DFS | class Solution:
"""
Time: O(n)
Memory: O(n)
"""
def tree2str(self, root: TreeNode) -> str:
return self._construct(root)
@classmethod
def _construct(cls, tree: Optional[TreeNode]) -> str:
if tree is None:
return ''
return '{}{}{}'.format(
tree.val,
f'({cls._construct(tree.left)})' * (tree.left... | construct-string-from-binary-tree | Python Elegant & Short | DFS | Kyrylo-Ktl | 1 | 49 | construct string from binary tree | 606 | 0.636 | Easy | 10,384 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/2542569/Easy-python-10-line-solution | class Solution:
def tree2str(self, root: Optional[TreeNode]) -> str:
def pre(root):
if root is None:
return ''
x=str(root.val)
lt='('+pre(root.left)+')'
rt='('+pre(root.right)+')'
if lt=='()' and rt=="()":
return x
... | construct-string-from-binary-tree | Easy python 10 line solution | shubham_1307 | 1 | 26 | construct string from binary tree | 606 | 0.636 | Easy | 10,385 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/1509747/Python3-simple-recursive-solution | class Solution:
def __init__(self):
self.s = ""
def tree2str(self, root: TreeNode) -> str:
if root:
self.s += str(root.val)
flag = False
if root.left:
flag = True
self.s += '('
self.tree2str(root.left)
... | construct-string-from-binary-tree | Python3 simple recursive solution | EklavyaJoshi | 1 | 114 | construct string from binary tree | 606 | 0.636 | Easy | 10,386 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/1372063/5-line-recursion-in-Python-3 | class Solution:
def tree2str(self, root: TreeNode) -> str:
if (None, None) == (root.left, root.right):
return str(root.val)
if root.left and root.right:
return f"{root.val}({self.tree2str(root.left)})({self.tree2str(root.right)})"
return f"{root.val}({self.tree2str(ro... | construct-string-from-binary-tree | 5-line recursion in Python 3 | mousun224 | 1 | 113 | construct string from binary tree | 606 | 0.636 | Easy | 10,387 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/623602/Intuitive-approach-by-rules-from-observation-of-the-examples | class Solution:
def tree2str(self, t: TreeNode) -> str:
if t is None:
return ''
bin_str = []
def preorder(node):
if node:
bin_str.append(str(node.val))
if node.left and node.rig... | construct-string-from-binary-tree | Intuitive approach by rules from observation of the examples | puremonkey2001 | 1 | 86 | construct string from binary tree | 606 | 0.636 | Easy | 10,388 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/441122/Python-3-recursive-solution-or-Time-99.38-or-Space-100 | class Solution:
def helper(self, node):
if not node:
return ''
if node.left and node.right:
left = '(' + self.helper(node.left) + ')'
right = '(' + self.helper(node.right) + ')'
elif node.left and not node.right:
left = '(' + self.help... | construct-string-from-binary-tree | Python 3 recursive solution | Time 99.38% | Space 100% | ilee102 | 1 | 201 | construct string from binary tree | 606 | 0.636 | Easy | 10,389 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/2786601/Easy-Python-Solution-with-Recursion | class Solution:
def tree2str(self, root: Optional[TreeNode]) -> str:
res = []
def print(r):
if not r:
return
res.append('(' + str(r.val))
if not r.left and r.right:
res.append('()')
print(r.left)
print(r.rig... | construct-string-from-binary-tree | Easy Python Solution with Recursion | user4358Mz | 0 | 6 | construct string from binary tree | 606 | 0.636 | Easy | 10,390 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/2553475/Python-Solution-or-99-Faster-or-Recursive-Simulation-Based | class Solution:
def tree2str(self, root: Optional[TreeNode]) -> str:
if not root:
return ''
if (root.left or root.right):
left = f'({self.tree2str(root.left)})'
else:
left = ''
if root.right:
right = f'({s... | construct-string-from-binary-tree | Python Solution | 99% Faster | Recursive Simulation Based | Gautam_ProMax | 0 | 21 | construct string from binary tree | 606 | 0.636 | Easy | 10,391 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/2546630/Python3-One-line-solution-) | class OneLineSolution:
def tree2str(self, root: Optional[TreeNode]) -> str:
return "" if not root else str(root.val) \
+ (f"({self.tree2str(root.left)})" if root.left or root.right else '') \
+ (f"({self.tree2str(root.right)})" if root.ri... | construct-string-from-binary-tree | Python3 One line solution ) | malegkin | 0 | 7 | construct string from binary tree | 606 | 0.636 | Easy | 10,392 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/2546440/606.-Construct-String-from-Binary-Tree-(faster-than-94) | class Solution(object):
def tree2str(self, root):
arr = []
self.helper(root, arr)
return ''.join(arr)
def helper(self,root,arr):
if not root:
return
arr.append(str(root.val))
if not root.left and not root.right:
return
arr.append('(... | construct-string-from-binary-tree | 606. Construct String from Binary Tree (faster than 94%) | idon003 | 0 | 6 | construct string from binary tree | 606 | 0.636 | Easy | 10,393 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/2546021/Python-Easy-Peasy-O(N) | class Solution:
def tree2str(self, root: Optional[TreeNode]) -> str:
def traverse(root, ans):
if not root: return ans
if not (root.left or root.right): return ans + str(root.val)
ans += (str(root.val) + '(')
ans = traverse(root.left, ans)
if root.r... | construct-string-from-binary-tree | ✅ Python Easy Peasy O(N) | dhananjay79 | 0 | 11 | construct string from binary tree | 606 | 0.636 | Easy | 10,394 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/2545983/Easy-and-Clear-Python3-Solution | class Solution:
def tree2str(self, root: Optional[TreeNode]) -> str:
def jaw(node:Optional[TreeNode])-> str:
res=str(node.val)
if not node.left and not node.right:
return res
if node.left:
res=res+'('+jaw(node.left)+')'
else:
... | construct-string-from-binary-tree | Easy & Clear Python3 Solution | moazmar | 0 | 6 | construct string from binary tree | 606 | 0.636 | Easy | 10,395 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/2544918/Python3-Clean-and-Concise | class Solution:
def tree2str(self, root: Optional[TreeNode]) -> str:
ans = f'{root.val}'
if root.left and root.right:
ans += f'({self.tree2str(root.left)})({self.tree2str(root.right)})'
elif root.left:
ans += f'({self.tree2str(root.left)})'
elif root.right:
... | construct-string-from-binary-tree | Python3 Clean and Concise | ynnekuw | 0 | 4 | construct string from binary tree | 606 | 0.636 | Easy | 10,396 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/2544083/EASY-PYTHON3-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 | 🔥 EASY PYTHON3 SOLUTION 🔥 | rajukommula | 0 | 11 | construct string from binary tree | 606 | 0.636 | Easy | 10,397 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/2544050/Python-Simple-Python-Solution-Using-DFS-or-Recursion | class Solution:
def tree2str(self, root: Optional[TreeNode]) -> str:
def PreOrder(node):
if node == None:
return ''
if node.left == None and node.right == None:
return str(node.val) + ''
elif node.right == None:
return str(node.val) + '(' + PreOrder(node.left) + ')'
return str(node.val) ... | construct-string-from-binary-tree | [ Python ] ✅✅ Simple Python Solution Using DFS | Recursion 🥳✌👍 | ASHOK_KUMAR_MEGHVANSHI | 0 | 10 | construct string from binary tree | 606 | 0.636 | Easy | 10,398 |
https://leetcode.com/problems/construct-string-from-binary-tree/discuss/2543730/Python3-easy-and-best-solution | class Solution:
def tree2str(self, root: Optional[TreeNode]) -> str:
S=str(root.val)
if root.left:
S+="("+self.tree2str(root.left)+")"
if root.right:
if not root.left:
S+="()"
S+="("+self.tree2str(root.right)+")"
return S | construct-string-from-binary-tree | Python3 easy and best solution | pranjalmishra334 | 0 | 10 | construct string from binary tree | 606 | 0.636 | Easy | 10,399 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.