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/remove-k-digits/discuss/1780283/Python3-oror-Using-Stack-oror-faster-solution | class Solution:
def removeKdigits(self, num: str, k: int) -> str:
stack = ["0"]
if len(num)==k:
return "0"
for i in range(len(num)):
while stack[-1] > num[i] and k > 0:
stack.pop()
k=k-1
stack.append(num[i])
while k>0:
stack.pop()
k-=1
while stack[0] == "0" and len(stack)>1:
stack.pop(0)
return "".join(stack) | remove-k-digits | 📍 ✔Python3 || Using Stack || faster solution | Anilchouhan181 | 6 | 513 | remove k digits | 402 | 0.305 | Medium | 7,000 |
https://leetcode.com/problems/remove-k-digits/discuss/1548787/I-bet-you-will-Understand!!!-Peak-and-Valley-Python-3 | class Solution:
# O(N) O(N)
def removeKdigits(self, nums: str, k: int) -> str:
if len(nums)==1: # if only 1 digit we can only get 0
return "0"
stack = []
for num in nums:
# k--> no of deletion
# len(nums)-k --> len of our answer --> len of stack at last
while stack and stack[-1]>num and k>0:
stack.pop()
k-=1
stack.append(num)
# a case like [112] k = 1 no deletion will happen because there is no valley
# so we pop k elements from last
while k>0 and stack:
stack.pop()
k-=1
return ''.join(stack).lstrip("0") or "0" # lstrip is for removal of cases where stack is 000200
# but our ans should be 200 | remove-k-digits | I bet you will Understand!!! Peak and Valley Python 3 | hX_ | 4 | 314 | remove k digits | 402 | 0.305 | Medium | 7,001 |
https://leetcode.com/problems/remove-k-digits/discuss/1015381/Python3-Stack | class Solution:
def removeKdigits(self, num: str, k: int) -> str:
if len(num) == k:
return "0"
if not k:
return num
arr = []
for n in num:
while arr and arr[-1] > n and k:
arr.pop()
k -= 1
arr.append(n)
if len(arr) == 1 and arr[-1] == '0':
arr.pop()
while k:
arr.pop()
k -= 1
return "".join(arr) if arr else "0" | remove-k-digits | Python3 Stack | jlee9077 | 3 | 309 | remove k digits | 402 | 0.305 | Medium | 7,002 |
https://leetcode.com/problems/remove-k-digits/discuss/2400211/Standard-Greedy-Approach | class Solution:
def removeKdigits(self, num: str, k: int) -> str:
stack = []
for n in num:
while(stack and int(stack[-1])>int(n) and k):
k-=1
stack.pop()
stack.append(n)
while(k):
stack.pop()
k-=1
if len(stack)==0:
return "0"
s=''
for i in range(len(stack)):
s+=stack[i]
return str(int(s)) | remove-k-digits | Standard Greedy Approach | Taruncode007 | 1 | 59 | remove k digits | 402 | 0.305 | Medium | 7,003 |
https://leetcode.com/problems/remove-k-digits/discuss/1798698/Easy-Python-Solution | class Solution:
def removeKdigits(self, n: str, x: int) -> str:
stack=[]
if x==len(n):
return '0'
for i in range(len(n)):
while stack and stack[-1]>n[i] and x:
stack.pop()
x-=1
stack.append(n[i])
while stack and x:
x-=1
stack.pop()
ans=''
for i in stack:
if i=='0':
if len(ans)>0:
ans=ans+i
else:
ans+=i
if len(ans)==0:
return '0'
return ans | remove-k-digits | Easy Python Solution | ayush_kushwaha | 1 | 105 | remove k digits | 402 | 0.305 | Medium | 7,004 |
https://leetcode.com/problems/remove-k-digits/discuss/1779828/Python3-or-Easy-to-understand-with-loops-or-With-Explanation | class Solution:
def removeKdigits(self, num: str, k: int) -> str:
li_num=list(num) #converting to list
while(k>0):
i=0
found=0 #flag to check if current is greater than next(0: not greater)
while(i<len(li_num)-1 and k>0):
if int(li_num[i])>int(li_num[i+1]): #if current>next:del current
del li_num[i]
found=1 #(flag set to 1 means greater current is found)
k=k-1 #decrement k
break #iterate from start again
i+=1
if found==0: #if no greater is found eg.1234 (then remove last k elements)
while(k>0):
li_num.pop()
k=k-1
if(len(li_num)==0): #if string is empty
return "0"
ans=int(''.join(li_num)) #joining list elements as int
return str(ans) | remove-k-digits | Python3 | Easy to understand with loops | With Explanation | dhanashreeg368 | 1 | 78 | remove k digits | 402 | 0.305 | Medium | 7,005 |
https://leetcode.com/problems/remove-k-digits/discuss/1630099/Python3-Solution | class Solution:
def removeKdigits(self, num: str, k: int) -> str:
stack = []
for i in num:
while k and stack and stack[-1]>i:
stack.pop()
k-=1
if not stack and i=='0': continue
stack.append(i)
return "0" if not stack or k>=len(stack) else "".join(stack[:len(stack)-k]) | remove-k-digits | Python3 Solution | satyam2001 | 1 | 219 | remove k digits | 402 | 0.305 | Medium | 7,006 |
https://leetcode.com/problems/remove-k-digits/discuss/1578927/WEEB-DOES-PYTHON-MONOTONIC-STACK | class Solution:
def removeKdigits(self, nums: str, k: int) -> str:
stack = []
# implement increasing stack
for i in range(len(nums)):
while stack and stack[-1] > nums[i] and len(nums) - i + len(stack) > len(nums) - k:
stack.pop()
stack.append(nums[i])
if len(stack) > len(nums) - k:
return str(int("".join(stack[:len(nums) - k]))) if len(nums) != k else "0"
return str(int("".join(stack))) | remove-k-digits | WEEB DOES PYTHON MONOTONIC STACK | Skywalker5423 | 1 | 180 | remove k digits | 402 | 0.305 | Medium | 7,007 |
https://leetcode.com/problems/remove-k-digits/discuss/1408405/Python3-oror-Monotonicstack-oror-Easy-Understanding | class Solution:
def removeKdigits(self, num: str, k: int) -> str:
num=[int(i) for i in str(num)]
stack=[]
for val in num:
while stack and stack[-1]>val and k:
stack.pop()
k-=1
stack.append(val)
while stack and k:
stack.pop()
k-=1
val=[str(i) for i in stack]
step=0
for i in val:
if int(i)>0:
break
step+=1
res="".join(val[step:])
if len(val)==0 or len(val)==1 and val[0]=='0' or res=="":
return '0'
return "".join(val[step:]) | remove-k-digits | Python3 || Monotonicstack || Easy Understanding | bug_buster | 1 | 127 | remove k digits | 402 | 0.305 | Medium | 7,008 |
https://leetcode.com/problems/remove-k-digits/discuss/1335089/Python3-Stacks | class Solution:
def removeKdigits(self, nums: str, k: int) -> str:
stack=[]
for i in range(len(nums)):
curr=nums[i]
remain=len(nums)-i
while stack and stack[-1]>curr and len(stack)+remain>len(nums)-k:
stack.pop()
if len(stack)<len(nums)-k:
stack.append(curr)
if stack==[]: #case of empty stack which is zer0
return "0"
for i in range(len(stack)): #for zeros at start at start of stack
if stack[i]!="0":
break
return "".join(stack[i:]) | remove-k-digits | Python3 Stacks | atharva_shirode | 1 | 316 | remove k digits | 402 | 0.305 | Medium | 7,009 |
https://leetcode.com/problems/remove-k-digits/discuss/2802891/Python-Stack-solution | class Solution:
def removeKdigits(self, num: str, k: int) -> str:
stack = []
for n in num:
while stack and k != 0 and int(stack[-1]) > int(n):
k -= 1
stack.pop()
if stack or n != '0':
stack.append(n)
while stack and k:
k -= 1
stack.pop()
return ''.join(stack) if stack else '0' | remove-k-digits | Python Stack solution | KevinJM17 | 0 | 5 | remove k digits | 402 | 0.305 | Medium | 7,010 |
https://leetcode.com/problems/remove-k-digits/discuss/2798898/Easy-to-Understand-Solution-Using-Stack | class Solution:
def removeKdigits(self, num: str, k: int) -> str:
stack = []
stack.append(num[0])
counter = 0
l = 1
if len(num) == k:
return "0"
while l < len(num):
if num[l] >= stack[-1]:
stack.append(num[l])
else:
while stack and counter < k and num[l] < stack[-1]:
stack.pop()
counter += 1
stack.append(num[l])
l += 1
return str(int("".join(stack[:len(num)-k]))) | remove-k-digits | Easy to Understand Solution Using Stack | fikremariam | 0 | 3 | remove k digits | 402 | 0.305 | Medium | 7,011 |
https://leetcode.com/problems/remove-k-digits/discuss/2666071/Python-deque-Easy-O(n)-monotonic-stack | class Solution:
def removeKdigits(self, num: str, k: int) -> str:
s = deque()
last_checked = 0
for i,e in enumerate(num):
while(s and s[-1] > e and k > 0):
k-=1
s.pop()
s.append(e)
while k:
s.pop()
k -= 1
while(s and s[0] == '0'):
s.popleft()
k-=1
return "".join(s) if s else '0' | remove-k-digits | Python deque Easy O(n) monotonic stack | anu1rag | 0 | 2 | remove k digits | 402 | 0.305 | Medium | 7,012 |
https://leetcode.com/problems/remove-k-digits/discuss/2336313/Python-Simple-Solution | class Solution:
def removeKdigits(self, num: str, k: int) -> str:
stack = []
if len(num)==k:
return "0"
for ch in num+'0':
if not stack:
stack.append(ch)
elif ch<stack[-1]:
while stack and k>0 and stack[-1]>ch:
stack.pop()
k-=1
stack.append(ch)
else:
stack.append(ch)
if len(stack)>0:
stack.pop()
return str(int(''.join(stack))) | remove-k-digits | Python Simple Solution | Abhi_009 | 0 | 57 | remove k digits | 402 | 0.305 | Medium | 7,013 |
https://leetcode.com/problems/remove-k-digits/discuss/2333146/Python-monotonic-stack-O(N)O(N) | class Solution:
def removeKdigits(self, num: str, k: int) -> str:
stack = []
for d in num:
while k and stack and stack[-1] > d:
stack.pop()
k -= 1
stack.append(d)
if k:
stack = stack[:-k]
idx = 0
while idx < len(stack) and stack[idx] == '0':
idx += 1
return "".join(stack[idx:]) if idx < len(stack) else '0' | remove-k-digits | Python, monotonic stack O(N)/O(N) | blue_sky5 | 0 | 15 | remove k digits | 402 | 0.305 | Medium | 7,014 |
https://leetcode.com/problems/remove-k-digits/discuss/1905002/Python-easy-understand-solution-with-comment | class Solution:
def removeKdigits(self, num: str, k: int) -> str:
num = list(map(int, num)) # "123" -> [1, 2, 3]
stack = [-1] # Trick to make it a little simpler.
for i in range(len(num)):
while num[i] < stack[-1] and k: # make stack non-decreasing array
stack.pop()
k -= 1
stack.append(num[i])
while k: # when dealing with non-decreasing array, just delete the largest number which is at last idx.
stack.pop()
k -= 1
stack = list(map(str, stack[1:])) # make it int -> str, also delete "-1" at the same time
start = 0
while start < len(stack) and stack[start] == "0": # Avoid leading zeros
start += 1
return "".join(stack[start:]) if start <= len(stack) - 1 else "0" # Avoid "" output | remove-k-digits | Python easy-understand solution with comment | byroncharly3 | 0 | 98 | remove k digits | 402 | 0.305 | Medium | 7,015 |
https://leetcode.com/problems/remove-k-digits/discuss/1781412/Python-solution-with-explanation | class Solution:
def removeKdigits(self, num: str, k: int) -> str:
# the stack that stores the remaining digits
stack = []
# number of digits that have been deleted so far
deleted = 0
# iterate over the whole string
for c in num:
# should not delete any digit any more
if deleted == k or len(stack)==0:
stack.append(c)
continue
# if the incoming digit is smaller than the number in the stack so far,
# we pop out the larger numbers until 1: stack is empty; 2: deleted==k
if c < stack[-1]:
while len(stack)>0 and c <stack[-1]:
stack.pop()
deleted += 1
if deleted == k:
break
stack.append(c)
# if deleted<k, we need to pop out (k-deleted) number of digits out of the stack
res=min(len(stack), max(len(stack)-(k-deleted),0))
# remove leading zeros
ret = ""
for i in range(0,res):
c=stack[i]
if not ret and c=='0':
continue
ret += c
return ret if len(ret)>0 else "0" | remove-k-digits | Python solution with explanation | m15575593150 | 0 | 42 | remove k digits | 402 | 0.305 | Medium | 7,016 |
https://leetcode.com/problems/remove-k-digits/discuss/1780444/Python3-Solution-with-using-stack | class Solution:
def removeKdigits(self, num: str, k: int) -> str:
stack = []
for digit in num:
while stack and k and digit < stack[-1]:
stack.pop()
k -= 1
stack.append(digit)
# trip k remaining elements
if k > 0:
stack = stack[:-k]
# trip leading
begin_idx = 0
for elem in stack:
if elem != "0":
break
begin_idx += 1
return ''.join(stack[begin_idx:]) or '0' # or '0' it is case if in result we have empty seq | remove-k-digits | [Python3] Solution with using stack | maosipov11 | 0 | 20 | remove k digits | 402 | 0.305 | Medium | 7,017 |
https://leetcode.com/problems/remove-k-digits/discuss/1779613/Python-Simple-Python-Solution-By-Deleting-Element-Without-Stack | class Solution:
def removeKdigits(self, num: str, k: int) -> str:
str_num=list(num)
while k>0:
i=0
t=0
while i<(len(str_num)-1) and k>0:
if int(str_num[i])>int(str_num[i+1]):
del str_num[i]
t=1
k=k-1
break
i=i+1
if t==0:
while k>0:
str_num.pop(-1)
k=k-1
if len(str_num)==0:
return "0"
n=int(''.join(str_num))
return str(n) | remove-k-digits | [ Python ] ✔✔ Simple Python Solution By Deleting Element Without Stack 🔥✌ | ASHOK_KUMAR_MEGHVANSHI | 0 | 100 | remove k digits | 402 | 0.305 | Medium | 7,018 |
https://leetcode.com/problems/remove-k-digits/discuss/1762678/Python-or-Monotonic-Stack | class Solution:
def removeKdigits(self, num: str, k: int) -> str:
stk=[]
if len(num)==k:
return "0"
i=0
while i<len(num) and k>0:
n=num[i]
while stk and stk[-1]>n:#Lighter nums can replace the heavier nums from stack
stk.pop()
k-=1
if k==0:
break
stk.append(n)
i+=1
while stk and k>0:#Stack is monotonic increasing here so remove from the last el which is greater iff k>0
stk.pop()
k-=1
if not stk and not num[i:]:
return "0"
ans=''.join(stk)+num[i:]
return str(int(ans)) | remove-k-digits | Python | Monotonic Stack | heckt27 | 0 | 117 | remove k digits | 402 | 0.305 | Medium | 7,019 |
https://leetcode.com/problems/remove-k-digits/discuss/1472414/Solved-without-Stack | class Solution:
def removeKdigits(self, num: str, k: int) -> str:
num = list(map(int,list(num)))
i = 1
while i<len(num) and k>0:
if i > 0 and num[i] < num[i-1]:
del num[i-1]
k -= 1
i -= 1
else:
i += 1
while k>0:
num = num[:-1]
k -= 1
if len(num) == 0:
return "0"
return str(int(''.join(map(str,num)))) | remove-k-digits | Solved without Stack | Tensor08 | 0 | 126 | remove k digits | 402 | 0.305 | Medium | 7,020 |
https://leetcode.com/problems/remove-k-digits/discuss/1462376/PyPy3-Solution-using-monotonic-stack-w-comments | class Solution:
def removeKdigits(self, nums: str, k: int) -> str:
# Base Case
if len(nums) == k:
return "0"
# Init
m = len(nums)
# Build a monotonic stack, by removing
# greater element previous to current
# index
stack = []
for i in range(m):
if stack:
curr = int(nums[i])
while k and stack and int(stack[-1]) > curr:
stack.pop()
k -= 1
stack.append(nums[i])
# Pop remaining k elements,
# as they will be greatest in the
# monotonic stack so created
while k and stack:
stack.pop()
k -= 1
# Join all the element in the stack
# and first convert it to int, to
# remove the leading zeros, and
# then convert to string before
# returning
return str(int("".join(stack)))
"""
def REC(s: int, k: int, n: int):
if n < 0 or k==0 or s=="":
return -1 if s=="" else int(s)
else:
return min(REC(s[:n] + s[n+1:], k-1, n-1), REC(s,k,n-1))
"""
"""
def REC(s: int, k: int, n: int, t: Dict = dict()):
if n < 0 or k==0 or s=="":
return -1 if s=="" else int(s)
else:
key = s
if key not in t:
t[key] = min(REC(s[:n] + s[n+1:], k-1, n-1, t), REC(s,k,n-1,t))
return t[key]
val = REC(nums, k, m-1)
return "" if val == -1 else str(val)
""" | remove-k-digits | [Py/Py3] Solution using monotonic stack w/ comments | ssshukla26 | 0 | 147 | remove k digits | 402 | 0.305 | Medium | 7,021 |
https://leetcode.com/problems/remove-k-digits/discuss/828667/Python3-O(N)-using-stack | class Solution:
def removeKdigits(self, num: str, k: int) -> str:
if k >= len(num):
return '0'
stack = []
for i in num:
while k > 0 and len(stack) and int(stack[-1]) > int(i):
k -= 1
stack.pop()
stack.append(i)
while k > 0:
k -= 1
stack.pop()
while len(stack) and stack[0] == '0':
stack.pop(0)
return ''.join(stack) if len(stack) else '0' | remove-k-digits | Python3 O(N) using stack | himanshu0503 | 0 | 172 | remove k digits | 402 | 0.305 | Medium | 7,022 |
https://leetcode.com/problems/remove-k-digits/discuss/630628/JavaPython3-via-a-stack | class Solution:
def removeKdigits(self, num: str, k: int) -> str:
stack = []
for x in num:
while k and stack and stack[-1] > x:
k -= 1
stack.pop()
stack.append(x)
return "".join(stack[:-k or None]).lstrip("0") or "0" | remove-k-digits | [Java/Python3] via a stack | ye15 | 0 | 67 | remove k digits | 402 | 0.305 | Medium | 7,023 |
https://leetcode.com/problems/remove-k-digits/discuss/361907/Solution-in-Python-3 | class Solution:
def removeKdigits(self, num: str, k: int) -> str:
n, N, j = list(num), [], k
for _ in range(len(num)-k):
i = n.index(min(n[:j+1]))
N.append(n[i])
j -= i
del n[:i+1]
return "".join(N).lstrip("0") or "0"
- Junaid Mansuri
(LeetCode ID)@hotmail.com | remove-k-digits | Solution in Python 3 | junaidmansuri | 0 | 627 | remove k digits | 402 | 0.305 | Medium | 7,024 |
https://leetcode.com/problems/frog-jump/discuss/418003/11-line-DFS-solution | class Solution(object):
def canCross(self, stones):
n = len(stones)
stoneSet = set(stones)
visited = set()
def goFurther(value,units):
if (value+units not in stoneSet) or ((value,units) in visited):
return False
if value+units == stones[n-1]:
return True
visited.add((value,units))
return goFurther(value+units,units) or goFurther(value+units,units-1) or goFurther(value+units,units+1)
return goFurther(stones[0],1) | frog-jump | 11 line DFS solution | nirajmotiani | 20 | 2,700 | frog jump | 403 | 0.432 | Hard | 7,025 |
https://leetcode.com/problems/frog-jump/discuss/2192998/Python3-TOP-BOTTOM-DP-with-BINARY-SEARCH-Explained | class Solution:
def canCross(self, stones: List[int]) -> bool:
L = len(stones)
if stones[1] != 1: return False
@cache
def canCross(stone, k):
if stone == stones[-1]:
return True
jumps = [k, k+1]
if k > 1: jumps.append(k-1)
for jump in jumps:
nextStone = stone + jump
found = bisect_left(stones, nextStone)
if found < L and stones[found] == nextStone and canCross(nextStone, jump):
return True
return False
return canCross(1, 1) | frog-jump | ✔️ [Python3] TOP-BOTTOM DP with BINARY SEARCH, Explained | artod | 4 | 67 | frog jump | 403 | 0.432 | Hard | 7,026 |
https://leetcode.com/problems/frog-jump/discuss/801493/Python3-Recursion-with-Memo.-Beats-87-!!Need-Help!! | class Solution:
def canCross(self, stones: List[int]) -> bool:
# will store valid stone positions
stoneSet = set(stones)
# will store (stone, jumpsize) which will not lead to the last stone
stoneReach = set()
# set target
self.last = stones[-1]
# stoneReach[self.last] = True
def help(curStone, lastJump):
#check if target
if curStone==self.last:
return True
#check if valid stone
if curStone not in stoneSet:
return False
# for all possible jumps
for jump in (lastJump-1, lastJump, lastJump+1):
nextStone = curStone+jump
# if jumpsize>0 and if (stone, jumpsize) already encounterd
# and does not lead to target
if jump and (nextStone, jump) not in stoneReach:
if self.last==nextStone:
return True
else:
if help(nextStone, jump):
return True
# add (stone, jumpsize) if the function returns False
stoneReach.add((nextStone, jump))
return False
# Check if stone at position 1
if 1 not in stoneSet:
return False
return help(1, 1) | frog-jump | Python3, Recursion with Memo. Beats 87% !!Need Help!! | akdagade | 3 | 520 | frog jump | 403 | 0.432 | Hard | 7,027 |
https://leetcode.com/problems/frog-jump/discuss/2199364/python-3-or-memoization | class Solution:
def canCross(self, stones: List[int]) -> bool:
if stones[1] != 1:
return False
lastStone = stones[-1]
stones = set(stones)
@lru_cache(None)
def helper(stone, k):
if stone == lastStone:
return True
return ((k != 1 and stone + k - 1 in stones and helper(stone + k - 1, k - 1)) or
(stone + k in stones and helper(stone + k, k)) or
(stone + k + 1 in stones and helper(stone + k + 1, k + 1)))
return helper(1, 1) | frog-jump | python 3 | memoization | dereky4 | 2 | 136 | frog jump | 403 | 0.432 | Hard | 7,028 |
https://leetcode.com/problems/frog-jump/discuss/1277637/Python3-top-down-dp | class Solution:
def canCross(self, stones: List[int]) -> bool:
if stones[1] != 1: return False
loc = set(stones)
@cache
def fn(x, step):
"""Return True if it is possible to cross river at stones[i]."""
if x == stones[-1]: return True
ans = False
for ss in (step-1, step, step+1):
if 0 < ss and x + ss in loc: ans = ans or fn(x + ss, ss)
return ans
return fn(1, 1) | frog-jump | [Python3] top-down dp | ye15 | 1 | 92 | frog jump | 403 | 0.432 | Hard | 7,029 |
https://leetcode.com/problems/frog-jump/discuss/2718565/Python-Solution-or-90-Faster-Clean-Code-or-Custom-HashSet-Based | class Solution:
def canCross(self, stones: List[int]) -> bool:
store = collections.defaultdict(set)
for stone in stones:
store[stone] = set()
store[stones[0]].add(1)
for i in range(0,len(stones)):
currStone = stones[i]
jumpsTodo = store[currStone]
for jump in jumpsTodo:
nxtPos = currStone + jump
if nxtPos == stones[-1]: # last stone reached
return True
if nxtPos in store:
if jump - 1 > 0: # non zero case required for successful jump
store[nxtPos].add(jump - 1)
store[nxtPos].add(jump)
store[nxtPos].add(jump + 1)
return False | frog-jump | Python Solution | 90% Faster - Clean Code | Custom HashSet Based | Gautam_ProMax | 0 | 19 | frog jump | 403 | 0.432 | Hard | 7,030 |
https://leetcode.com/problems/frog-jump/discuss/2610593/Clean-Fast-Python3-or-Hashmap-and-Cache-or-O(n2) | class Solution:
def canCross(self, stones: List[int]) -> bool:
@cache
def cross(i, jump):
if i == len(stones) - 1:
return True
j = stones[i] + jump - 1
if jump > 1 and j in indices and cross(indices[j], jump - 1):
return True
j += 1
if j in indices and cross(indices[j], jump):
return True
j += 1
if j in indices and cross(indices[j], jump + 1):
return True
return False
indices = {x: i for i, x in enumerate(stones)}
if stones[1] != 1:
return False
return cross(1, 1) | frog-jump | Clean, Fast Python3 | Hashmap & Cache | O(n^2) | ryangrayson | 0 | 40 | frog jump | 403 | 0.432 | Hard | 7,031 |
https://leetcode.com/problems/frog-jump/discuss/2312112/Python3-Simple-Recursive-Solution | class Solution:
def canCross(self, stones: List[int]) -> bool:
@lru_cache(None)
def recur(cur, prevJump):
if cur not in st_set or prevJump==0:
return False
if cur == target:
return True
return recur(cur+prevJump-1, prevJump-1) or recur(cur+prevJump, prevJump) or recur(cur+prevJump+1, prevJump+1)
st_set = set(stones)
target = stones[-1]
return recur(1,1) | frog-jump | [Python3] Simple Recursive Solution | __PiYush__ | 0 | 63 | frog jump | 403 | 0.432 | Hard | 7,032 |
https://leetcode.com/problems/frog-jump/discuss/2156571/Python-DFS-w-Early-Termination.-Iterative-w-Stack-99.67-time-99.50-space | class Solution:
def canCross(self, stones: List[int]) -> bool:
jump_mods = [-1,0,1]
stone_ref = {}
prev = 0
# Make dictionary of the indexes of each stone
for i,stone in enumerate(stones):
stone_ref[stone] = i
# if any stone is further away from the previous one than its index,
# it is unreachable in all scenarios and we can return False early
if stone-prev > i:
return False
prev = stone
stack = [(0,0)]
seen = set()
# DFS
while stack:
jump,stone = stack.pop()
seen.add((jump,stone))
# Return true if we reached our goal
if stone == stones[-1]:
return True
# Go through each of the jump modifiers to see if any of them result in the following conditions:
# 1) There is a stone that matches the current stone's value plus that modified jump value
# 2) The index of that matching stone is greater than the index that the frog is currently on
# 3) We have not visited that stone with the same jump value in the past
for mod in jump_mods:
if stone+jump+mod in stone_ref and stone_ref[stone+jump+mod] > stone_ref[stone]:
if (jump+mod,stone+jump+mod) not in seen:
stack.append((jump+mod,stone+jump+mod))
return False | frog-jump | Python, DFS w/ Early Termination. Iterative w/ Stack - 99.67% time, 99.50% space | sirmxanot | 0 | 80 | frog jump | 403 | 0.432 | Hard | 7,033 |
https://leetcode.com/problems/frog-jump/discuss/1746526/Python-or-DFS | class Solution:
def canCross(self, stones: List[int]) -> bool:
@lru_cache(None)
def dfs(curr,lastjump):
j=curr+1
if lastjump<=0:
return False
while j<len(stones):
if stones[curr]+lastjump==stones[j]:#Reached
break
elif stones[curr]+lastjump>stones[j]:#new stone is too near, checking for further stones
j+=1
else:#new stone is beyond the jump
return False
if j==len(stones)-1:#Reached at the end
return True
elif j>=len(stones):#Reached far from destination
return False
return dfs(j,lastjump) or dfs(j,lastjump-1) or dfs(j,lastjump+1)
return dfs(0,1) | frog-jump | Python | DFS | heckt27 | 0 | 94 | frog jump | 403 | 0.432 | Hard | 7,034 |
https://leetcode.com/problems/frog-jump/discuss/1455637/Python3-or-Explained-Dynamic-Programming-Top-Down-Approach | class Solution:
def canCross(self, stones: List[int]) -> bool:
hset=set(stones)
start,end=stones[0],max(stones)
self.dp={}
if stones[0]+1 in hset:
return self.dfs(stones[1],1,hset,end)
else:
return False
def dfs(self,start,jump,hset,end):
k1,k2,k3=False,False,False
if start==end:
return True
if start>end:
return False
if (start,jump) in self.dp:
return self.dp[(start,jump)]
if jump-1>0 and start+(jump-1) in hset:
k1=self.dfs(start+(jump-1),jump-1,hset,end)
if jump>0 and start+jump in hset:
k2=self.dfs(start+(jump),jump,hset,end)
if jump+1>0 and start+(jump+1) in hset:
k3=self.dfs(start+(jump+1),jump+1,hset,end)
if k1 or k2 or k3:
self.dp[(start,jump)]=True
else:
self.dp[(start,jump)]=False
return self.dp[(start,jump)] | frog-jump | [Python3] | Explained Dynamic Programming Top-Down Approach | swapnilsingh421 | 0 | 176 | frog jump | 403 | 0.432 | Hard | 7,035 |
https://leetcode.com/problems/frog-jump/discuss/1436151/BFS-O(n)-time-and-space-complexities! | class Solution:
def canCross(self, stones: List[int]) -> bool:
visit = set()
stack = collections.deque([(0,0)])
finalDist = stones[-1]
stones = set(stones)
while stack:
node, jump = stack.popleft()
if finalDist == node: return True
# find next place/jump!
for i in range(max(0,jump-1) , jump + 2):
if (node + i) in stones and (node + i, i) not in visit:
stack.append((node + i, i))
visit.add((node + i, i))
return False | frog-jump | BFS O(n) time and space complexities! | abuOmar2 | 0 | 161 | frog jump | 403 | 0.432 | Hard | 7,036 |
https://leetcode.com/problems/frog-jump/discuss/1097524/Python-BFS-%2B-Memorization | class Solution:
def canCross(self, stones: List[int]) -> bool:
units = {n : i for i, n in enumerate(stones)}
# remember if stone 'i' has been visited by jumping 'k' steps from the last stone
visited = set()
if 1 not in units: return False
queue = deque([(1, 1)])
visited.add((1, 1))
while queue:
u, k = queue.popleft()
# return True if has reached to the last stone
if units[u] == len(stones) - 1:
return True
for dk in (-1, 0, 1):
nu = u + k + dk
# move to the next stone with (k+dk) steps
if nu in units and units[nu] > units[u] and (nu, k+dk) not in visited:
visited.add((nu, k+dk))
queue.append((nu, k+dk))
return False | frog-jump | Python BFS + Memorization | pochy | 0 | 91 | frog jump | 403 | 0.432 | Hard | 7,037 |
https://leetcode.com/problems/frog-jump/discuss/1006157/Python-Simple-Solution-or-DFS-w-memo | class Solution(object):
def canCross(self, stones):
def dfs(i, step):
if (i, step) in memo: return False
if i == len(stones)-1: return True
for j in xrange(i+1, len(stones)):
cur = stones[j] - stones[i]
if cur in [step-1, step, step+1]:
if dfs(j, cur): return True
else: memo.add((j, cur))
elif cur > step+1: break
return False
memo = set()
if stones[1] != 1: return False
return dfs(1, 1) | frog-jump | [Python] Simple Solution | DFS w/ memo | ianliuy | 0 | 199 | frog jump | 403 | 0.432 | Hard | 7,038 |
https://leetcode.com/problems/frog-jump/discuss/1390586/Python-3-Super-Easy-Solution | class Solution:
def solve(self,stones,jump,current,d,dp):
if (jump,current) in dp:return dp[(jump,current)]
if current==stones[-1]:return True
if current>stones[-1]:return False
options=(jump-1,jump,jump+1)
for i in options:
if current+i in d and i!=0:
if self.solve(stones,i,current+i,d,dp):
dp[(i,current+i)]=True
return True
else:
dp[(i,current+i)]=False
return False
def canCross(self, stones: List[int]) -> bool:
n=len(stones)
if n>1 and stones[1]!=1:return False
d=dict()
for i in range(n):d[stones[i]]=i
dp=dict()
return self.solve(stones,1,1,d,dp) | frog-jump | Python 3 Super Easy Solution | reaper_27 | -1 | 106 | frog jump | 403 | 0.432 | Hard | 7,039 |
https://leetcode.com/problems/frog-jump/discuss/1255558/Python3-%3A-DP-memoization-Different-Approach | class Solution:
def canCross(self, s: List[int]):
# memory variable
mem = dict()
# k = jump variable; i = current index; s = stones list; mem = memory dictionary
def frog(s, k, i, mem):
# unique key
key = str(s[i]) + ":" + str(k) + ":" + str(i)
if i == len(s)-1:
return True
if key in mem:
return mem[key]
# possible jumps
jumps = [k-1, k, k+1]
result = False
for jump in jumps:
if jump and s[i]+jump in s:
result = frog(s,jump,s.index(s[i]+jump,i),mem)
mem[key] = result
return mem[key]
frog(s, 0, 0, mem)
return True in mem.values() | frog-jump | Python3 : DP, memoization - Different Approach | harshilsoni45 | -1 | 198 | frog jump | 403 | 0.432 | Hard | 7,040 |
https://leetcode.com/problems/sum-of-left-leaves/discuss/1558223/Python-DFSRecursion-Easy-%2B-Intuitive | class Solution:
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
# does this node have a left child which is a leaf?
if root.left and not root.left.left and not root.left.right:
# gotcha
return root.left.val + self.sumOfLeftLeaves(root.right)
# no it does not have a left child or it's not a leaf
else:
# bummer
return self.sumOfLeftLeaves(root.left) + self.sumOfLeftLeaves(root.right) | sum-of-left-leaves | Python DFS/Recursion Easy + Intuitive | aayushisingh1703 | 36 | 2,000 | sum of left leaves | 404 | 0.563 | Easy | 7,041 |
https://leetcode.com/problems/sum-of-left-leaves/discuss/1653813/Python-Simple-iterative-BFS | class Solution:
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
if not root: return 0
queue = collections.deque([root])
res = 0
while queue:
node = queue.popleft()
if node:
# check if the current node has a left child
# and that left child is a leaf, if yes, consider it
if node.left and not node.left.left and not node.left.right:
res += node.left.val
# irrespectively, add the children
# of the current node to continue
# your bfs exploration further
queue.append(node.left)
queue.append(node.right)
return res | sum-of-left-leaves | [Python] Simple iterative BFS | buccatini | 4 | 172 | sum of left leaves | 404 | 0.563 | Easy | 7,042 |
https://leetcode.com/problems/sum-of-left-leaves/discuss/2287383/oror-Easiest-Wayoror-Pythonoror-38ms | class Solution:
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
ans=[0]
def finder(node,ans,check):
if not node:
return
if not node.left and not node.right and check:
# print(node.val)
ans[0]+=node.val
return
finder(node.left,ans,True)
finder(node.right,ans,False)
return
finder(root,ans,False)
return ans[0] | sum-of-left-leaves | ✅|| Easiest Way|| Python|| 38ms | HarshVardhan71 | 2 | 133 | sum of left leaves | 404 | 0.563 | Easy | 7,043 |
https://leetcode.com/problems/sum-of-left-leaves/discuss/1988301/Python-Easy-Solution-or-Faster-than-97-submits | class Solution:
def isLeaf(self, node: Optional[TreeNode]) -> float :
if node :
if not node.left and not node.right :
return 1
return 0
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
res = 0
if root :
if self.isLeaf(root.left) :
res += root.left.val
res += self.sumOfLeftLeaves(root.left)
res += self.sumOfLeftLeaves(root.right)
return res | sum-of-left-leaves | [ Python ] Easy Solution | Faster than 97% submits | crazypuppy | 2 | 164 | sum of left leaves | 404 | 0.563 | Easy | 7,044 |
https://leetcode.com/problems/sum-of-left-leaves/discuss/1660431/Python-Easy-to-Understand-or-Faster-Than-99.59 | class Solution:
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
elif root.left and not root.left.left and not root.left.right:
return root.left.val+self.sumOfLeftLeaves(root.right)
else:
return self.sumOfLeftLeaves(root.left)+self.sumOfLeftLeaves(root.right) | sum-of-left-leaves | [Python] Easy to Understand | Faster Than 99.59% | user9015Y | 2 | 243 | sum of left leaves | 404 | 0.563 | Easy | 7,045 |
https://leetcode.com/problems/sum-of-left-leaves/discuss/1557952/Python-or-Simple-or-O(N)-or-Recursion-or-DFS-or-Simple-Intuitive-Approach | class Solution:
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
res = 0
def dfs(root, flag):
nonlocal res
if root:
if flag and root.left == None and root.right == None:
res += root.val
dfs(root.left, True)
dfs(root.right, False)
dfs(root, False)
return res | sum-of-left-leaves | Python | Simple | O(N) | Recursion | DFS | Simple Intuitive Approach | codingteam225 | 2 | 278 | sum of left leaves | 404 | 0.563 | Easy | 7,046 |
https://leetcode.com/problems/sum-of-left-leaves/discuss/1138127/Python-Recursive | class Solution:
def sumOfLeftLeaves(self, root: TreeNode) -> int:
if not root or not root.left and not root.right:
return 0
def getSum(node, isLeft):
if not node:
return 0
if isLeft and not node.left and not node.right:
return node.val
return getSum(node.left, True) + getSum(node.right, False)
return getSum(root, False) | sum-of-left-leaves | Python Recursive | doubleimpostor | 2 | 351 | sum of left leaves | 404 | 0.563 | Easy | 7,047 |
https://leetcode.com/problems/sum-of-left-leaves/discuss/1044330/Simple-BFS-Python | class Solution:
def sumOfLeftLeaves(self, root: TreeNode) -> int:
if not root:
return 0
que = [root]
res = 0
while que:
for _ in range(len(que)):
node = que.pop(0)
if node.left:
que.append(node.left)
if not node.left.left and not node.left.right:
res += node.left.val
if node.right:
que.append(node.right)
return res | sum-of-left-leaves | Simple BFS Python | Venezsia1573 | 2 | 81 | sum of left leaves | 404 | 0.563 | Easy | 7,048 |
https://leetcode.com/problems/sum-of-left-leaves/discuss/2246504/Python3-simple-DFS | class Solution:
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
def dfs(root,direction):
if not root:
return 0
if not root.left and not root.right and direction == "L":
return root.val
return dfs(root.left, "L") + dfs(root.right, "R")
return dfs(root,"R") | sum-of-left-leaves | 📌 Python3 simple DFS | Dark_wolf_jss | 1 | 21 | sum of left leaves | 404 | 0.563 | Easy | 7,049 |
https://leetcode.com/problems/sum-of-left-leaves/discuss/1917215/python3-DFS | class Solution:
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
sums = 0
def helper(root):
nonlocal sums
if not root:
return
if not root.left:
helper(root.right)
return
if not root.left.left and not root.left.right:
sums += root.left.val
helper(root.left)
helper(root.right)
return
helper(root)
return sums | sum-of-left-leaves | python3 DFS | gulugulugulugulu | 1 | 113 | sum of left leaves | 404 | 0.563 | Easy | 7,050 |
https://leetcode.com/problems/sum-of-left-leaves/discuss/1559517/Python-simple-DFS | class Solution:
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
def dfs(node, isLeft):
if not node:
return
# checks if it was left node and a leaf
if isLeft and not node.left and not node.right:
self.res += node.val
dfs(node.left, True)
dfs(node.right, False)
self.res = 0
dfs(root, False)
return self.res | sum-of-left-leaves | Python simple DFS | abkc1221 | 1 | 70 | sum of left leaves | 404 | 0.563 | Easy | 7,051 |
https://leetcode.com/problems/sum-of-left-leaves/discuss/1559423/Python3-Short-Code-with-Simple-Explanation-oror-3-Lines-oror-LC-Daily-Challenge-Nov4-21 | class Solution:
def sumOfLeftLeaves(self,root: Optional[TreeNode]) -> int:
#If root is None return 0
if not root: return 0
# If the current node has the left tree and if it is a leaf node, then return the sum of it's value and call for right child
if root.left and not root.left.left and not root.left.right:
return root.left.val + self.sumOfLeftLeaves(root.right)
#else we recursively perform the same for left child and right child and return their sum
return self.sumOfLeftLeaves(root.left) + self.sumOfLeftLeaves(root.right) | sum-of-left-leaves | [Python3] Short Code with Simple Explanation || 3 Lines || LC Daily Challenge Nov4 21 | suhana9010 | 1 | 101 | sum of left leaves | 404 | 0.563 | Easy | 7,052 |
https://leetcode.com/problems/sum-of-left-leaves/discuss/1436806/Python-oror-Easy-Solution | class Solution:
def sumOfLeftLeaves(self, root: TreeNode) -> int:
def leaf(root, temp):
if root is None:
return 0
if temp == True and root.left is None and root.right is None:
return root.val
x = leaf(root.left, True)
y = leaf(root.right, False)
return x + y
return leaf(root, False) | sum-of-left-leaves | Python || Easy Solution | naveenrathore | 1 | 123 | sum of left leaves | 404 | 0.563 | Easy | 7,053 |
https://leetcode.com/problems/sum-of-left-leaves/discuss/809558/Python3-iteratively-traversing-the-tree | class Solution:
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
ans = 0
stack = [(root, False)]
while stack:
node, tf = stack.pop()
if not node.left and not node.right and tf: ans += node.val
if node.left: stack.append((node.left, True))
if node.right: stack.append((node.right, False))
return ans | sum-of-left-leaves | [Python3] iteratively traversing the tree | ye15 | 1 | 32 | sum of left leaves | 404 | 0.563 | Easy | 7,054 |
https://leetcode.com/problems/sum-of-left-leaves/discuss/806656/Python3%3A-Recursion-Easy | class Solution:
# Test Cases: [3,9,20,null,10,null,7]\n[]\n[1]\n[1,2,null,3]
def helper(self, root):
if not root: return
if root.left and root.left.left == None and root.left.right == None:
self.lVal += root.left.val
else:
self.helper(root.left)
self.helper(root.right)
def sumOfLeftLeaves(self, root: TreeNode) -> int:
self.lVal = 0
self.helper(root)
return self.lVal
def sumOfLeftLeaves(self, root: TreeNode) -> int:
self.lVal = 0
self.helper(root)
return self.lVal | sum-of-left-leaves | Python3: Recursion Easy | nachiketsd | 1 | 75 | sum of left leaves | 404 | 0.563 | Easy | 7,055 |
https://leetcode.com/problems/sum-of-left-leaves/discuss/2848725/python3 | class Solution:
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
def f(n: Optional[TreeNode], l: bool):
if not n:
return 0
if l and (not n.left) and (not n.right):
return n.val
return f(n.left, True) + f(n.right, False)
return f(root, False) | sum-of-left-leaves | python3 | wduf | 0 | 1 | sum of left leaves | 404 | 0.563 | Easy | 7,056 |
https://leetcode.com/problems/sum-of-left-leaves/discuss/2789325/Python-recursive-intuitive-solution-explained | class Solution:
def sumOfLeftLeaves(self, root: Optional[TreeNode], isLeftNode=False) -> int:
if not(root):
return 0
# If root is a left leaf
if isLeftNode and not(root.left) and not(root.right):
return root.val
return self.sumOfLeftLeaves(root.left, isLeftNode=True) + self.sumOfLeftLeaves(root.right, isLeftNode=False) | sum-of-left-leaves | Python recursive intuitive solution explained | Pirmil | 0 | 1 | sum of left leaves | 404 | 0.563 | Easy | 7,057 |
https://leetcode.com/problems/sum-of-left-leaves/discuss/2579460/Python-Concise-DFS-and-BFS-Commented-No-Globals | class Solution:
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
# return dfs(root, False)
return bfs(root)
def dfs(node, left_child):
# check whether we have a value
if node is None:
return 0
# check whether we are a left child and a leave
if left_child and node.left is None and node.right is None:
return node.val
# go further down
return dfs(node.left, True) + dfs(node.right, False)
def bfs(root):
# make the queue
queue = [(root, False)]
# boolean changes all the time
left = False
# initialize the sum
summed = 0
while queue:
# get the node
node, left = queue.pop(0)
# save the queue length
tmp_len = len(queue)
# insert the children
if node.left:
queue.append((node.left, True))
if node.right:
queue.append((node.right, False))
# check whether we inserted children
if left and tmp_len == len(queue):
summed += node.val
return summed | sum-of-left-leaves | [Python] - Concise DFS and BFS - Commented - No Globals | Lucew | 0 | 36 | sum of left leaves | 404 | 0.563 | Easy | 7,058 |
https://leetcode.com/problems/sum-of-left-leaves/discuss/2203221/Python-Simple-Python-Solution-Using-Recursion-oror-DFS | class Solution:
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
self.result = 0
def SumLeftLeaves(node):
if node == None:
return None
if node.left != None and node.left.left == None and node.left.right == None:
self.result = self.result + node.left.val
SumLeftLeaves(node.left)
SumLeftLeaves(node.right)
return self.result
return SumLeftLeaves(root) | sum-of-left-leaves | [ Python ] ✅✅ Simple Python Solution Using Recursion || DFS🥳✌👍 | ASHOK_KUMAR_MEGHVANSHI | 0 | 54 | sum of left leaves | 404 | 0.563 | Easy | 7,059 |
https://leetcode.com/problems/sum-of-left-leaves/discuss/2200197/Easy-Straight-Forward-with-in-line-comment | class Solution:
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
if not root:
return None
q=deque()
q.append(root)
count=0
value =0 #This will store the value of left child
while len(q)!=0:
for i in range(len(q)):
node = q.popleft()
if node.left:
q.append(node.left)
#check if the left node of this has any left child or right child, if doesn't have any then
#store the value of this left child
if not node.left.left and not node.left.right:
value+=node.left.val
if node.right:
q.append(node.right)
return value | sum-of-left-leaves | Easy, Straight Forward with in-line comment | Taruncode007 | 0 | 29 | sum of left leaves | 404 | 0.563 | Easy | 7,060 |
https://leetcode.com/problems/sum-of-left-leaves/discuss/2093073/Python-or-Recursive-solution | class Solution:
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
return self.getSumOfLeft(root.left, "left") + self.getSumOfLeft(root.right, "right")
def getSumOfLeft(self, root, direction):
if not root:
return 0
if root.left == None and root.right == None and direction == "left":
return root.val
return self.getSumOfLeft(root.left, "left") + self.getSumOfLeft(root.right, "right") | sum-of-left-leaves | Python | Recursive solution | rahulsh31 | 0 | 53 | sum of left leaves | 404 | 0.563 | Easy | 7,061 |
https://leetcode.com/problems/sum-of-left-leaves/discuss/1914944/Python-Recursion-With-Explanation-Easy-To-Understand | class Solution:
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
sm = [0]
def dfs(tree, indicatior = None):
if tree.left is None and tree.right is None:
if indicatior == "left":
sm[0] += tree.val
if tree.left:
dfs(tree.left, "left")
if tree.right:
dfs(tree.right, "right")
dfs(root)
return sm[0] | sum-of-left-leaves | Python Recursion With Explanation, Easy To Understand | Hejita | 0 | 102 | sum of left leaves | 404 | 0.563 | Easy | 7,062 |
https://leetcode.com/problems/sum-of-left-leaves/discuss/1902611/Python3-Solution-with-using-recursive-dfs | class Solution:
def traversal(self, node, is_left):
if not node:
return 0
if not node.left and not node.right:
return node.val if is_left else 0
return self.traversal(node.left, True) + self.traversal(node.right, False)
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
return self.traversal(root, False) | sum-of-left-leaves | [Python3] Solution with using recursive dfs | maosipov11 | 0 | 34 | sum of left leaves | 404 | 0.563 | Easy | 7,063 |
https://leetcode.com/problems/sum-of-left-leaves/discuss/1804777/2-soln-or-using-recursion-and-iterative-in-order-traversal-or-Time-%3A-O(N) | class Solution:
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
stack = []
node = root
sumLeaves = 0
while node or stack:
if node:
stack.append(node)
node = node.left
if node and node.left == None and node.right == None:
sumLeaves += node.val
else:
node = stack.pop()
node = node.right
return sumLeaves | sum-of-left-leaves | 2 soln | using recursion and iterative in-order traversal | Time : O(N) | prankurgupta18 | 0 | 43 | sum of left leaves | 404 | 0.563 | Easy | 7,064 |
https://leetcode.com/problems/sum-of-left-leaves/discuss/1804777/2-soln-or-using-recursion-and-iterative-in-order-traversal-or-Time-%3A-O(N) | class Solution:
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
switch = 0 # use this to determine if the node is left or right
return self.sumLeaveHelper(root, switch)
def sumLeaveHelper(self, node, switch):
sumLeaves = 0
if node == None:
return 0
if switch and node.left == None and node.right == None:
sumLeaves += node.val
return sumLeaves + self.sumLeaveHelper(node.left, 1) + self.sumLeaveHelper(node.right, 0) | sum-of-left-leaves | 2 soln | using recursion and iterative in-order traversal | Time : O(N) | prankurgupta18 | 0 | 43 | sum of left leaves | 404 | 0.563 | Easy | 7,065 |
https://leetcode.com/problems/sum-of-left-leaves/discuss/1722887/My-Python-recursive-way-andand-Kotlin-3-solutions | class Solution:
# Recursive O(n)
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
"""
we need to count only LEAVES nodes
"""
self.count = 0
self.start_traversal(root)
return self.count
def start_traversal(self, node):
if node.left and node.left.left is None and node.left.right is None:
self.count += node.left.val
if node.left:
self.start_traversal(node.left)
if node.right:
self.start_traversal(node.right) | sum-of-left-leaves | My Python recursive way && Kotlin 3 solutions | SleeplessChallenger | 0 | 72 | sum of left leaves | 404 | 0.563 | Easy | 7,066 |
https://leetcode.com/problems/sum-of-left-leaves/discuss/1559657/Python-BFS-Implementation-Faster-than-98 | class Solution:
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
result = 0
q = collections.deque()
q.append(root)
while q:
que = q.popleft()
if que.left:
if que.left.right == None and que.left.left == None:
result += que.left.val
q.append(que.left)
if que.right:
q.append(que.right)
return result | sum-of-left-leaves | Python BFS Implementation - Faster than 98% | sonali1597 | 0 | 25 | sum of left leaves | 404 | 0.563 | Easy | 7,067 |
https://leetcode.com/problems/sum-of-left-leaves/discuss/1559512/Simple-and-Intuitive-oror-Python-oror-DFS-oror-Recursion | class Solution:
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
def dfs(node: TreeNode, is_left: bool) -> int:
# check if root is None
if not node:
return 0
# check if the current node is a leaf or not
if node.left is None and node.right is None:
# return value if it is left child of its parent, 0 otherwise
return node.val if is_left else 0
# if current node isn't a leaf, calculate the recursive results of children
left = dfs(node.left, True)
right = dfs(node.right, False)
return left + right
# start traversal from the root
return dfs(root, False) | sum-of-left-leaves | Simple & Intuitive || Python || DFS || Recursion | BhaveshAchhada | 0 | 20 | sum of left leaves | 404 | 0.563 | Easy | 7,068 |
https://leetcode.com/problems/sum-of-left-leaves/discuss/1559402/Python-easy-recurrsion-(Faster-than50) | class Solution:
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
self.result = 0
def dfs(root):
if root:
if root.left:
if not root.left.left and not root.left.right:
self.result += root.left.val
dfs(root.left)
dfs(root.right)
dfs(root)
return self.result | sum-of-left-leaves | Python easy recurrsion (Faster than50%) | revanthnamburu | 0 | 20 | sum of left leaves | 404 | 0.563 | Easy | 7,069 |
https://leetcode.com/problems/sum-of-left-leaves/discuss/1559029/Python-Iterative-DFS-Timeless98.39-Spaceless98.27-Involved-but-short-and-documented | class Solution:
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
# Store only left children in stack
self.stack = []
# Add left children of rightmost diagonal to stack
self.explore_right_add_left(root)
total = 0
# We have left children
while self.stack:
# Explore the right diagonal from next left child
while self.stack and self.stack[-1] and self.stack[-1].right:
# Explore right children and add all left children on this diagonal to stack
self.explore_right_add_left(self.stack.pop())
# If there is a node still at top of stack, it must be a left child with no direct right child
if self.stack:
if self.stack[-1].left:
# has left child, so add left child to explore
self.stack.append(self.stack.pop().left)
else:
# is a left leaf
total += self.stack.pop().val
return total
def explore_right_add_left(self, node):
""" Explore right children and add left children to stack."""
while node:
if node.left:
self.stack.append(node.left)
node = node.right | sum-of-left-leaves | [Python] Iterative DFS, Time<98.39%, Space<98.27%, Involved, but short and documented | u2agarwal99 | 0 | 21 | sum of left leaves | 404 | 0.563 | Easy | 7,070 |
https://leetcode.com/problems/convert-a-number-to-hexadecimal/discuss/1235709/easy-solution-with-explanation | class Solution:
def toHex(self, num: int) -> str:
hex="0123456789abcdef" #created string for reference
ot="" # created a string variable to store and update output string
if num==0:
return "0"
elif num<0:
num+=2**32
while num:
ot=hex[num%16]+ot # we update the output string with the reminder of num/16 , 16 because we are dealing with hex.
num//=16 # now we are updating num by dividing it by 16 ***// operator used for floor division , means division will be always integer not float.
return ot # then we simply return ot | convert-a-number-to-hexadecimal | easy solution with explanation | souravsingpardeshi | 12 | 756 | convert a number to hexadecimal | 405 | 0.462 | Easy | 7,071 |
https://leetcode.com/problems/convert-a-number-to-hexadecimal/discuss/1842189/4-Lines-Python-Solution-oror-98-Faster-(24ms)-oror-Memory-less-than-76 | class Solution:
def toHex(self, num: int) -> str:
Hex='0123456789abcdef' ; ans=''
if num<0: num+=2**32
while num>0: ans=Hex[num%16]+ans ; num//=16
return ans if ans else '0' | convert-a-number-to-hexadecimal | 4-Lines Python Solution || 98% Faster (24ms) || Memory less than 76% | Taha-C | 2 | 214 | convert a number to hexadecimal | 405 | 0.462 | Easy | 7,072 |
https://leetcode.com/problems/convert-a-number-to-hexadecimal/discuss/1049558/Python-Easy-understanding-solution-94.75-time-93.21-memory | class Solution:
def toHex(self, num: int) -> str:
if num == 0:
return "0"
elif num < 0:
num += 2 ** 32
res = ""
letter = "0123456789abcdef"
while num > 0:
res = letter[num % 16] + res
num //= 16
return res | convert-a-number-to-hexadecimal | [Python] Easy-understanding solution, 94.75% time 93.21% memory | Pandede | 2 | 252 | convert a number to hexadecimal | 405 | 0.462 | Easy | 7,073 |
https://leetcode.com/problems/convert-a-number-to-hexadecimal/discuss/2241800/Python3-One-line | class Solution:
def toHex(self, num: int) -> str:
return "{0:x}".format(num) if num >= 0 else "{0:x}".format(num + 2 ** 32) | convert-a-number-to-hexadecimal | Python3 One line | frolovdmn | 1 | 160 | convert a number to hexadecimal | 405 | 0.462 | Easy | 7,074 |
https://leetcode.com/problems/convert-a-number-to-hexadecimal/discuss/1243222/Python3-simple-solution-using-dictionary | class Solution:
def toHex(self, num: int) -> str:
# d = {}
# for i in range(16):
# if i < 10:
# d[i] = str(i)
# if i == 10:
# d[i] = 'a'
# elif i > 10:
# d[i] = chr(ord(d[i-1])+1)
d = {0:'0',1:'1',2:'2',3:'3',4:'4',5:'5',6:'6',7:'7',8:'8',9:'9',10:'a',11:'b',12:'c',13:'d',14:'e',15:'f'}
if num < 0:
num = abs(num)
num = 2**32 - num
if num == 0:
return '0'
res = ''
while num > 0:
res = d[num%16] + res
num //= 16
return res | convert-a-number-to-hexadecimal | Python3 simple solution using dictionary | EklavyaJoshi | 1 | 177 | convert a number to hexadecimal | 405 | 0.462 | Easy | 7,075 |
https://leetcode.com/problems/convert-a-number-to-hexadecimal/discuss/2801070/Simple-Python3-Solution | class Solution:
def toHex(self, num: int) -> str:
if num < 0:
return hex(num & 2**32-1)[2:]
return hex(num)[2:] | convert-a-number-to-hexadecimal | Simple Python3 Solution | vivekrajyaguru | 0 | 10 | convert a number to hexadecimal | 405 | 0.462 | Easy | 7,076 |
https://leetcode.com/problems/convert-a-number-to-hexadecimal/discuss/2583719/Python-One-Pass-Commented | class Solution:
# make a string to get the hexadecimal symbols
hexed = "0123456789abcdef"
def toHex(self, num: int) -> str:
# make the result
result = []
# if zero
if not num:
return "0"
# if negative (two's compliment)
num = num + 2**32 if num < 0 else num
# make the digits
while num:
num, res = divmod(num, 16)
result.append(self.hexed[res])
return "".join(reversed(result)) | convert-a-number-to-hexadecimal | [Python] - One Pass, Commented | Lucew | 0 | 77 | convert a number to hexadecimal | 405 | 0.462 | Easy | 7,077 |
https://leetcode.com/problems/convert-a-number-to-hexadecimal/discuss/2376723/Convert-a-Number-to-Hexadecimal | class Solution:
def toHex(self, num: int) -> str:
if num >= 0:
x=str(hex(num))
return x[2:]
else:
x=hex(num+2**32)
return x[2:] | convert-a-number-to-hexadecimal | Convert a Number to Hexadecimal | Faraz369 | 0 | 67 | convert a number to hexadecimal | 405 | 0.462 | Easy | 7,078 |
https://leetcode.com/problems/convert-a-number-to-hexadecimal/discuss/2093183/Python-or-Easy-to-understand | class Solution:
def toHex(self, num: int) -> str:
hexMap = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']
out = ""
if num < 0:
num = 2**32 + num
if num == 0:
return "0"
while num:
remainder = num % 16
out = hexMap[remainder] + out
num = num // 16
return out | convert-a-number-to-hexadecimal | Python | Easy to understand | rahulsh31 | 0 | 192 | convert a number to hexadecimal | 405 | 0.462 | Easy | 7,079 |
https://leetcode.com/problems/convert-a-number-to-hexadecimal/discuss/1879702/Python-One-Line-Solution-%3A- | class Solution:
def toHex(self, num: int) -> str:
return hex((num + (1 << 128)) % (1 << 128)).replace('0x','')[-8:] | convert-a-number-to-hexadecimal | Python One Line Solution :- | _191500221 | 0 | 130 | convert a number to hexadecimal | 405 | 0.462 | Easy | 7,080 |
https://leetcode.com/problems/convert-a-number-to-hexadecimal/discuss/1837362/Python-Simple-and-Concise!-Multiple-solutions! | class Solution:
def toHex(self, num: int) -> str:
if num == 0: return "0"
if num < 0: num += 2**32
result = ""
while num:
num, digit = num // 16, num % 16
char = str(digit) if digit < 10 else chr(digit-10 + ord("a"))
result = char + result
return result | convert-a-number-to-hexadecimal | Python - Simple and Concise! Multiple solutions! | domthedeveloper | 0 | 91 | convert a number to hexadecimal | 405 | 0.462 | Easy | 7,081 |
https://leetcode.com/problems/convert-a-number-to-hexadecimal/discuss/1837362/Python-Simple-and-Concise!-Multiple-solutions! | class Solution:
def toHex(self, num: int) -> str:
if num == 0: return "0"
if num < 0: num += 2**32
result = ""
while num:
num, digit = divmod(num,16)
char = str(digit) if digit < 10 else chr(digit-10 + ord("a"))
result = char + result
return result | convert-a-number-to-hexadecimal | Python - Simple and Concise! Multiple solutions! | domthedeveloper | 0 | 91 | convert a number to hexadecimal | 405 | 0.462 | Easy | 7,082 |
https://leetcode.com/problems/convert-a-number-to-hexadecimal/discuss/1837362/Python-Simple-and-Concise!-Multiple-solutions! | class Solution:
def toHex(self, num: int) -> str:
if num == 0: return "0"
if num < 0: num += 2**32
ans, hex = "", "0123456789abcdef"
while num:
ans = hex[num%16] + ans
num //= 16
return ans | convert-a-number-to-hexadecimal | Python - Simple and Concise! Multiple solutions! | domthedeveloper | 0 | 91 | convert a number to hexadecimal | 405 | 0.462 | Easy | 7,083 |
https://leetcode.com/problems/convert-a-number-to-hexadecimal/discuss/1837362/Python-Simple-and-Concise!-Multiple-solutions! | class Solution:
def toHex(self, num: int) -> str:
if num == 0: return "0"
if num < 0: num += 2**32
ans, hex = "", list(map(str,range(10))) + list(map(chr,range(ord("a"),ord("f")+1)))
while num:
ans = hex[num%16] + ans
num //= 16
return ans | convert-a-number-to-hexadecimal | Python - Simple and Concise! Multiple solutions! | domthedeveloper | 0 | 91 | convert a number to hexadecimal | 405 | 0.462 | Easy | 7,084 |
https://leetcode.com/problems/convert-a-number-to-hexadecimal/discuss/1837362/Python-Simple-and-Concise!-Multiple-solutions! | class Solution:
def toHex(self, num: int) -> str:
if num == 0: return "0"
if num < 0: num += 2**32
ans, hex = deque(), list(map(str,range(10))) + list(map(chr,range(ord("a"),ord("f")+1)))
while num:
ans.appendleft(hex[num%16])
num //= 16
return reduce(add, ans) | convert-a-number-to-hexadecimal | Python - Simple and Concise! Multiple solutions! | domthedeveloper | 0 | 91 | convert a number to hexadecimal | 405 | 0.462 | Easy | 7,085 |
https://leetcode.com/problems/convert-a-number-to-hexadecimal/discuss/1439946/Python3-Nice-Idea-Memory-Less-Than-98.44 | class Solution:
def toHex(self, n: int) -> str:
if n == 0:
return '0'
v, r = "", ['a', 'b', 'c', 'd', 'e', 'f']
if n < 0:
n += 2 ** 32
while n > 0:
rr = n % 16
if rr > 9:
v += str(r[rr - 10])
else:
v += (str(rr))
n >>= 4
return v[::-1] | convert-a-number-to-hexadecimal | Python3 Nice Idea, Memory Less Than 98.44% | Hejita | 0 | 256 | convert a number to hexadecimal | 405 | 0.462 | Easy | 7,086 |
https://leetcode.com/problems/convert-a-number-to-hexadecimal/discuss/1426591/python3-32m | class Solution:
def toHex(self, num: int) -> str:
hex_map = {
10:'a',
11:'b',
12:'c',
13:'d',
14:'e',
15:'f',
}
output = []
if num>0:
while num != 0:
if num >= 16:
output.append(self.toHex(num//16))
num -= (num//16)*16
if num == 0:
output.append(str(0))
elif num >= 10:
output.append(hex_map[num])
num-=num
else:
output.append(str(num))
num-=num
elif num==0: return '0'
else: return self.toHex(2**32+num)
return "".join(output) | convert-a-number-to-hexadecimal | python3 32m | jam411 | 0 | 120 | convert a number to hexadecimal | 405 | 0.462 | Easy | 7,087 |
https://leetcode.com/problems/convert-a-number-to-hexadecimal/discuss/993718/Faster-than-99.58-using-dictionary | class Solution:
def toHex(self, num: int) -> str:
if num==0:
return '0'
d={10:'a',11:'b',12:'c',13:'d',14:'e',15:'f'}
d1={'0000':0,'0001':1,'0010':2,'0011':3,'0100':4,'0101':5,'0110':6,'0111':7,'1000':8,'1001':9,'1010':'a','1011':'b','1100':'c','1101':'d','1110':'e','1111':'f'}
s,s1="",""
if num>0:
while num!=0:
r=num%16
if r in d:
s+=str(d[r])
else:
s+=str(r)
num//=16
else:
num1=-num
carry=1
l1=list(bin(num1)[2:].zfill(32))
for i in range(len(l1)):
if l1[i]=="1":
l1[i]="0"
else:
l1[i]="1"
for i in range(len(l1)):
if l1[-i-1]!='1':
l1[-i-1]=str(int(l1[-i-1])+carry)
carry=0
elif carry==1 and l1[-i-1]=='1':
l1[-i-1]='0'
carry=1
if carry==1:
l1.insert(0,'1')
s="".join(l1)
for i in range(0,len(s),4):
s1+=str(d1[s[i:i+4]])
return s[::-1] if num>=0 else s1 | convert-a-number-to-hexadecimal | Faster than 99.58%, using dictionary | thisisakshat | 0 | 127 | convert a number to hexadecimal | 405 | 0.462 | Easy | 7,088 |
https://leetcode.com/problems/convert-a-number-to-hexadecimal/discuss/489180/Python-without-using-builtin-hex | class Solution:
def toHex(self, num: int) -> str:
bits = f'{num&0xffffffff:>032b}'
ciphers = [str(i) for i in range(10)] + [chr(i+97) for i in range(6)]
ans = ''.join([ciphers[int(bits[i*4:(i+1)*4],2)] for i in range(8)]).lstrip('0')
return ans or '0' | convert-a-number-to-hexadecimal | Python without using builtin hex | wingsoflight2003 | 0 | 137 | convert a number to hexadecimal | 405 | 0.462 | Easy | 7,089 |
https://leetcode.com/problems/convert-a-number-to-hexadecimal/discuss/458642/Stupid-converting-using-dict(99.77100) | class Solution:
def toHex(self, num: int) -> str:
if num == 0:
return '0'
strs = []
if num < 0:
num += pow(2,32)
dicts = {0:'0',1:'1',2:'2',3:'3',4:'4',5:'5',6:'6',7:'7',8:'8',9:'9',10:'a',11:'b',12:'c',13:'d',14:'e',15:'f'}
while num:
strs.append(dicts[num%16])
num //= 16
return ''.join(strs[::-1]) | convert-a-number-to-hexadecimal | Stupid converting using dict(99.77%/100%) | SilverCHN | 0 | 140 | convert a number to hexadecimal | 405 | 0.462 | Easy | 7,090 |
https://leetcode.com/problems/convert-a-number-to-hexadecimal/discuss/407661/Python-one-liner-using-STL | class Solution:
def toHex(self, num: int) -> str:
return hex(num & (2**32-1))[2:] | convert-a-number-to-hexadecimal | Python one liner using STL | saffi | 0 | 292 | convert a number to hexadecimal | 405 | 0.462 | Easy | 7,091 |
https://leetcode.com/problems/queue-reconstruction-by-height/discuss/2211602/Python-Easy-Greedy-O(1)-Space-approach | class Solution:
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
output=[]
# sort the array in decreasing order of height
# within the same height group, you would sort it in increasing order of k
# eg: Input : [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
# after sorting: [[7,0],[7,1],[6,1],[5,0],[5,2],[4,4]]
people.sort(key=lambda x: (-x[0], x[1]))
for a in people:
# Now let's start the greedy here
# We insert the entry in the output array based on the k value
# k will act as a position within the array
output.insert(a[1], a)
return output | queue-reconstruction-by-height | Python Easy Greedy O(1) Space approach | constantine786 | 79 | 3,300 | queue reconstruction by height | 406 | 0.728 | Medium | 7,092 |
https://leetcode.com/problems/queue-reconstruction-by-height/discuss/2211754/Python-O(NlogN)-Solution-using-SortedList | class Solution:
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
from sortedcontainers import SortedList
n = len(people)
people.sort()
ans = [None] * n
ans[people[0][1]] = people[0]
sl = SortedList(range(n))
toRemove = [people[0][1]]
for i in range(1, n):
if people[i][0] != people[i - 1][0]:
for index in toRemove:
sl.remove(index)
toRemove = []
ans[sl[people[i][1]]] = people[i]
toRemove.append(sl[people[i][1]])
return ans | queue-reconstruction-by-height | [Python] O(NlogN) Solution using SortedList | xil899 | 3 | 182 | queue reconstruction by height | 406 | 0.728 | Medium | 7,093 |
https://leetcode.com/problems/queue-reconstruction-by-height/discuss/2212916/Python-96-Fast-and-93-space-efficient-solution | class Solution:
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
people.sort(key = lambda x: (-x[0], x[1]))
queue = []
for p in people:
queue.insert(p[1], p)
return queue | queue-reconstruction-by-height | Python 96% Fast and 93% space efficient solution | anuvabtest | 2 | 171 | queue reconstruction by height | 406 | 0.728 | Medium | 7,094 |
https://leetcode.com/problems/queue-reconstruction-by-height/discuss/2212916/Python-96-Fast-and-93-space-efficient-solution | class Solution:
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
ans = []
for p in sorted(people, key=lambda p: (-1 * p[0], p[1])):
ans.insert(p[1], p)
return ans | queue-reconstruction-by-height | Python 96% Fast and 93% space efficient solution | anuvabtest | 2 | 171 | queue reconstruction by height | 406 | 0.728 | Medium | 7,095 |
https://leetcode.com/problems/queue-reconstruction-by-height/discuss/1170683/Python-Greedy-Algo-Self-Explanatory | class Solution:
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
people.sort(key = lambda x: [-x[0], x[1]])
ans = []
for h, k in people:
ans = ans[:k] + [[h, k]] + ans[k:]
return ans | queue-reconstruction-by-height | Python - Greedy Algo [Self Explanatory] | leeik | 2 | 224 | queue reconstruction by height | 406 | 0.728 | Medium | 7,096 |
https://leetcode.com/problems/queue-reconstruction-by-height/discuss/2211883/PYTHON3-or-EXPLAINED-or-easy-to-understand-or-sort | class Solution:
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
# sort height in decreasing and rest in increasing order
# as person with greater height is likely to have less # of person greater than them
people = sorted(people, key = lambda x: (-x[0], x[1]))
# print(people)
# initialise queue
queue = []
# iterate throughtout the modified people array
for p in people:
# inserting person w.r.t their height and required pos as pos is already given
queue.insert(p[1], p)
# return the modified queue
return queue | queue-reconstruction-by-height | PYTHON3 | EXPLAINED | easy to understand | sort | H-R-S | 1 | 34 | queue reconstruction by height | 406 | 0.728 | Medium | 7,097 |
https://leetcode.com/problems/queue-reconstruction-by-height/discuss/673745/Python3-two-approaches%3A-shortest-to-tallest-and-tallest-to-shortest | class Solution:
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
people.sort(key=lambda x: (-x[0], x[1])) #tallest -> shortest
ans = []
for p in people:
ans.insert(p[1], p)
return ans | queue-reconstruction-by-height | [Python3] two approaches: shortest to tallest & tallest to shortest | ye15 | 1 | 66 | queue reconstruction by height | 406 | 0.728 | Medium | 7,098 |
https://leetcode.com/problems/queue-reconstruction-by-height/discuss/673745/Python3-two-approaches%3A-shortest-to-tallest-and-tallest-to-shortest | class Solution:
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
people.sort(key=lambda x: (x[0], -x[1])) #shortest to tallest
ans = [None]*len(people)
idx = list(range(len(people)))
for p in people:
ans[idx.pop(p[1])] = p
return ans | queue-reconstruction-by-height | [Python3] two approaches: shortest to tallest & tallest to shortest | ye15 | 1 | 66 | queue reconstruction by height | 406 | 0.728 | Medium | 7,099 |
Subsets and Splits