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/univalued-binary-tree/discuss/1373585/1-line-recursion-in-Python | class Solution:
def isUnivalTree(self, root: TreeNode) -> bool:
return all(c.val == root.val and self.isUnivalTree(c) for c in (root.left, root.right) if c) | univalued-binary-tree | 1-line recursion in Python | mousun224 | 0 | 60 | univalued binary tree | 965 | 0.693 | Easy | 15,600 |
https://leetcode.com/problems/univalued-binary-tree/discuss/1070218/Elegant-Python-DFS-and-Recursion-Solutions | class Solution:
def isUnivalTree(self, root: TreeNode) -> bool:
if not root:
return
val = root.val
self.flag = True
def dfs(node):
if node:
if node.val != val:
self.flag = False
return
else:
... | univalued-binary-tree | Elegant Python DFS & Recursion Solutions | 111989 | 0 | 57 | univalued binary tree | 965 | 0.693 | Easy | 15,601 |
https://leetcode.com/problems/univalued-binary-tree/discuss/1070218/Elegant-Python-DFS-and-Recursion-Solutions | class Solution:
def isUnivalTree(self, root: TreeNode) -> bool:
if not root:
return
def isValidTree(node, val = root.val):
if not node:
return True
if node.val != val:
return False
return isValidTree(node.left, val) and ... | univalued-binary-tree | Elegant Python DFS & Recursion Solutions | 111989 | 0 | 57 | univalued binary tree | 965 | 0.693 | Easy | 15,602 |
https://leetcode.com/problems/univalued-binary-tree/discuss/1011884/BFS-iterative | class Solution:
def isUnivalTree(self, root: TreeNode) -> bool:
queue = [root]
while len(queue) > 0:
current = queue.pop(0)
if current.val != root.val: return False
if current.left: queue.append(current.left)
if current.right: queue.append(cur... | univalued-binary-tree | BFS iterative | borodayev | 0 | 29 | univalued binary tree | 965 | 0.693 | Easy | 15,603 |
https://leetcode.com/problems/univalued-binary-tree/discuss/621076/Python-3.-Univalued-Binary-Tree.-Very-Easy.-beats-95 | class Solution:
def isUnivalTree(self, root: TreeNode) -> bool:
# Traverse through the tree, store and use set.
res = []
def traverse(root):
if not root:
return None
traverse(root.left)
res.append(root.val)
tr... | univalued-binary-tree | [Python 3]. Univalued Binary Tree. Very-Easy. beats 95% | tilak_ | 0 | 38 | univalued binary tree | 965 | 0.693 | Easy | 15,604 |
https://leetcode.com/problems/univalued-binary-tree/discuss/600507/DFS-Python.-3 | class Solution:
def isUnivalTree(self, root: TreeNode) -> bool:
if root is None:
return root
c = self.dfs(root, None)
return c
def dfs(self, root, prev):
if root:
if prev is not None and root.val != prev:
return False
else:
... | univalued-binary-tree | DFS Python. 3 | yadavalli | 0 | 34 | univalued binary tree | 965 | 0.693 | Easy | 15,605 |
https://leetcode.com/problems/univalued-binary-tree/discuss/521907/Python-or-98-Faster-or-100-less-memory | class Solution:
def Util(self, node, value):
if not node:
return True
if node.val != value:
return False
return self.Util(node.left, value) and self.Util(node.right, value)
def isUnivalTree(self, root: TreeNode) -> bool:
... | univalued-binary-tree | Python | 98% Faster | 100% less memory | Adeel_Syed | 0 | 55 | univalued binary tree | 965 | 0.693 | Easy | 15,606 |
https://leetcode.com/problems/univalued-binary-tree/discuss/472005/Why-this-code-gives-wrong-answer | class Solution:
def isUnivalTree(self, root: TreeNode) -> bool:
if root is None:
return True
self.cons=root.val
def dfs(node):
if node:
if node.val!=self.cons:
return False
dfs(node.left)
dfs(node.rig... | univalued-binary-tree | Why this code gives wrong answer? | Golnoush123 | 0 | 32 | univalued binary tree | 965 | 0.693 | Easy | 15,607 |
https://leetcode.com/problems/vowel-spellchecker/discuss/1121773/Python-One-Case-At-A-Time | class Solution:
def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:
# Convert words and vowels to sets for O(1) lookup times
words = set(wordlist)
vowels = set('aeiouAEIOU')
# Create two maps.
# One for case insensitive word to al... | vowel-spellchecker | [Python] One Case At A Time | rowe1227 | 5 | 288 | vowel spellchecker | 966 | 0.514 | Medium | 15,608 |
https://leetcode.com/problems/vowel-spellchecker/discuss/981316/Python3-3-hash-tables | class Solution:
def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:
orig = set(wordlist) # original words O(1) lookup
case = {} # diff in case
vowel = {} # diff in vowel
for word in wordlist:
key = word.lower()
case.setdefa... | vowel-spellchecker | [Python3] 3 hash tables | ye15 | 1 | 142 | vowel spellchecker | 966 | 0.514 | Medium | 15,609 |
https://leetcode.com/problems/vowel-spellchecker/discuss/838793/Python-3-or-Hash-Table-%2B-Wild-card-or-Explanations | class Solution:
def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:
low_origin, wild_origin = collections.defaultdict(str), collections.defaultdict(str)
s = set(wordlist)
def to_wild(word): return ''.join(['*' if c in 'aeiou' else c for c in word])
... | vowel-spellchecker | Python 3 | Hash Table + Wild card | Explanations | idontknoooo | 1 | 193 | vowel spellchecker | 966 | 0.514 | Medium | 15,610 |
https://leetcode.com/problems/vowel-spellchecker/discuss/2814626/Python3-Readable-and-Commented-Hashmap-Solution | class Solution:
# a static vowel set so we don't initialize it with every quer
vowel_set = set(('a', 'e', 'i', 'o', 'u'))
def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:
# keep a set of all variations in their original form for fast lookup
wordset = set(w... | vowel-spellchecker | [Python3] - Readable and Commented Hashmap Solution | Lucew | 0 | 1 | vowel spellchecker | 966 | 0.514 | Medium | 15,611 |
https://leetcode.com/problems/vowel-spellchecker/discuss/1925880/PYTHON-SOL-oror-SIMPLE-oror-EASY-CODE-oror-HASHTABLE-oror | class Solution:
def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:
n = len(wordlist)
d = {}
sd = {}
vd = {}
cd = {}
for i in range(n):
d[wordlist[i]] = i
s = wordlist[i].lower()
if s not in sd:sd[s] = i
... | vowel-spellchecker | PYTHON SOL || SIMPLE || EASY CODE || HASHTABLE || | reaper_27 | 0 | 63 | vowel spellchecker | 966 | 0.514 | Medium | 15,612 |
https://leetcode.com/problems/vowel-spellchecker/discuss/1420676/Dictionary-of-words-100-speed | class Solution:
def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:
set_words = set(wordlist)
dict_words = dict()
for w in wordlist:
w_lower = w.lower()
w_key = (w_lower.replace("a", "_").replace("e", "_")
.replace("i", "... | vowel-spellchecker | Dictionary of words, 100% speed | EvgenySH | 0 | 89 | vowel spellchecker | 966 | 0.514 | Medium | 15,613 |
https://leetcode.com/problems/vowel-spellchecker/discuss/1132749/Easy-python-solution! | class Solution:
def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:
l=len(queries)
lk=len(wordlist)
l1=[]
lw=[]
l3=['a','e','i','o','u']
l4=[]
for e in range(lk):
l6=len(wordlist[e])
s9=""
for c in ... | vowel-spellchecker | Easy python solution! | Rajashekar_Booreddy | 0 | 146 | vowel spellchecker | 966 | 0.514 | Medium | 15,614 |
https://leetcode.com/problems/vowel-spellchecker/discuss/1122370/Python-hash-map-easy-to-understand | class Solution:
vowels = ["a", "e", "i", "o", "u"]
def make_key(self, word):
key = ""
for index in range(len(word)):
if word[index].lower() not in self.vowels:
key += str(index) + word[index].lower()
return key
def spellchecker(self, wor... | vowel-spellchecker | Python hash map easy to understand | dlog | 0 | 66 | vowel spellchecker | 966 | 0.514 | Medium | 15,615 |
https://leetcode.com/problems/vowel-spellchecker/discuss/715753/Python3-use-a-set-and-a-dict-Vowel-Spellchecker | class Solution:
def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:
def replaceVowel(s):
return re.sub(r'[aeiou]', '_', s.lower())
d = {}
s = set()
for w in wordlist:
s.add(w)
if (low := w.lower()) not in d:
... | vowel-spellchecker | Python3 use a set and a dict - Vowel Spellchecker | r0bertz | 0 | 132 | vowel spellchecker | 966 | 0.514 | Medium | 15,616 |
https://leetcode.com/problems/vowel-spellchecker/discuss/527434/Python3-simple-solution | class Solution:
def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:
original_hash, lowercase_hash, ignore_vowel_hash = {}, {}, {}
for index, word in enumerate(wordlist):
original_hash[word] = index
lowercase_word = word.lower()
if lowerca... | vowel-spellchecker | Python3 simple solution | tjucoder | 0 | 216 | vowel spellchecker | 966 | 0.514 | Medium | 15,617 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/2521416/44ms-PYTHON-91-Faster-93-Memory-Efficient-Solution-MULTIPLE-APPROACHES | class Solution:
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
graph = defaultdict(list)
for i in range(0, 10):
if i-k >= 0:
graph[i].append(i-k)
if i +k < 10:
graph[i].append(i+k)
start = [i for i in graph if i!= 0]
for j in range(n-1):
new = set()
for i in start:
last = i%... | numbers-with-same-consecutive-differences | 🔥44ms PYTHON 91% Faster 93% Memory Efficient Solution MULTIPLE APPROACHES 🔥 | anuvabtest | 5 | 421 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,618 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/2521416/44ms-PYTHON-91-Faster-93-Memory-Efficient-Solution-MULTIPLE-APPROACHES | class Solution:
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
numset = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for i in range(n - 1):
res = []
for num in numset:
cur = num % 10
if cur + k <= 9: res.append(num * 10 + cur + k)
if k != 0 and cur - k >= 0: res.append(num * 10 + cur - k)
... | numbers-with-same-consecutive-differences | 🔥44ms PYTHON 91% Faster 93% Memory Efficient Solution MULTIPLE APPROACHES 🔥 | anuvabtest | 5 | 421 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,619 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/2521416/44ms-PYTHON-91-Faster-93-Memory-Efficient-Solution-MULTIPLE-APPROACHES | class Solution:
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
res = []
stack = deque((1, num) for num in range(1, 10))
while stack:
curr_pos, curr_num = stack.pop()
if curr_pos == n:
res.append(curr_num)
else:
last_digit = curr_num % 10
next_pos = curr_pos + 1
can... | numbers-with-same-consecutive-differences | 🔥44ms PYTHON 91% Faster 93% Memory Efficient Solution MULTIPLE APPROACHES 🔥 | anuvabtest | 5 | 421 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,620 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/2524256/Python-Elegant-and-Short-or-DFS-%2B-BFS | class Solution:
"""
Time: O(2^n)
Memory: O(2^n)
"""
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
nums = list(range(1, 10))
for i in range(1, n):
nums = [num * 10 + d for num in nums for d in {num % 10 + k, num % 10 - k} if 0 <= d <= 9]
return nums | numbers-with-same-consecutive-differences | Python Elegant & Short | DFS + BFS | Kyrylo-Ktl | 2 | 48 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,621 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/2526418/Python-backtracking | class Solution:
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
def bt(d, num, i):
num = num * 10 + d
if i == n:
result.append(num)
return
if d + k < 10:
bt(d + k, num, i + 1... | numbers-with-same-consecutive-differences | Python, backtracking | blue_sky5 | 1 | 12 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,622 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/2522025/python3-or-easy-understanding-or-explained-with-comments-or-DFS | class Solution:
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
self.ans = {}
for i in range(1, 10): self.dfs(i, str(i), n, k) # dfs for numbers starting from i
return self.ans.keys()
def dfs(self, num, s, n, k): ... | numbers-with-same-consecutive-differences | python3 | easy-understanding | explained with comments | DFS | H-R-S | 1 | 28 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,623 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/2521368/Clean-Python-Backtracking-solution | class Solution:
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
res = []
def backtrack(tempRes):
nonlocal res
if len(tempRes) == n:
s = [str(i) for i in tempRes]
res.append("".join(s))
return... | numbers-with-same-consecutive-differences | Clean Python Backtracking solution | RayML | 1 | 108 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,624 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/1752951/Python-3-DFS-Super-Simple-Code-beats-99-Time-and-98-Space-(Hours-of-Optimization). | class Solution:
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
l=[]
def dfs(i,n1):
j=i%10
if(n1==n):
l.append(i)
return
q1=j+k
q2=j-k
i=i*10
if(q1<10 and q2>=0 and k!=0 ):
... | numbers-with-same-consecutive-differences | [Python 3] DFS Super Simple Code beats 99% Time and 98% Space (Hours of Optimization). | vedank98 | 1 | 100 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,625 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/798467/Consecutive-Differences-or-Python3-or-100-time-or-explained | class Solution:
def __init__(self):
self.vals = {
0: [1, 2, 3, 4, 5, 6, 7, 8, 9],
1: [1, 2, 3, 4, 5, 6, 7, 8, 9],
2: [1, 2, 3, 4, 5, 6, 7, 8, 9],
3: [1, 2, 3, 4, 5, 6, 7, 8, 9],
4: [1, 2, 3, 4, 5, 6, 7, 8, 9],
5: [1, 2, 3, 4, 5, 6, 7, 8... | numbers-with-same-consecutive-differences | Consecutive Differences | Python3 | 100% time | explained | Matthias_Pilz | 1 | 177 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,626 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/2640843/Python-O(2N)-O(2N) | class Solution:
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
queue = collections.deque([(i, 1) for i in range(1, 10)])
result = []
while queue:
current, length = queue.popleft()
if length == n:
result.append(current)
con... | numbers-with-same-consecutive-differences | Python - O(2^N), O(2^N) | Teecha13 | 0 | 1 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,627 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/2535437/Python-Solution-using-recursion | class Solution:
def dfs(self,curNum,digLeft,k,ans):
if digLeft==0:
ans.append(curNum)
return
lastDig = curNum%10
if lastDig+k<=9:
self.dfs(curNum*10+lastDig+k,digLeft-1,k,ans)
if lastDig-k>=0 and k!=0:
self.dfs(curNum*10+la... | numbers-with-same-consecutive-differences | Python Solution [using recursion] | miyachan | 0 | 10 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,628 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/2534537/Python-Easy-solution | class Solution:
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
# Find what is accepted first digit
accepted_digits = []
for digit in range(1, 10):
if digit - k >= 0 or digit + k <= 9:
accepted_digits.append(str(digit))
# Get the rest of digits... | numbers-with-same-consecutive-differences | [Python] Easy solution | stefan_ivi | 0 | 18 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,629 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/2530664/python-dfs-quite-straight-forward | class Solution:
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
def list2num(l) :
# for example, [ 1, 8, 1 ] --> 181
s=0
for j in range(len(l)) :
s += l[j] * 10**(len(l)-j-1)
return s
def dfs(i,nums) :
... | numbers-with-same-consecutive-differences | python dfs quite straight-forward | 3upt | 0 | 6 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,630 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/2526070/GolangPython-O(n2)-time-or-O(n2)-space | class Solution:
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
output = []
for i in range(1,10):
dfs(i,i,1,n,k,output)
return output
def dfs(number,last_digit,lenght,n,k,output):
if last_digit < 0 or last_digit > 9:
return
if lenght == n:
outpu... | numbers-with-same-consecutive-differences | Golang/Python O(n^2) time | O(n^2) space | vtalantsev | 0 | 4 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,631 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/2526064/Simplest-and-easy-to-understand-Python-recursive-solution | class Solution:
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
solution = []
def rf(current):
if len(current) == n:
solution.append(int(current))
return
last = int(current[-1])
if k == 0:
rf(current + str(last))
else:
if last - k >= 0:
rf(current + str(last-k)... | numbers-with-same-consecutive-differences | Simplest and easy to understand Python recursive solution | zebra-f | 0 | 9 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,632 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/2525908/Python-solution-or-Backtracking | class Solution:
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
"""backtracking"""
def dfs(num, curDigit):
if len(num) == n:
if int(num) in res: return
res.append(int(num))
return
if curDigit >= ... | numbers-with-same-consecutive-differences | Python solution | Backtracking | MushroomRice | 0 | 5 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,633 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/2525246/Python-solution-oror-Easy-approach-oror-65ms-runtime | class Solution:
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
if n==1:
return range(10)
q=range(1, 10)
for i in range(n-1):
tmp=[]
for i in q:
for d in set([i%10+k, i%10-k]):
if 0<=d<10:
... | numbers-with-same-consecutive-differences | Python solution || Easy approach || 65ms runtime | sowmyamalla111 | 0 | 8 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,634 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/2524532/Backtracking-in-Python-3 | class Solution:
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
def bt(cur: int, i: int = 1):
if i == n:
yield cur
else:
u = cur % 10
cur *= 10
if u + k <= 9:
yield from bt(cur + u... | numbers-with-same-consecutive-differences | Backtracking in Python 3 | mousun224 | 0 | 16 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,635 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/2523868/Python-Easy-readable-DFS | class Solution:
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
result = []
# make a depth first search and make the beginning number
# while leaving out zero
for digit in range(1,10):
dfs(digit, digit, 1, n, k, result)
return result
... | numbers-with-same-consecutive-differences | [Python] - Easy readable DFS | Lucew | 0 | 3 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,636 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/2523849/Python-solution!! | class Solution:
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
ans = []
def countDigit(number):
return len(str(number))
def helper(num,n,k):
if countDigit(num) == n:
ans.append(int(num))
return
... | numbers-with-same-consecutive-differences | Python solution!! | Namangarg98 | 0 | 13 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,637 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/2523569/Backtracking | class Solution:
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
def fsol(t):
vrange = range(1, 10) if t==0 else set([digits[t-1] + v for v in [-k, k] if digits[t-1] + v>=0 and digits[t-1] + v<=9])
for i in vrange:
digits[t] = i
if t==n-1:
... | numbers-with-same-consecutive-differences | Backtracking | dntai | 0 | 3 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,638 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/2523399/Python3-BFS | class Solution:
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
if n == 1:
return [*range(1,10)]
res = []
for num in self.numsSameConsecDiff(n-1,k):
for digit in {num%10+k, num%10-k}:
if 0 <= digit < 10:
res.append(10*num... | numbers-with-same-consecutive-differences | [Python3] BFS | ruosengao | 0 | 4 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,639 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/2523094/Python-oror-DFS-oror-Simple | class Solution:
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
ans = []
def getNumbers(size, x, num):
if size == 0:
ans.append(num)
return
if x + k < 10:
getNumbers(size - 1, x + k, (num * 10) + (x + k))
... | numbers-with-same-consecutive-differences | Python || DFS || Simple ✅ | wilspi | 0 | 19 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,640 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/2522708/Python-recursive-EASY-SOLUTION-DFS | class Solution:
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
self.out = set()
def recurse(curr_num, count):
if count==0:
self.out.add(int(curr_num))
return
last_val = int(curr_num[-1])
if last_val+k>=10 and last_val-k<... | numbers-with-same-consecutive-differences | Python recursive EASY SOLUTION DFS | shubham3 | 0 | 4 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,641 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/2522688/Python-recursive-easy-enderstand | class Solution:
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
res = []
def helper(curr):
if len(curr) == n:
res.append(int(curr))
return
if int(curr[-1]) + k < 10:
helper(curr + str(int(curr[-1]) + k))
i... | numbers-with-same-consecutive-differences | Python recursive easy-enderstand | Kennyyhhu | 0 | 7 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,642 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/2522501/Python-simple-and-clear-or-96-faster-or-Recursive | class Solution:
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
#goes an iteration deeper as long as we haven't reached n length and adding or subtracting k does not go out of bounds
def buildNum (n, k, digitloc, digit, number, sol):
if digitloc == n:
return so... | numbers-with-same-consecutive-differences | Python simple and clear | 96% faster | Recursive | yuvalsaf | 0 | 11 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,643 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/2522476/Easy-Recursive-solution. | class Solution:
def listtoNumber(self,con):
st=''
for i in con:
st+=str(i)
return int(st)
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
res = []
con = [0] * n
def fun(ind,con):
if ind==n:
nos=... | numbers-with-same-consecutive-differences | Easy Recursive solution. | Mohit_Hadiyal16 | 0 | 6 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,644 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/2522289/Python-or-Neat-and-Clean-Code-or-Using-DFS | class Solution:
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
res = []
if n == 1:
res.append(0)
def dfs(num,n):
if n == 0:
res.append(num)
return
lastDigit = num % 10
if lastDigit >= k:
... | numbers-with-same-consecutive-differences | Python | Neat and Clean Code | Using DFS | __Asrar | 0 | 8 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,645 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/2522016/python | class Solution:
res = set()
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
self.res = set()
def createNum(num):
#termination case
if len(num)>n:
return
#acceptance case + termination
if len(num)==n:
self.res.add(num)
... | numbers-with-same-consecutive-differences | python | rojanrookhosh | 0 | 7 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,646 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/2521941/Python-Solution-or-Brute-Force-or-Optimized | class Solution:
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
# Brute Force
# ans=[]
# for i in range(10**(n-1), 10**(n)):
# s=str(i)
# flag=True
# for j in range(1, len(s)):
# if abs(int(s[j])-int(s[j-1]))!=k:
# ... | numbers-with-same-consecutive-differences | Python Solution | Brute Force | Optimized | Siddharth_singh | 0 | 17 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,647 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/2521832/Python3-Solution-or-BFS | class Solution:
def numsSameConsecDiff(self, n, k):
q = collections.deque(list(range(1, 10)))
for i in range(1, n):
m = len(q)
for j in range(m):
val = q.popleft()
mod = val % 10
if mod + k <= 9: q.append(val * 10 + mod + k)
... | numbers-with-same-consecutive-differences | ✔ Python3 Solution | BFS | satyam2001 | 0 | 11 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,648 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/2521458/Easy-python-solution-using-recursion | class Solution:
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
def func(i,n,k,lst,s):
if i<0 or i>=10:
return lst
if n==0:
if s not in lst:
lst.append(s)
return lst
lst=func(i-k,n-1,k,lst,s+st... | numbers-with-same-consecutive-differences | Easy python solution using recursion | shubham_1307 | 0 | 23 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,649 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/2521404/Python-or-Backtracking | class Solution:
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
allNumbers = []; currentNumber = []
def backtrack(currentDigit: int, numberOfDigits: int = 1):
if numberOfDigits == n:
allNumbers.append(int("".join(currentNumber)))
els... | numbers-with-same-consecutive-differences | Python | Backtracking | sr_vrd | 0 | 17 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,650 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/1966069/Python-easy-to-read-and-understand-or-recursion | class Solution:
def dfs(self, num, n, k):
if n == 0:
#print(num)
self.ans.append(int(num))
return
else:
digit = num[-1]
if int(digit) - k >= 0:
self.dfs(num+str(int(digit)-k), n-1, k)
if int(digit) + k < 10:
... | numbers-with-same-consecutive-differences | Python easy to read and understand | recursion | sanial2001 | 0 | 41 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,651 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/1925944/PYTHON-SOL-oror-EASY-oror-RECURSION-%2B-MEMOIZATION-oror-FAST-oror-WELL-COMMENTED-CODE-oror | class Solution:
def recursion(self, cur , size , n , diff ):
# if we have already made cur don't repeat
if cur in self.dp : return
# mark cur as visited
self.dp[cur] = True
# base case
if size == n:
self.ans.append(int(cur))
... | numbers-with-same-consecutive-differences | PYTHON SOL || EASY || RECURSION + MEMOIZATION || FAST || WELL COMMENTED CODE || | reaper_27 | 0 | 48 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,652 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/1841947/python-simple-dfs-solution | class Solution:
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
stack = []
for i in range(1, 10):
stack.append((str(i), 1))
res = set()
while stack:
num, length = stack.pop()
if length == n:
... | numbers-with-same-consecutive-differences | python simple dfs solution | byuns9334 | 0 | 39 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,653 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/1737155/easy-using-backtracking-python3 | class Solution:
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
self.ans = []
def solve(com):
if(len(com) == n):
self.ans.append(int(com))
return
for i in range(10):
if(not com and i == 0):
continue
if(not com or abs(int(com[-1])-int(i)) == k):
solve(com+str(i))
solve("... | numbers-with-same-consecutive-differences | easy using backtracking python3 | jagdishpawar8105 | 0 | 33 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,654 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/1307934/Python-3-BFS-using-yields | class Solution:
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
poss_entries = self.get_valid_entries_two_digits(k)
for _ in range(n-2):
poss_entries = self.get_valid_entries(poss_entries, k)
return list(map(int, poss_entries))
def get_valid_entries_two_digits(self... | numbers-with-same-consecutive-differences | Python 3 BFS using yields | gamestopcantstop | 0 | 60 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,655 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/1191960/simple-bfs | class Solution:
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
queue=[]
for i in range(1,10):
queue.append(str(i))
vis=set(queue)
count=0
ans=[]
while(queue):
print(queue)
if count<n:
l=len(queue)
... | numbers-with-same-consecutive-differences | simple bfs | heisenbarg | 0 | 29 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,656 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/842073/python3-or-dfs | class Solution:
def __init__(self):
self.nums = []
def Util(self, N, K, cur):
if len(cur) > N:
return []
if len(cur) == N:
return [cur]
lis1 = []
lis2 = []
if int(cur[-1]) - K >= 0 and cur[0] != '0':
lis1 = self.Util(N, K, ... | numbers-with-same-consecutive-differences | python3 | dfs | _YASH_ | 0 | 28 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,657 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/798955/Python3-backtracking-and-dp | class Solution:
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
def fn(i, x):
"""Populate ans via a stack."""
stack.append(x)
if i == n-1: ans.append(int("".join(map(str, stack))))
else:
if x + k < 10: fn(i+1, x+k)
... | numbers-with-same-consecutive-differences | [Python3] backtracking & dp | ye15 | 0 | 42 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,658 |
https://leetcode.com/problems/numbers-with-same-consecutive-differences/discuss/798955/Python3-backtracking-and-dp | class Solution:
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
def fn(i, x):
"""Return numbers with same consecutive differences."""
if i == n-1: return [str(x)]
ans = []
if x+k < 10: ans += [str(x) + xx for xx in fn(i+1, x+k)]
... | numbers-with-same-consecutive-differences | [Python3] backtracking & dp | ye15 | 0 | 42 | numbers with same consecutive differences | 967 | 0.571 | Medium | 15,659 |
https://leetcode.com/problems/binary-tree-cameras/discuss/2160386/Python-Making-a-Hard-Problem-Easy!-Postorder-Traversal-with-Explanation | class Solution:
def minCameraCover(self, root: TreeNode) -> int:
# set the value of camera nodes to 1
# set the value of monitored parent nodes to 2
def dfs(node: Optional[TreeNode]) -> int:
if not node:
return 0
res = dfs(node.left)+dfs(node.right)
... | binary-tree-cameras | [Python] Making a Hard Problem Easy! Postorder Traversal with Explanation | zayne-siew | 50 | 2,100 | binary tree cameras | 968 | 0.468 | Hard | 15,660 |
https://leetcode.com/problems/binary-tree-cameras/discuss/2160618/Python-Easy-Postorder-with-explanation | class Solution:
def minCameraCover(self, root: Optional[TreeNode]) -> int:
def postorder(node):
if not node:
return (0, math.inf)
l_count, l_state = postorder(node.left)
r_count, r_state = postorder(node.right)
state =... | binary-tree-cameras | ✅ Python Easy Postorder with explanation | constantine786 | 18 | 551 | binary tree cameras | 968 | 0.468 | Hard | 15,661 |
https://leetcode.com/problems/binary-tree-cameras/discuss/2160278/PYTHON-oror-EXPLAINED-oror | class Solution:
res = 0
def minCameraCover(self, root: TreeNode) -> int:
def dfs(node: TreeNode) -> int:
if not node: return 0
val = dfs(node.left) + dfs(node.right)
if val == 0: return 3
if val < 3: return 0
self.re... | binary-tree-cameras | ✔️ PYTHON || EXPLAINED || ;] | karan_8082 | 8 | 479 | binary tree cameras | 968 | 0.468 | Hard | 15,662 |
https://leetcode.com/problems/binary-tree-cameras/discuss/2160423/Python3-DFS-solution | class Solution(object):
def minCameraCover(self, root):
result = [0]
# 0 indicates that it doesn't need cam which might be a leaf node or a parent node which is already covered
# < 3 indicates that it has already covered
# >= 3 indicates that it needs a cam
... | binary-tree-cameras | 📌 Python3 DFS solution | Dark_wolf_jss | 5 | 31 | binary tree cameras | 968 | 0.468 | Hard | 15,663 |
https://leetcode.com/problems/binary-tree-cameras/discuss/2163005/Python-Recursion-%2B-Memoization-%2B-DP-%2B-Greedy | class Solution:
def minCameraCover(self, root: Optional[TreeNode]) -> int:
hasCamera = False
isMonitored = False
return self.helper(root, hasCamera, isMonitored)
# Hypothesis -> Will always return the minimum Cameras required
def helper(self, root, hasCamera, isMonitored) -> int:
... | binary-tree-cameras | Python Recursion + Memoization + DP + Greedy | zippysphinx | 1 | 94 | binary tree cameras | 968 | 0.468 | Hard | 15,664 |
https://leetcode.com/problems/binary-tree-cameras/discuss/2163005/Python-Recursion-%2B-Memoization-%2B-DP-%2B-Greedy | class Solution:
def minCameraCover(self, root: Optional[TreeNode]) -> int:
def solve(node):
# Keep track, of all the cases
# CASE-1 = ans[0] means, all nodes below i,e subtrees of left and right are monitored, not the current node
# CASE-2 = ans[1] means, all, the nodes b... | binary-tree-cameras | Python Recursion + Memoization + DP + Greedy | zippysphinx | 1 | 94 | binary tree cameras | 968 | 0.468 | Hard | 15,665 |
https://leetcode.com/problems/binary-tree-cameras/discuss/2163005/Python-Recursion-%2B-Memoization-%2B-DP-%2B-Greedy | class Solution:
def minCameraCover(self, root: Optional[TreeNode]) -> int:
self.minimumCameras = 0
# Again, Distinguish with cases
# 1 = node is not monitored
# 2 = node is monitored, but no camera
# 3 = has camera
def dfs(root):
# Null node, is always not... | binary-tree-cameras | Python Recursion + Memoization + DP + Greedy | zippysphinx | 1 | 94 | binary tree cameras | 968 | 0.468 | Hard | 15,666 |
https://leetcode.com/problems/binary-tree-cameras/discuss/2163895/Python3-or-O(n)-or-Greedy-Approach | class Solution:
def minCameraCover(self, root: Optional[TreeNode]) -> int:
def dfs(root):
if not root:
return "covered"
l = dfs(root.left)
r = dfs(root.right)
if l=="needed" or r=="needed":
self.camera+=1
... | binary-tree-cameras | Python3 | O(n) | Greedy Approach | theKshah | 0 | 7 | binary tree cameras | 968 | 0.468 | Hard | 15,667 |
https://leetcode.com/problems/binary-tree-cameras/discuss/2161454/Postorder-DFS-oror-Fastest-OptimalSolution-oror-TC-%3A-O(N)-oror-SC-%3A-O(1) | class Solution:
def __init__(self):
self.minCameras = 0
def cameras(self,root, minCameras):
if root == None:
return "ok"
left = self.cameras(root.left, minCameras)
right = self.cameras(root.right, minCameras)
if left == "want" or rig... | binary-tree-cameras | Postorder DFS || Fastest OptimalSolution || TC :- O(N) || SC :- O(1) | Vaibhav7860 | 0 | 21 | binary tree cameras | 968 | 0.468 | Hard | 15,668 |
https://leetcode.com/problems/binary-tree-cameras/discuss/1464031/Python-memoization-rec-easy-to-understand-with-comments | class Solution:
def minCameraCover(self, root: Optional[TreeNode]) -> int:
memo = {} # dict for storing dp states
# we need three variables, node , parent denoting if parent is covered or not,
# and done denoting whether current node is covered or not
def ... | binary-tree-cameras | Python memoization rec easy to understand -- with comments | ashish_chiks | 0 | 83 | binary tree cameras | 968 | 0.468 | Hard | 15,669 |
https://leetcode.com/problems/binary-tree-cameras/discuss/1428731/Python3-Postorder-DFS | class Solution:
def __init__(self):
self.cameras = 0
def traversal(self, node):
if node == None:
return (False, False)
l_camera, l_need_vision = self.traversal(node.left)
r_camera, r_need_vision = self.traversal(node.right)
... | binary-tree-cameras | [Python3] Postorder DFS | maosipov11 | 0 | 73 | binary tree cameras | 968 | 0.468 | Hard | 15,670 |
https://leetcode.com/problems/binary-tree-cameras/discuss/1214372/Python3-greedy-tri-color-encoding | class Solution:
def minCameraCover(self, root: TreeNode) -> int:
def fn(node):
"""Return color-coding of a node.
0 - not covered
1 - covered w/o camera
2 - covered w/ camera
"""
nonlocal ans
if not node: return 1... | binary-tree-cameras | [Python3] greedy - tri-color encoding | ye15 | 0 | 53 | binary tree cameras | 968 | 0.468 | Hard | 15,671 |
https://leetcode.com/problems/binary-tree-cameras/discuss/1212014/python3-post-order-traversal-solution-for-reference | class Solution:
def minCameraCover(self, root: TreeNode) -> int:
# Post order traversal to make sure we transit left, right and root so that camera's can be assigned in the right order.
def postorder(root, parent):
if not root:
return 0
L = postor... | binary-tree-cameras | [python3] post order traversal solution for reference | vadhri_venkat | 0 | 59 | binary tree cameras | 968 | 0.468 | Hard | 15,672 |
https://leetcode.com/problems/pancake-sorting/discuss/2844744/Kind-of-a-simulation-solution | class Solution:
def pancakeSort(self, arr: List[int]) -> List[int]:
if arr == sorted(arr):
return []
flips = []
end = len(arr) - 1
# find the max flip all the numbers from the first position to the max position
# ==> from 0 to max_position = k
... | pancake-sorting | Kind of a simulation solution | khaled_achech | 0 | 1 | pancake sorting | 969 | 0.7 | Medium | 15,673 |
https://leetcode.com/problems/pancake-sorting/discuss/2516256/Very-simple-Python3-solution-beats-most-submissions | class Solution:
def pancakeSort(self, arr: List[int]) -> List[int]:
#helper function to flip the numbers in the array
def flip(i, j):
while i < j:
arr[i], arr[j] = arr[j], arr[i]
j -= 1
i += 1
#sort from 0 to i
def sort(i... | pancake-sorting | ✔️ Very simple Python3 solution beats most submissions | Kagoot | 0 | 20 | pancake sorting | 969 | 0.7 | Medium | 15,674 |
https://leetcode.com/problems/pancake-sorting/discuss/2393939/Python-Easy-Recursive-thorough-explanation | class Solution:
def pancakeSort(self, arr: List[int]) -> List[int]:
#helper function to flip the numbers in the array
def flip(i, j):
while i < j:
arr[i], arr[j] = arr[j], arr[i]
j -= 1
i += 1
#sort from 0 to i
def sort(i... | pancake-sorting | Python Easy Recursive, thorough explanation | gypark23 | 0 | 37 | pancake sorting | 969 | 0.7 | Medium | 15,675 |
https://leetcode.com/problems/pancake-sorting/discuss/1933155/PYTHON-SOL-oror-GREEDY-oror-TWO-POINTER-oror-WELL-EXPLAINED-oror-SIMPLE-oror | class Solution:
def pancakeSort(self, arr: List[int]) -> List[int]:
n = len(arr)
issorted = True
for i in range(1,n):
if arr[i] < arr[i-1] :
issorted = False
break
if issorted == True : return []
flips = []
for i in... | pancake-sorting | PYTHON SOL || GREEDY || TWO POINTER || WELL EXPLAINED || SIMPLE || | reaper_27 | 0 | 118 | pancake sorting | 969 | 0.7 | Medium | 15,676 |
https://leetcode.com/problems/pancake-sorting/discuss/1795022/Easily-understandable-Python-solution | class Solution:
def pancakeSort(self, arr: List[int]) -> List[int]:
i=0
n=len(arr)
kArr=[]
sorted = 0
while i<n:
if not self.checkSorted(arr):
k=self.getMaxIndex(arr[:n-sorted])
if k != 0:
kArr.append(k+1)
... | pancake-sorting | Easily understandable Python solution | wadhwahitesh | 0 | 42 | pancake sorting | 969 | 0.7 | Medium | 15,677 |
https://leetcode.com/problems/pancake-sorting/discuss/1380943/Python-or-Shifting-max-to-first-then-flipping-the-whole-array | class Solution:
def pancakeSort(self, arr: List[int]) -> List[int]:
n=len(arr)
ans=[]
for i in range(n):
maxi=self.findmax(arr,n-i)
ans.append(maxi+1)
self.reverse(arr,maxi+1)
self.reverse(arr,n-i)
ans.append(n-i)
return ans... | pancake-sorting | Python | Shifting max to first then flipping the whole array | swapnilsingh421 | 0 | 51 | pancake sorting | 969 | 0.7 | Medium | 15,678 |
https://leetcode.com/problems/pancake-sorting/discuss/1343748/Python-3-hack-or-O(n)-T-or-O(n)-S | class Solution:
def pancakeSort(self, arr: List[int]) -> List[int]:
a = arr.copy()
end = len(a)
cur_max = len(a)
to_index = [0]*(len(a)+1)
for i, num in enumerate(a):
to_index[num] = i
result = []
while end > 1:
i = to_index[cu... | pancake-sorting | Python 3 hack | O(n) T | O(n) S | CiFFiRO | 0 | 48 | pancake sorting | 969 | 0.7 | Medium | 15,679 |
https://leetcode.com/problems/pancake-sorting/discuss/1290819/Python3-solution-94-faster | class Solution:
def pancakeSort(self, arr: List[int]) -> List[int]:
result, temp, arr_len=[], 0, len(arr)
while True:
if arr_len-temp==1:
break
curr_max=max(arr[:arr_len-temp])
curr_index=arr.index(curr_max)
if curr_index==arr_len-temp-... | pancake-sorting | Python3 solution, 94% faster | alter_mage | 0 | 75 | pancake sorting | 969 | 0.7 | Medium | 15,680 |
https://leetcode.com/problems/pancake-sorting/discuss/981053/Python3-greedy-O(N2) | class Solution:
def pancakeSort(self, arr: List[int]) -> List[int]:
ans = []
for x in range(len(arr), 0, -1):
i = arr.index(x)
if i+1 != x:
if i: ans.append(i+1)
ans.append(x)
arr[:i+1] = arr[:i+1][::-1]
arr[:x... | pancake-sorting | [Python3] greedy O(N^2) | ye15 | 0 | 64 | pancake sorting | 969 | 0.7 | Medium | 15,681 |
https://leetcode.com/problems/pancake-sorting/discuss/817919/Python-easy-solution | class Solution:
def pancakeSort(self, A: List[int]) -> List[int]:
ans=[]
for i in range(len(A),1,-1):
temp=A.index(max(A[:i]))
if temp==i-1:
continue
else:
A=A[temp::-1]+A[temp+1:]
ans.append(temp+1)
A=A[i-1::-1]+A[i:]
ans.append(i)
return ans | pancake-sorting | Python easy solution | RedHeadphone | 0 | 120 | pancake sorting | 969 | 0.7 | Medium | 15,682 |
https://leetcode.com/problems/pancake-sorting/discuss/642983/Intuitive-approach-by-sorting-from-the-end-of-array-(similar-to-select-sort) | class Solution:
def pancakeSort(self, A: List[int]) -> List[int]:
sort_k_list = []
''' sorting k used for pancake flip '''
for i in range(len(A), 0, -1):
v = A[i-1]
if v != i:
# Start pancake sorting
# 0) Look for value as `v`
... | pancake-sorting | Intuitive approach by sorting from the end of array (similar to select sort) | puremonkey2001 | 0 | 47 | pancake sorting | 969 | 0.7 | Medium | 15,683 |
https://leetcode.com/problems/powerful-integers/discuss/1184254/Python3-brute-force | class Solution:
def powerfulIntegers(self, x: int, y: int, bound: int) -> List[int]:
bx = int(log(bound)/log(x)) if x > 1 else 0
by = int(log(bound)/log(y)) if y > 1 else 0
ans = set()
for i in range(bx+1):
for j in range(by+1):
if x**i + y**j <... | powerful-integers | [Python3] brute force | ye15 | 2 | 45 | powerful integers | 970 | 0.436 | Medium | 15,684 |
https://leetcode.com/problems/powerful-integers/discuss/795396/Python-Solution%3A-faster-than-100-less-memory-than-64 | class Solution:
def powerfulIntegers(self, x: int, y: int, bound: int) -> List[int]:
if x == 1 and y == 1:
if bound >= 2:
return [2]
else:
return []
ans = []
if x == 1 or y == 1:
num = max(x, y)
exponent = 0
... | powerful-integers | Python Solution: faster than 100%, less memory than 64% | amateurpirate | 1 | 102 | powerful integers | 970 | 0.436 | Medium | 15,685 |
https://leetcode.com/problems/powerful-integers/discuss/2129143/python-3-oror-simple-generator-solution | class Solution:
def powerfulIntegers(self, x: int, y: int, bound: int) -> List[int]:
if bound == 0:
return []
def get(v):
yield 1
if v == 1:
return
vi = v
while vi <= bound:
yield vi
... | powerful-integers | python 3 || simple generator solution | dereky4 | 0 | 30 | powerful integers | 970 | 0.436 | Medium | 15,686 |
https://leetcode.com/problems/powerful-integers/discuss/1933201/PYTHON-SOL-oror-SIMPLE-oror-LOOPS-oror-EXPLAINED-WELL-oror | class Solution:
def powerfulIntegers(self, x: int, y: int, bound: int) -> List[int]:
d = {}
a = 1
while a < bound:
b = 1
while True:
if a + b <= bound:
d[a+b] = True
b*=y
if y == 1: b... | powerful-integers | PYTHON SOL || SIMPLE || LOOPS || EXPLAINED WELL || | reaper_27 | 0 | 51 | powerful integers | 970 | 0.436 | Medium | 15,687 |
https://leetcode.com/problems/powerful-integers/discuss/1837625/Python-easy-to-read-and-understand-or-math | class Solution:
def powerfulIntegers(self, x: int, y: int, bound: int) -> List[int]:
xset, yset = set(), set()
for i in range(20):
if x**i < bound:
xset.add(x**i)
for i in range(20):
if y**i < bound:
yset.add(y**i)
... | powerful-integers | Python easy to read and understand | math | sanial2001 | 0 | 30 | powerful integers | 970 | 0.436 | Medium | 15,688 |
https://leetcode.com/problems/powerful-integers/discuss/1489078/Python-super-simple-solution | class Solution:
def powerfulIntegers(self, x: int, y: int, bound: int) -> List[int]:
res = set()
powerx = [1]
powery = [1]
a, b = x, y
if x > 1:
while x < bound:
powerx.append(x)
x = x*a
if y > 1:
while y < bound... | powerful-integers | Python super simple solution | byuns9334 | 0 | 81 | powerful integers | 970 | 0.436 | Medium | 15,689 |
https://leetcode.com/problems/powerful-integers/discuss/1184072/Super-Easy-python-solution-Weird-observation | class Solution:
def powerfulIntegers(self, x: int, y: int, bound: int) -> List[int]:
if bound <=1:
return []
ans=set()
for i in range(101):
xpow=x**i
if xpow > bound:
break
for j ... | powerful-integers | Super Easy python solution, Weird observation | hasham | 0 | 35 | powerful integers | 970 | 0.436 | Medium | 15,690 |
https://leetcode.com/problems/powerful-integers/discuss/1183895/python-simplest-chillbro | class Solution:
def powerfulIntegers(self, x: int, y: int, bound: int) -> List[int]:
s=[]
i=0;j=0
while x**(i+1)<=bound:
if x==1:
break
i+=1
while y**(j+1)<=bound:
if y==1:
break
j+=1
for ii in range(i+1):
for j... | powerful-integers | python simplest #chillbro | Khacker | 0 | 31 | powerful integers | 970 | 0.436 | Medium | 15,691 |
https://leetcode.com/problems/powerful-integers/discuss/1183895/python-simplest-chillbro | class Solution:
def powerfulIntegers(self, x: int, y: int, bound: int) -> List[int]:
a = bound if x == 1 else int(log(bound, x))
b = bound if y == 1 else int(log(bound, y))
powerful_integers = set([])
for i in range(a + 1):
for j in range(b + 1)... | powerful-integers | python simplest #chillbro | Khacker | 0 | 31 | powerful integers | 970 | 0.436 | Medium | 15,692 |
https://leetcode.com/problems/powerful-integers/discuss/1183843/PythonPython3-Solution-with-exaplanation-and-easy-understanding | class Solution:
def powerfulIntegers(self, x: int, y: int, bound: int) -> List[int]:
x_bound, y_bound = 0, 0 # assign two variable and set it to zero
resLis = set() # to store the values computed
if x == 1: # if x is 1 then reinitialize x_bound to 1
x_bound = 1
else: #el... | powerful-integers | Python/Python3 Solution with exaplanation and easy understanding | prasanthksp1009 | 0 | 60 | powerful integers | 970 | 0.436 | Medium | 15,693 |
https://leetcode.com/problems/powerful-integers/discuss/1183843/PythonPython3-Solution-with-exaplanation-and-easy-understanding | class Solution:
def powerfulIntegers(self, x: int, y: int, bound: int) -> List[int]:
x_bound, y_bound = 0, 0 # assign two variable and set it to zero
resLis = [] # to store the values computed
if x == 1: # if x is 1 then reinitialize x_bound to 1
x_bound = 1
else: #else ... | powerful-integers | Python/Python3 Solution with exaplanation and easy understanding | prasanthksp1009 | 0 | 60 | powerful integers | 970 | 0.436 | Medium | 15,694 |
https://leetcode.com/problems/powerful-integers/discuss/246276/Python.-Good-solution.-Both-speed-and-memory | class Solution:
def powerfulIntegers(self, x, y, bound):
answer = []
cnt1 = 0
cnt2 = 0
while x**(cnt1+1) <= bound:
if x == 1:
break
cnt1 += 1
while y**(cnt2+1) <= bound:
if y == 1:
break
cnt2 += 1... | powerful-integers | Python. Good solution. Both speed and memory | ali95 | 0 | 228 | powerful integers | 970 | 0.436 | Medium | 15,695 |
https://leetcode.com/problems/flip-binary-tree-to-match-preorder-traversal/discuss/1343381/Elegant-Python-Iterative-and-Recursive-Preorder-Traversals | class Solution:
def __init__(self):
self.flipped_nodes = []
self.index = 0
def flipMatchVoyage(self, root: TreeNode, voyage: List[int]) -> List[int]:
queue = deque([root])
while queue:
node = queue.pop()
if not node: continue
if node.v... | flip-binary-tree-to-match-preorder-traversal | Elegant Python Iterative & Recursive Preorder Traversals | soma28 | 0 | 75 | flip binary tree to match preorder traversal | 971 | 0.499 | Medium | 15,696 |
https://leetcode.com/problems/flip-binary-tree-to-match-preorder-traversal/discuss/1343381/Elegant-Python-Iterative-and-Recursive-Preorder-Traversals | class Solution:
def __init__(self):
self.flipped_nodes = []
self.index = 0
self.flag = False
def flipMatchVoyage(self, root: TreeNode, voyage: List[int]) -> List[int]:
def preorder(node = root):
if not node: return
if node.val != voyage[self.index... | flip-binary-tree-to-match-preorder-traversal | Elegant Python Iterative & Recursive Preorder Traversals | soma28 | 0 | 75 | flip binary tree to match preorder traversal | 971 | 0.499 | Medium | 15,697 |
https://leetcode.com/problems/flip-binary-tree-to-match-preorder-traversal/discuss/1132472/Python-3-Iterative-DFS | class Solution:
def flipMatchVoyage(self, root: TreeNode, voyage: List[int]) -> List[int]:
stack, res = [root], []
for i in range(len(voyage) - 1):
node = stack.pop()
if node.val != voyage[i]:
return [-1]
if node.left and node.right and node.right.... | flip-binary-tree-to-match-preorder-traversal | [Python 3] Iterative DFS | mcolen | 0 | 76 | flip binary tree to match preorder traversal | 971 | 0.499 | Medium | 15,698 |
https://leetcode.com/problems/flip-binary-tree-to-match-preorder-traversal/discuss/982172/Python3-recursive-dfs | class Solution:
def flipMatchVoyage(self, root: TreeNode, voyage: List[int]) -> List[int]:
def fn(node, i):
"""Return if tree can be flipped to match traversal & size."""
if not node: return True, 0
if node.right and node.right.val == voyage[i+1]:
... | flip-binary-tree-to-match-preorder-traversal | [Python3] recursive dfs | ye15 | -1 | 92 | flip binary tree to match preorder traversal | 971 | 0.499 | Medium | 15,699 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.