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>... | 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... | 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)
... | 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"... | 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(a... | 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... | 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>=l... | 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... | 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:
sta... | 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)... | 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:... | 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... | 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()
... | 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:
... | 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]
... | 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] < stac... | 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
... | 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
... | 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
... | 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()
... | 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
... | 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
... | 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':
... | 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]:
... | 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.ap... | 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[... | 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
... | 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... | 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[cu... | 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 Tr... | 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, prev... | 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 fu... | 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
brea... | 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):
k... | 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: ... | 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... | 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]:
... | 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... | 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:
... | 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(... | 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
#... | 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
... | 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 :
... | 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.le... | 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)
... | 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:
ret... | 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)
... | 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(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 ro... | 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.va... | 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 no... | 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))
... | 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.help... | 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)
... | 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, isLeftNod... | 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... | 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)
... | 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)
#c... | 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... | 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
... | 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(s... | 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 no... | 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 n... | 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 n... | 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:
resul... | 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 a... | 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(ro... | 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... | 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... | 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'... | 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)... | 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
... | 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 = cha... | 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 + r... | 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
... | 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
... | 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])
... | 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:
... | 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':... | 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... | 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]]
... | 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]]
... | 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[... | 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 a... | 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
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.