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/fibonacci-number/discuss/2813107/Python-Solution-2-Lines-..-Easy-Recursibe | class Solution:
def fib(self, n: int) -> int:
if n == 0 or n == 1:return n
return self.fib(n-1) + self.fib(n-2) | fibonacci-number | Python Solution 2 Lines .. Easy Recursibe | poison_11 | 0 | 6 | fibonacci number | 509 | 0.692 | Easy | 9,000 |
https://leetcode.com/problems/fibonacci-number/discuss/2807986/Python-90-Easy-Solution-No-Recursion | class Solution:
def fib(self, n: int) -> int:
arr = [0,1]
for i in range(n):
arr.append(arr[i] + arr[i+1])
return arr[n] | fibonacci-number | Python 90% Easy Solution No Recursion | Jashan6 | 0 | 4 | fibonacci number | 509 | 0.692 | Easy | 9,001 |
https://leetcode.com/problems/fibonacci-number/discuss/2807980/Python-Recursion-Solution-Easy | class Solution:
def fib(self, n: int) -> int:
if n < 2:
return n
return self.fib(n-1) + self.fib(n-2) | fibonacci-number | Python Recursion Solution Easy | Jashan6 | 0 | 3 | fibonacci number | 509 | 0.692 | Easy | 9,002 |
https://leetcode.com/problems/fibonacci-number/discuss/2803330/Pyth3-Recursion-Solution-for-Fibonacci | class Solution:
def fib(self, n: int) -> int:
if n == 0 or n == 1:
return n
return self.fib(n-1) + self.fib(n-2) | fibonacci-number | Pyth3 Recursion Solution for Fibonacci | abandonedCat | 0 | 3 | fibonacci number | 509 | 0.692 | Easy | 9,003 |
https://leetcode.com/problems/fibonacci-number/discuss/2803135/Bottom-Up-Dynamic-Programming-(with-memoization)-approach | class Solution:
def fib(self, n: int) -> int:
dp = [0, 1]
if n == 0 or n == 1:
return dp[n]
for i in range(2, n + 1):
dp.append(dp[i - 1]+dp[i - 2])
return dp[n] | fibonacci-number | Bottom-Up Dynamic Programming (with memoization) approach | khritish17 | 0 | 2 | fibonacci number | 509 | 0.692 | Easy | 9,004 |
https://leetcode.com/problems/fibonacci-number/discuss/2800369/Python3-or-509.-Fibonacci-Number | class Solution:
memo = {}
def fib(self, n: int) -> int:
if n == 1:
return 1
if n == 0:
return 0
if n in self.memo:
return self.memo[n]
self.memo[n] = self.fib(n-1) + self.fib(n-2)
return self.memo[n] | fibonacci-number | Python3 | 509. Fibonacci Number | AndrewMitchell25 | 0 | 2 | fibonacci number | 509 | 0.692 | Easy | 9,005 |
https://leetcode.com/problems/fibonacci-number/discuss/2799187/Best-Dynamic-SOLUTION | class Solution:
def fib(self, n: int) -> int:
num = [0,1]
for i in range(2, n+1):
num.append(num[i-1] + num[i-2])
return num[n] | fibonacci-number | Best Dynamic SOLUTION | zaberraiyan | 0 | 2 | fibonacci number | 509 | 0.692 | Easy | 9,006 |
https://leetcode.com/problems/fibonacci-number/discuss/2790533/Backtracking | class Solution:
def fib(self, n: int) -> int:
if n == 0: return 0
elif n == 1: return 1
else: return self.fib(n-1)+self.fib(n-2) | fibonacci-number | Backtracking | haniyeka | 0 | 2 | fibonacci number | 509 | 0.692 | Easy | 9,007 |
https://leetcode.com/problems/fibonacci-number/discuss/2786579/Python%3A-Faster-than-98-Begginer-Friendly-Simple | class Solution:
def fib(self, n: int) -> int:
cache = {}
def fib_calculator(n):
if n in cache:
return cache[n]
elif n < 2:
cache[n] = n
return n
else:
cache[n] = fib_calculator(n-1) + fib_calculator(n... | fibonacci-number | Python: Faster than 98%, Begginer Friendly, Simple | faisalkhan91 | 0 | 3 | fibonacci number | 509 | 0.692 | Easy | 9,008 |
https://leetcode.com/problems/fibonacci-number/discuss/2759112/PythonorEasy-solution | class Solution:
def fib(self, n: int) -> int:
if n == 0:
return 0
if n == 1:
return 1
return self.fib(n-1)+self.fib(n-2) | fibonacci-number | Python|Easy solution | lucy_sea | 0 | 3 | fibonacci number | 509 | 0.692 | Easy | 9,009 |
https://leetcode.com/problems/fibonacci-number/discuss/2750102/Fabonacci-number | class Solution:
def fib(self, n: int) -> int:
first, second = 0,1
if n == 0 or n == 1:
return n
count = 0
for i in range(1, n):
count = first + second
first = second
second = count
return count | fibonacci-number | Fabonacci number | Yamini2255 | 0 | 4 | fibonacci number | 509 | 0.692 | Easy | 9,010 |
https://leetcode.com/problems/fibonacci-number/discuss/2749910/1-line-Python-solution-using-recursion | class Solution:
def fib(self, n: int) -> int:
return n if n in [0, 1] else self.fib(n-1) + self.fib(n-2) | fibonacci-number | ๐ 1-line Python solution using recursion | croatoan | 0 | 6 | fibonacci number | 509 | 0.692 | Easy | 9,011 |
https://leetcode.com/problems/fibonacci-number/discuss/2748729/The-easiest-solution-of-Fibonacci-series | class Solution:
def fib(self, n: int) -> int:
l1 = [0,1]
for i in range(n-1):
summ = l1[-1] + l1[-2]
l1.append(summ)
return l1[-1] | fibonacci-number | The easiest solution of Fibonacci series | Bizi_77 | 0 | 2 | fibonacci number | 509 | 0.692 | Easy | 9,012 |
https://leetcode.com/problems/fibonacci-number/discuss/2741058/Python3-Solution-Using-Recursion | class Solution:
def fib(self, n: int) -> int:
if n == 0:
return 0
elif n == 1:
return 1
else:
return self.fib(n-1)+self.fib(n-2) | fibonacci-number | Python3 Solution Using Recursion | dnvavinash | 0 | 4 | fibonacci number | 509 | 0.692 | Easy | 9,013 |
https://leetcode.com/problems/fibonacci-number/discuss/2735915/Python3-recursion-with-memoization-explained | class Solution:
mem = {0:0, 1:1}
def fib(self, n: int) -> int:
if n in self.mem.keys():
return self.mem[n]
else:
res = self.fib(n-1) + self.fib(n-2)
self.mem[n] = res
return res | fibonacci-number | Python3 recursion with memoization explained | aaalex_lit | 0 | 1 | fibonacci number | 509 | 0.692 | Easy | 9,014 |
https://leetcode.com/problems/fibonacci-number/discuss/2735915/Python3-recursion-with-memoization-explained | class Solution:
mem = [0, 1]
def fib(self, n: int) -> int:
if n < len(self.mem):
return self.mem[n]
else:
res = self.fib(n-1) + self.fib(n-2)
self.mem.append(res)
return res | fibonacci-number | Python3 recursion with memoization explained | aaalex_lit | 0 | 1 | fibonacci number | 509 | 0.692 | Easy | 9,015 |
https://leetcode.com/problems/fibonacci-number/discuss/2717365/Simple-Recursively-Solution-using-DP | class Solution:
def fib(self, n: int, dp = {0:0, 1:1}) -> int:
if n in dp:
return dp[n]
dp[n] = self.fib(n-1, dp) + self.fib(n-2, dp)
return dp[n] | fibonacci-number | Simple Recursively Solution using DP | Akkishanu | 0 | 1 | fibonacci number | 509 | 0.692 | Easy | 9,016 |
https://leetcode.com/problems/fibonacci-number/discuss/2714933/Fibonacci-Number-with-Recursion-in-Python-very-Easy-way | class Solution:
sys.setrecursionlimit(10**9)
def fib(self, n: int) -> int:
if n == 1:
return 1
if n == 0:
return 0
a = 0
b = 1
c = 0
for i in range(n-1):
c = a + b
a = b
b = c
return c | fibonacci-number | Fibonacci Number with Recursion in Python very Easy way | jashii96 | 0 | 4 | fibonacci number | 509 | 0.692 | Easy | 9,017 |
https://leetcode.com/problems/fibonacci-number/discuss/2714359/python-best-solution-(Runtime-Beats-94.76-Memory13.8-MB-Beats-95.80) | class Solution:
def fib(self, n: int) -> int:
if n<=1: return n
l = [0]*(n+1)
l[0]=0
l[1]=1
for i in range(2,n+1):
l[i]=l[i-1]+l[i-2]
return l[n] | fibonacci-number | python best solution (Runtime Beats 94.76% Memory13.8 MB Beats 95.80%) | sahityasetu1996 | 0 | 3 | fibonacci number | 509 | 0.692 | Easy | 9,018 |
https://leetcode.com/problems/fibonacci-number/discuss/2704252/Python-!-DP-!-Simple-Approach | class Solution:
def fib(self, n: int) -> int:
if not n: return 0
seq = [0,1]
for _ in range(1,n):
seq.append(sum(seq))
seq.pop(0)
return seq[1] | fibonacci-number | Python ! DP ! Simple Approach | w7Pratham | 0 | 8 | fibonacci number | 509 | 0.692 | Easy | 9,019 |
https://leetcode.com/problems/fibonacci-number/discuss/2701329/Python3-oror-Simple-Solution-oror-Recursion | class Solution:
def fib(self, n: int) -> int:
def F(n):
if(n == 0 or n==1):
return n
return F(n-1) + F(n-2)
return F(n) | fibonacci-number | Python3 || Simple Solution || Recursion | ahamedmusadiq_-12 | 0 | 3 | fibonacci number | 509 | 0.692 | Easy | 9,020 |
https://leetcode.com/problems/fibonacci-number/discuss/2693865/Python-Simple-Solution-using-Recursion-and-Iterative-method | class Solution:
def fib(self, n: int) -> int:
if ( n== 0 or n==1):
return n
return self.fib(n-1)+ self.fib(n-2)
# another solution
# a =0
# b=1
# for i in range(n):
# a,b = b , a+b
# return a | fibonacci-number | Python Simple Solution using Recursion and Iterative method | Baboolal | 0 | 3 | fibonacci number | 509 | 0.692 | Easy | 9,021 |
https://leetcode.com/problems/fibonacci-number/discuss/2693864/Python-Simple-Solution-using-Recursion-and-Iterative-method | class Solution:
def fib(self, n: int) -> int:
if ( n== 0 or n==1):
return n
return self.fib(n-1)+ self.fib(n-2)
# another solution
# a =0
# b=1
# for i in range(n):
# a,b = b , a+b
# return a | fibonacci-number | Python Simple Solution using Recursion and Iterative method | Baboolal | 0 | 0 | fibonacci number | 509 | 0.692 | Easy | 9,022 |
https://leetcode.com/problems/fibonacci-number/discuss/2693120/python3 | class Solution:
cache = {0: 0, 1: 1}
def fib(self, N: int) -> int:
if N in self.cache:
return self.cache[N]
self.cache[N] = self.fib(N - 1) + self.fib(N - 2)
return self.cache[N] | fibonacci-number | python3 | parryrpy | 0 | 3 | fibonacci number | 509 | 0.692 | Easy | 9,023 |
https://leetcode.com/problems/fibonacci-number/discuss/2682710/python-recursive-approach | class Solution:
def fib(self, n: int) -> int:
if n==1 or n==0:
return n
else :
return self.fib(n-1) + self.fib(n-2) | fibonacci-number | python recursive approach | AMBER_FATIMA | 0 | 4 | fibonacci number | 509 | 0.692 | Easy | 9,024 |
https://leetcode.com/problems/fibonacci-number/discuss/2654480/Python-Solution-Using-Double-Ended-Queue-or-Deque | class Solution:
def fib(self, n: int) -> int:
if n == 0:
return 0
fibs = deque()
fibs.append(0)
fibs.append(1)
for i in range(2, n+1):
fibs.append(fibs[-1] + fibs[-2])
fibs.popleft()
return fibs[-1] | fibonacci-number | Python Solution Using Double Ended Queue | Deque | dodleyandu | 0 | 2 | fibonacci number | 509 | 0.692 | Easy | 9,025 |
https://leetcode.com/problems/fibonacci-number/discuss/2654459/Python-Fibonnaci-or-Extremely-Fast | class Solution:
def fib(self, n: int) -> int:
if n == 0:
return 0
fibs = [0,1]
for i in range(2, n+1):
fibs.append(fibs[-1] + fibs[-2])
return fibs[-1] | fibonacci-number | Python Fibonnaci | Extremely Fast | dodleyandu | 0 | 5 | fibonacci number | 509 | 0.692 | Easy | 9,026 |
https://leetcode.com/problems/find-bottom-left-tree-value/discuss/2680305/Pyhton3-BFS-Solution-oror-BGG | class Solution:
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
q=[[root]]
nodes=[]
while q:
nodes = q.pop(0)
t=[]
for n in nodes:
if n.left:
t.append(n.left)
if n.right:
... | find-bottom-left-tree-value | Pyhton3 BFS Solution || BGG | hoo__mann | 1 | 151 | find bottom left tree value | 513 | 0.665 | Medium | 9,027 |
https://leetcode.com/problems/find-bottom-left-tree-value/discuss/2035070/Why-is-this-a-medium-problem | class Solution:
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
queue = [root]
last = 0
while len(queue):
node = queue.pop(0)
last = node.val
if node.right:
queue.append(node.right)
if node.... | find-bottom-left-tree-value | Why is this a medium problem? | dericktolentino2002 | 1 | 50 | find bottom left tree value | 513 | 0.665 | Medium | 9,028 |
https://leetcode.com/problems/find-bottom-left-tree-value/discuss/869168/Python3-bfs-and-dfs | class Solution:
def findBottomLeftValue(self, root: TreeNode) -> int:
queue = [root]
while queue: # bfs
tmp = []
ans = None
for node in queue:
if ans is None: ans = node.val
if node.left: tmp.append(node.left)
if ... | find-bottom-left-tree-value | [Python3] bfs & dfs | ye15 | 1 | 98 | find bottom left tree value | 513 | 0.665 | Medium | 9,029 |
https://leetcode.com/problems/find-bottom-left-tree-value/discuss/869168/Python3-bfs-and-dfs | class Solution:
def findBottomLeftValue(self, root: TreeNode) -> int:
stack = [(root, 0)]
ii = -1
while stack:
node, i = stack.pop()
if i > ii: ans, ii = node.val, i # left-most node val
if node.right: stack.append((node.right, i+1))
if node.l... | find-bottom-left-tree-value | [Python3] bfs & dfs | ye15 | 1 | 98 | find bottom left tree value | 513 | 0.665 | Medium | 9,030 |
https://leetcode.com/problems/find-bottom-left-tree-value/discuss/690082/Python-Recursive-%2B-Iterative-Solution | class Solution:
def findBottomLeftValue(self, root: TreeNode) -> int:
q = collections.deque()
q.append(root)
bottom_left = root.val
while q:
length = len(q)
for i in range(length):
node = q.popleft()
if i == 0:
... | find-bottom-left-tree-value | Python Recursive + Iterative Solution | pratushah | 1 | 130 | find bottom left tree value | 513 | 0.665 | Medium | 9,031 |
https://leetcode.com/problems/find-bottom-left-tree-value/discuss/690082/Python-Recursive-%2B-Iterative-Solution | class Solution:
def findBottomLeftValue(self, root: TreeNode) -> int:
bottom_left,max_depth = root.val,0
def bottom(root,depth):
nonlocal bottom_left
nonlocal max_depth
if not root:
return None
if depth > max_depth:
max_... | find-bottom-left-tree-value | Python Recursive + Iterative Solution | pratushah | 1 | 130 | find bottom left tree value | 513 | 0.665 | Medium | 9,032 |
https://leetcode.com/problems/find-bottom-left-tree-value/discuss/2727864/Python-Solution-or-Easy-to-Understand | class Solution(object):
def findBottomLeftValue(self, root):
ans = root.val
q = deque()
q.append(root)
while q:
flag = False
for _ in range(len(q)):
p = q.popleft()
if p.left:
if not(flag):
... | find-bottom-left-tree-value | Python Solution | Easy to Understand | its_krish_here | 0 | 5 | find bottom left tree value | 513 | 0.665 | Medium | 9,033 |
https://leetcode.com/problems/find-bottom-left-tree-value/discuss/2465120/BFS-and-DFS-approach-Easy | class Solution(object):
def findBottomLeftValue(self, root):
# dfs solution
left = []
def dfs(node, height):
my_height = height+1
if my_height > len(left):
left.append(node.val)
if node.left:
d... | find-bottom-left-tree-value | BFS and DFS approach Easy | Abhi_009 | 0 | 57 | find bottom left tree value | 513 | 0.665 | Medium | 9,034 |
https://leetcode.com/problems/find-bottom-left-tree-value/discuss/2425785/Python-simple-BFS | class Solution:
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
q = collections.deque()
q.append(root)
while q:
isLast = True # track if the layer is last layer
layer = [] # record current layer
for _ in range(len(q)):
... | find-bottom-left-tree-value | Python simple BFS | scr112 | 0 | 19 | find bottom left tree value | 513 | 0.665 | Medium | 9,035 |
https://leetcode.com/problems/find-bottom-left-tree-value/discuss/2315653/Python3-Simple-level-order-traversal | class Solution:
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
q = deque([root])
while q:
first = None
for i in range(len(q)):
node = q.popleft()
if node:
if first is None:
first = no... | find-bottom-left-tree-value | [Python3] Simple level order traversal | Gp05 | 0 | 13 | find bottom left tree value | 513 | 0.665 | Medium | 9,036 |
https://leetcode.com/problems/find-bottom-left-tree-value/discuss/2022968/Python-easy-understanding-solution-with-comment | class Solution:
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
self.res = root.val
self.deepest_level = 0
def dfs(node, level):
if not (node.left or node.right) and level > self.deepest_level: # We only record the first_v... | find-bottom-left-tree-value | Python easy-understanding solution with comment | byroncharly3 | 0 | 19 | find bottom left tree value | 513 | 0.665 | Medium | 9,037 |
https://leetcode.com/problems/find-bottom-left-tree-value/discuss/1700736/Python3-dfs-solution | class Solution:
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
def dfs(node, depth):
if node.left == None and node.right == None:
return [depth, node.val]
if node.left == None:
return dfs(node.right, depth+1)
if no... | find-bottom-left-tree-value | Python3 dfs solution | ruthwang97 | 0 | 39 | find bottom left tree value | 513 | 0.665 | Medium | 9,038 |
https://leetcode.com/problems/find-bottom-left-tree-value/discuss/1695496/Python-BFS-implementation | class Solution:
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
q = deque([root])
res = []
if not root:
return []
while q:
node_lst = []
for i in range(len(q)):
node = q.popleft()
... | find-bottom-left-tree-value | Python BFS implementation | KoalaKeys | 0 | 42 | find bottom left tree value | 513 | 0.665 | Medium | 9,039 |
https://leetcode.com/problems/find-bottom-left-tree-value/discuss/1579506/Python3-2-logic | class Solution:
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
q = deque([root])
while q:
node = q.popleft()
if node.right: q.append(node.right)
if node.left: q.append(node.left)
... | find-bottom-left-tree-value | Python3 2 logic | Shubham_Muramkar | 0 | 54 | find bottom left tree value | 513 | 0.665 | Medium | 9,040 |
https://leetcode.com/problems/find-bottom-left-tree-value/discuss/1570613/Python-3-simple-recursion-faster-than-99 | class Solution:
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
self.row = 0
self.res = None
def helper(root, cur_row):
if root is None:
return
if cur_row > self.row:
self.row = cur_row
se... | find-bottom-left-tree-value | Python 3 simple recursion faster than 99% | dereky4 | 0 | 79 | find bottom left tree value | 513 | 0.665 | Medium | 9,041 |
https://leetcode.com/problems/find-bottom-left-tree-value/discuss/1522675/Easiest-Solution-Level-Order-Traversal-No-Height | class Solution:
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
queue = [root]
while queue:
node = queue.pop(0)
# We do not have any more items to visit, meaning we hit the end.
if not queue and not node.left and not node.right:
ret... | find-bottom-left-tree-value | Easiest Solution - Level Order Traversal, No Height | rdlcy | 0 | 123 | find bottom left tree value | 513 | 0.665 | Medium | 9,042 |
https://leetcode.com/problems/find-bottom-left-tree-value/discuss/1428561/Python3-Solution-based-on-BFS | class Solution:
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
queue = collections.deque([root])
ans = 0
while queue:
next_lvl = collections.deque([])
ans = queue[0].val
while queue:
node = queue.popleft()
... | find-bottom-left-tree-value | [Python3] Solution based on BFS | maosipov11 | 0 | 34 | find bottom left tree value | 513 | 0.665 | Medium | 9,043 |
https://leetcode.com/problems/find-bottom-left-tree-value/discuss/1159386/Python3-solution-(BFS) | class Solution:
def findBottomLeftValue(self, root: TreeNode) -> int:
queue: Deque[TreeNode] = deque([root])
cur_left: int = root.val
check_left: bool = True
while queue:
for _ in range(len(queue)):
node: TreeNode = queue.popleft()
... | find-bottom-left-tree-value | Python3 solution (BFS) | alexforcode | 0 | 32 | find bottom left tree value | 513 | 0.665 | Medium | 9,044 |
https://leetcode.com/problems/find-bottom-left-tree-value/discuss/957182/Python-or-Easy-Recursion | class Solution:
def __init__(self):
self.count = 0
self.ans = 0
def findBottomLeftValue(self, root: TreeNode) -> int:
def recursion(root, count):
if not root:
return 0
recursion(root.left,count+1)
if count>self.count:
... | find-bottom-left-tree-value | Python | Easy Recursion | bharatgg | 0 | 55 | find bottom left tree value | 513 | 0.665 | Medium | 9,045 |
https://leetcode.com/problems/find-bottom-left-tree-value/discuss/794987/Python-3-solution-99-faster | class Solution:
def findBottomLeftValue(self, root: TreeNode) -> int:
if not root:
return 0
ans = []
queue = deque([root])
while queue:
node = queue.popleft()
ans.append(node.val)
if node.right:
... | find-bottom-left-tree-value | Python 3 solution 99% faster | Geeky-star | 0 | 51 | find bottom left tree value | 513 | 0.665 | Medium | 9,046 |
https://leetcode.com/problems/find-bottom-left-tree-value/discuss/745107/Simple-Python-Recusive-Beats-80-with-Comments! | class Solution:
def findBottomLeftValue(self, root: TreeNode) -> int:
vals = []
def helper(node, level):
# Return when we hit a terminal node
if not node:
return
# Populate the vals list to store the current levels vals if it isn't already prese... | find-bottom-left-tree-value | Simple Python Recusive Beats 80% with Comments! | Pythagoras_the_3rd | 0 | 69 | find bottom left tree value | 513 | 0.665 | Medium | 9,047 |
https://leetcode.com/problems/find-bottom-left-tree-value/discuss/723418/Intuitive-approach-by-using-level-order-iteration | class Solution:
def findBottomLeftValue(self, root: TreeNode) -> int:
Q, tmp = [root], []
while Q:
for n in Q:
if n.left: tmp.append(n.left)
if n.right: tmp.append(n.right)
if tmp:
Q, tmp = tmp, []
... | find-bottom-left-tree-value | Intuitive approach by using level order iteration | puremonkey2001 | 0 | 31 | find bottom left tree value | 513 | 0.665 | Medium | 9,048 |
https://leetcode.com/problems/find-bottom-left-tree-value/discuss/580445/PythonC%2B%2B-O(n)-by-BFS-DFS-w-Hint | class Solution:
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
def finder(node, level):
if not node:
return
if level > finder.level:
finder.bottom_left = node.val
finder.level = level
... | find-bottom-left-tree-value | Python/C++ O(n) by BFS // DFS [w/ Hint ] | brianchiang_tw | 0 | 133 | find bottom left tree value | 513 | 0.665 | Medium | 9,049 |
https://leetcode.com/problems/find-bottom-left-tree-value/discuss/462014/Simple-Python-3 | class Solution:
def findBottomLeftValue(self, root: TreeNode) -> int:
queue = collections.deque([None, root])
res = root.val
while len(queue) > 0:
node = queue.pop()
if node:
if node.left: queue.appendleft(node.left)
if node.right: queue.appendleft(node.right)
elif len(queue) > 0:
res = ... | find-bottom-left-tree-value | Simple Python 3 | Christian_dudu | 0 | 38 | find bottom left tree value | 513 | 0.665 | Medium | 9,050 |
https://leetcode.com/problems/freedom-trail/discuss/1367426/Python3-top-down-dp | class Solution:
def findRotateSteps(self, ring: str, key: str) -> int:
locs = {}
for i, ch in enumerate(ring): locs.setdefault(ch, []).append(i)
@cache
def fn(i, j):
"""Return turns to finish key[j:] startin from ith position on ring."""
if j ==... | freedom-trail | [Python3] top-down dp | ye15 | 2 | 165 | freedom trail | 514 | 0.467 | Hard | 9,051 |
https://leetcode.com/problems/freedom-trail/discuss/2369037/faster-than-96.40-or-python3 | class Solution:
def findRotateSteps(self, ring: str, key: str) -> int:
char_pos = defaultdict(set)
for i, c in enumerate(ring):
char_pos[c].add(i)
def minStep(fromm, to):
if fromm == to:
return 0
minSteps = abs(fromm - to)
... | freedom-trail | faster than 96.40% | python3 | vimla_kushwaha | 1 | 74 | freedom trail | 514 | 0.467 | Hard | 9,052 |
https://leetcode.com/problems/find-largest-value-in-each-tree-row/discuss/1620422/Python-3-easy-dfs-recursive-solution-faster-than-94 | class Solution:
def largestValues(self, root: Optional[TreeNode]) -> List[int]:
res = []
def helper(root, depth):
if root is None:
return
if depth == len(res):
res.append(root.val)
else:
res[depth] = ma... | find-largest-value-in-each-tree-row | Python 3, easy dfs recursive solution, faster than 94% | dereky4 | 1 | 109 | find largest value in each tree row | 515 | 0.646 | Medium | 9,053 |
https://leetcode.com/problems/find-largest-value-in-each-tree-row/discuss/2847905/Python-Basic-BFS | class Solution:
def largestValues(self, root: Optional[TreeNode]) -> List[int]:
q=[]
if root:
q.append(root)
Max=[]
while len(q)>0:
arr=[]
length=len(q)
for i in range(len(q)):
current=q.pop(0)
arr.append... | find-largest-value-in-each-tree-row | Python Basic BFS | cjatherton19 | 0 | 1 | find largest value in each tree row | 515 | 0.646 | Medium | 9,054 |
https://leetcode.com/problems/find-largest-value-in-each-tree-row/discuss/2748994/Level-Order-way-in-Python-and-Golang | class Solution:
def largestValues(self, root: Optional[TreeNode]) -> List[int]:
if root is None:
return []
queue = [root]
level_order = []
while queue:
len_queue = len(queue)
level = []
for _ in range(len_queue):
node = queue.pop(0)
if node.left:
queue.append(node.left)... | find-largest-value-in-each-tree-row | Level Order way in Python and Golang | namashin | 0 | 2 | find largest value in each tree row | 515 | 0.646 | Medium | 9,055 |
https://leetcode.com/problems/find-largest-value-in-each-tree-row/discuss/2576796/Python3-Easy-to-Understand | class Solution:
def largestValues(self, root: Optional[TreeNode]) -> List[int]:
values = {}
q = deque()
q.append((root, 0))
while q:
node, depth = q.popleft()
if not node:
continue
if ... | find-largest-value-in-each-tree-row | Python3 Easy to Understand | Mbarberry | 0 | 8 | find largest value in each tree row | 515 | 0.646 | Medium | 9,056 |
https://leetcode.com/problems/find-largest-value-in-each-tree-row/discuss/2460015/python-simple-solution-using-BFS | class Solution:
def largestValues(self, root: Optional[TreeNode]) -> List[int]:
res = []
if(root == None):
return res
q = deque()
q.append(root)
while(q):
sz = len(q)
MIN_INT = -sys.maxsize - 1
mini = MIN_INT
for i ... | find-largest-value-in-each-tree-row | python simple solution using BFS | rajitkumarchauhan99 | 0 | 18 | find largest value in each tree row | 515 | 0.646 | Medium | 9,057 |
https://leetcode.com/problems/find-largest-value-in-each-tree-row/discuss/2257615/Python-BFS-Easy-to-Understand | class Solution:
def bfs(self,root):
if(root==None):
return []
level=0
queue=deque([(root,0)])
visited=set()
hashmap={}
while queue:
currentElement,level=queue.popleft()
if(currentElement not in visited):
visited.add(... | find-largest-value-in-each-tree-row | Python BFS - Easy to Understand | algorasm | 0 | 32 | find largest value in each tree row | 515 | 0.646 | Medium | 9,058 |
https://leetcode.com/problems/find-largest-value-in-each-tree-row/discuss/1944774/Python-BFS-solution-using-deque | class Solution:
def largestValues(self, root: Optional[TreeNode]) -> List[int]:
if not root:
return None
deq = collections.deque([root])
res = []
while deq:
m = -sys.maxsize
for i in range(len(deq)):
n... | find-largest-value-in-each-tree-row | Python BFS solution using deque | byroncharly3 | 0 | 49 | find largest value in each tree row | 515 | 0.646 | Medium | 9,059 |
https://leetcode.com/problems/find-largest-value-in-each-tree-row/discuss/1844705/Python3-Solution-easy-to-understand | class Solution:
def largestValues(self, root: Optional[TreeNode]) -> List[int]:
if root is None:
return []
ans = []
queue = [root]
while len(queue) > 0:
size = len(queue)
i = 0
max_val = float("-inf")
while i < size:
... | find-largest-value-in-each-tree-row | Python3 Solution easy to understand | piyushkumarpiyushkumar6 | 0 | 28 | find largest value in each tree row | 515 | 0.646 | Medium | 9,060 |
https://leetcode.com/problems/find-largest-value-in-each-tree-row/discuss/1684170/Python-BFS-Level-Order-Traversal | class Solution:
def largestValues(self, root: Optional[TreeNode]) -> List[int]:
# If Root is Null
if not root:
return []
queue = []
res = []
queue.append(root)
# While Queue is not empty
while queue:
rowMax =... | find-largest-value-in-each-tree-row | Python BFS Level Order Traversal | pillaimanish | 0 | 69 | find largest value in each tree row | 515 | 0.646 | Medium | 9,061 |
https://leetcode.com/problems/find-largest-value-in-each-tree-row/discuss/1427593/Python3-Clean-solution-with-using-bfs | class Solution:
def largestValues(self, root: Optional[TreeNode]) -> List[int]:
res = []
if root == None:
return res
queue = collections.deque([root])
while queue:
cur_max = -float('inf')
next_lvl = collections.deque(... | find-largest-value-in-each-tree-row | [Python3] Clean solution with using bfs | maosipov11 | 0 | 75 | find largest value in each tree row | 515 | 0.646 | Medium | 9,062 |
https://leetcode.com/problems/find-largest-value-in-each-tree-row/discuss/1257022/Python3-BFS | class Solution:
def largestValues(self, root: TreeNode) -> List[int]:
if not root:
return []
queue = [root]
level = []
res = []
while queue:
nextlevel = []
for node in queue:
level.append(node.val)
... | find-largest-value-in-each-tree-row | Python3 BFS | rahulkp220 | 0 | 77 | find largest value in each tree row | 515 | 0.646 | Medium | 9,063 |
https://leetcode.com/problems/find-largest-value-in-each-tree-row/discuss/1036216/Python-Intuitive-or-Beats-90-or-BFS | class Solution:
def largestValues(self, root: TreeNode) -> List[int]:
output = []
if not root:
return output
queue = [root]
while queue:
output.append(-2**31)
node = queue[0]
# size = len(queue)
for j i... | find-largest-value-in-each-tree-row | [Python] Intuitive | Beats 90% | BFS | May-i-Code | 0 | 116 | find largest value in each tree row | 515 | 0.646 | Medium | 9,064 |
https://leetcode.com/problems/find-largest-value-in-each-tree-row/discuss/869211/Python3-bfs-and-dfs | class Solution:
def largestValues(self, root: TreeNode) -> List[int]:
if not root: return [] # edge case
ans = []
queue = [root]
while queue:
ans.append(-inf)
tmp = []
for node in queue:
ans[-1] = max(ans[-1], node.val)
... | find-largest-value-in-each-tree-row | [Python3] bfs & dfs | ye15 | 0 | 56 | find largest value in each tree row | 515 | 0.646 | Medium | 9,065 |
https://leetcode.com/problems/find-largest-value-in-each-tree-row/discuss/869211/Python3-bfs-and-dfs | class Solution:
def largestValues(self, root: TreeNode) -> List[int]:
ans = []
stack = [(root, 0)]
while stack:
node, i = stack.pop()
if node:
if i == len(ans): ans.append(node.val)
else: ans[i] = max(ans[i], node.val)
... | find-largest-value-in-each-tree-row | [Python3] bfs & dfs | ye15 | 0 | 56 | find largest value in each tree row | 515 | 0.646 | Medium | 9,066 |
https://leetcode.com/problems/find-largest-value-in-each-tree-row/discuss/773511/Python-3-DFS | class Solution:
def largestValues(self, root: TreeNode) -> List[int]:
d = collections.defaultdict(lambda : -sys.maxsize)
def dfs(node, level):
if not node: return
if node.val > d[level]:
d[level] = node.val
if node.left:
dfs(no... | find-largest-value-in-each-tree-row | Python 3 DFS | idontknoooo | 0 | 79 | find largest value in each tree row | 515 | 0.646 | Medium | 9,067 |
https://leetcode.com/problems/find-largest-value-in-each-tree-row/discuss/698722/python3-ez-to-understand-BFS-w-explanation | class Solution:
def largestValues(self, root):
"""
44ms 88.31%
15.8MB 94.12%
:type root: TreeNode
:rtype: List[int]
"""
if not root:return []
ans=[]
queue=[root]
while queue or stack:
stack=[]
curr=float('-inf')
... | find-largest-value-in-each-tree-row | python3 ez to understand BFS w explanation | 752937603 | 0 | 57 | find largest value in each tree row | 515 | 0.646 | Medium | 9,068 |
https://leetcode.com/problems/find-largest-value-in-each-tree-row/discuss/314553/Level-order-dfs-python3 | class Solution:
def largestValues(self, root: TreeNode) -> List[int]:
levels = {}
def dfs(node, level):
if not node:return
if level not in levels.keys():levels[level] = [node.val]
else:levels[level].append(node.val)
if node.right:dfs(node.right, level+... | find-largest-value-in-each-tree-row | Level order dfs python3 | aj_to_rescue | 0 | 83 | find largest value in each tree row | 515 | 0.646 | Medium | 9,069 |
https://leetcode.com/problems/longest-palindromic-subsequence/discuss/1968323/Python-3-Approaches-(Recursion-%2B-Memoization-%2B-DP) | class Solution:
def longestPalindromeSubseq(self, s: str) -> int:
return self.get_longest_subseq(0, len(s)-1, s)
def get_longest_subseq(self, start, end, s):
"""
method used to find the longest palindrome subsequence in a string
start: start index of the string
... | longest-palindromic-subsequence | Python - 3 Approaches (Recursion + Memoization + DP) | superGloria | 6 | 225 | longest palindromic subsequence | 516 | 0.607 | Medium | 9,070 |
https://leetcode.com/problems/longest-palindromic-subsequence/discuss/1968323/Python-3-Approaches-(Recursion-%2B-Memoization-%2B-DP) | class Solution:
def longestPalindromeSubseq(self, s: str) -> int:
# add 2d list memo to avoid redundant calculation
self.memo = [[0 for _ in range(len(s))] for _ in range(len(s))]
return self.get_longest_subseq(0, len(s)-1, s)
def get_longest_subseq(self, start, end, s):
"""... | longest-palindromic-subsequence | Python - 3 Approaches (Recursion + Memoization + DP) | superGloria | 6 | 225 | longest palindromic subsequence | 516 | 0.607 | Medium | 9,071 |
https://leetcode.com/problems/longest-palindromic-subsequence/discuss/1968323/Python-3-Approaches-(Recursion-%2B-Memoization-%2B-DP) | class Solution:
def longestPalindromeSubseq(self, s: str) -> int:
# use dp approach
# create a 2d array to store the result
L = [ [0 for _ in range(len(s))] for _ in range(len(s)) ]
# initialize the first row and column: strings of length 1 are palindromes of length 1
for i in r... | longest-palindromic-subsequence | Python - 3 Approaches (Recursion + Memoization + DP) | superGloria | 6 | 225 | longest palindromic subsequence | 516 | 0.607 | Medium | 9,072 |
https://leetcode.com/problems/longest-palindromic-subsequence/discuss/1499854/Python-Recursion-%2B-Memoization-or-DP | class Solution:
def longestPalindromeSubseq(self, s: str) -> int:
def dfs(l= 0, r= len(s)-1):
if l > r:
return 0
elif l == r:
return 1
if (l, r) in cache:
return cache[(l, r)]
if s[l] == s[r]:
... | longest-palindromic-subsequence | [Python] Recursion + Memoization | DP | soma28 | 4 | 387 | longest palindromic subsequence | 516 | 0.607 | Medium | 9,073 |
https://leetcode.com/problems/longest-palindromic-subsequence/discuss/1499854/Python-Recursion-%2B-Memoization-or-DP | class Solution:
def longestPalindromeSubseq(self, s: str) -> int:
N = len(s)
dp = [[0 for _ in range(N)] for _ in range(N)]
for i in range(N):
dp[i][i] = 1
for r in range(N-2, -1, -1):
for c in range(r+1, N):
if s[r] == s[c]:
... | longest-palindromic-subsequence | [Python] Recursion + Memoization | DP | soma28 | 4 | 387 | longest palindromic subsequence | 516 | 0.607 | Medium | 9,074 |
https://leetcode.com/problems/longest-palindromic-subsequence/discuss/1601895/Simple-python-recursive-or-with-comments | class Solution:
def longestPalindromeSubseq(self, s: str):
cache = [[None for _ in s] for _ in s]
return self.lps_helper(s, cache, 0, len(s)-1)
def lps_helper(self, s, cache, start, end):
"""
- if the characters at the ends match,
we dive deeper and add 2 to the re... | longest-palindromic-subsequence | Simple python recursive | with comments | paulonteri | 2 | 266 | longest palindromic subsequence | 516 | 0.607 | Medium | 9,075 |
https://leetcode.com/problems/longest-palindromic-subsequence/discuss/989586/Simple-python-solution-using-DP | class Solution:
def longestPalindromeSubseq(self, s):
l_p_ss = [["" for i in s] for j in s]
# single length charecters
for index, val in enumerate(s):
l_p_ss[index][index] = val
# c_l is current length
# s_i is starting index
# e_i is ending index... | longest-palindromic-subsequence | Simple python solution using DP | nth-attempt | 2 | 323 | longest palindromic subsequence | 516 | 0.607 | Medium | 9,076 |
https://leetcode.com/problems/longest-palindromic-subsequence/discuss/1681886/Python3-Solution-or-3-line-code | class Solution:
@lru_cache(None)
def longestPalindromeSubseq(self, s: str) -> int:
if len(s)<=1 : return len(s)
if s[0]==s[-1] : return self.longestPalindromeSubseq(s[1:-1]) + 2
return max(self.longestPalindromeSubseq(s[1:]),self.longestPalindromeSubseq(s[:-1])) | longest-palindromic-subsequence | Python3 Solution | 3 line code | satyam2001 | 1 | 77 | longest palindromic subsequence | 516 | 0.607 | Medium | 9,077 |
https://leetcode.com/problems/longest-palindromic-subsequence/discuss/872757/Python3-%22interval%22-dp | class Solution:
def longestPalindromeSubseq(self, s: str) -> int:
@lru_cache(None)
def fn(lo, hi):
"""Return longest palindromic subsequence."""
if lo >= hi: return int(lo == hi)
if s[lo] == s[hi]: return fn(lo+1, hi-1) + 2
return max(fn(lo+1... | longest-palindromic-subsequence | [Python3] "interval" dp | ye15 | 1 | 91 | longest palindromic subsequence | 516 | 0.607 | Medium | 9,078 |
https://leetcode.com/problems/longest-palindromic-subsequence/discuss/872757/Python3-%22interval%22-dp | class Solution:
def longestPalindromeSubseq(self, s: str) -> int:
@lru_cache(None)
def lcs(i, j):
"""Return longest common subsequence of text1[i:] and text2[j:]."""
if i == len(s) or j == len(s): return 0
if s[i] == s[~j]: return 1 + lcs(i+1, j+1)
... | longest-palindromic-subsequence | [Python3] "interval" dp | ye15 | 1 | 91 | longest palindromic subsequence | 516 | 0.607 | Medium | 9,079 |
https://leetcode.com/problems/longest-palindromic-subsequence/discuss/872757/Python3-%22interval%22-dp | class Solution:
def longestPalindromeSubseq(self, s: str) -> int:
n = len(s)
dp = [[0]*n for _ in range(n)]
for i in reversed(range(n)):
dp[i][i] = 1
for j in range(i+1, n):
if s[i] == s[j]: dp[i][j] = 2 + dp[i+1][j-1]
else: dp[i][j] ... | longest-palindromic-subsequence | [Python3] "interval" dp | ye15 | 1 | 91 | longest palindromic subsequence | 516 | 0.607 | Medium | 9,080 |
https://leetcode.com/problems/longest-palindromic-subsequence/discuss/2835509/From-BruteForce-to-Optimized-Bottom-up | class Solution:
def longestPalindromeSubseq(self, s: str) -> int:
@cache
def dp(left,right):
if left==right:
return 1
if left > right:
return 0
count = 0
if s[left]==s[right]:
count+=dp(left+1,right-1)
... | longest-palindromic-subsequence | โ
โ
From BruteForce to Optimized Bottom-up | teyouale | 0 | 1 | longest palindromic subsequence | 516 | 0.607 | Medium | 9,081 |
https://leetcode.com/problems/longest-palindromic-subsequence/discuss/2835509/From-BruteForce-to-Optimized-Bottom-up | class Solution:
def longestPalindromeSubseq(self, s: str) -> int:
n = len(s)
dp = [[0]*n for i in range(n)]
longestPalindromeS = 1
for start in range(n-1,-1,-1):
dp[start][start]=1
for end in range(start+1,n):
if s[start]==s[end]:
... | longest-palindromic-subsequence | โ
โ
From BruteForce to Optimized Bottom-up | teyouale | 0 | 1 | longest palindromic subsequence | 516 | 0.607 | Medium | 9,082 |
https://leetcode.com/problems/longest-palindromic-subsequence/discuss/2835509/From-BruteForce-to-Optimized-Bottom-up | class Solution:
def longestPalindromeSubseq(self, s: str) -> int:
n = len(s)
dp = [0]*n
dp[-1]=1
longestPalindromeS = 1
for start in range(n-1,-1,-1):
curr = [0]*n
curr[start]=1
for end in range(start+1,n):
if s[start]==s[e... | longest-palindromic-subsequence | โ
โ
From BruteForce to Optimized Bottom-up | teyouale | 0 | 1 | longest palindromic subsequence | 516 | 0.607 | Medium | 9,083 |
https://leetcode.com/problems/longest-palindromic-subsequence/discuss/2599440/Simple-Solution-Using-LCS-oror-Python3-oror-DP-Tabluar-method | class Solution:
def longestPalindromeSubseq(self, s: str) -> int:
def lcs(x,y,n,m):
dp = [[-1 for _ in range(m+1)] for _ in range(n+1)]
for i in range(n+1):
for j in range(m+1):
if i==0 or j==0:
dp[i][j] = 0
... | longest-palindromic-subsequence | Simple Solution Using LCS || Python3 || DP Tabluar method | ajinkyabhalerao11 | 0 | 18 | longest palindromic subsequence | 516 | 0.607 | Medium | 9,084 |
https://leetcode.com/problems/longest-palindromic-subsequence/discuss/2544459/Python-DP-Solution | class Solution:
def longestPalindromeSubseq(self, s: str) -> int:
n=len(s)
dp=[[0 for i in range(n)]for j in range(n)]
maxx=1
for i in range(n):
dp[i][i]=1
for end in range(1,n):
for start in range(end-1,-1,-1):
... | longest-palindromic-subsequence | Python DP Solution | Prithiviraj1927 | 0 | 136 | longest palindromic subsequence | 516 | 0.607 | Medium | 9,085 |
https://leetcode.com/problems/longest-palindromic-subsequence/discuss/2534918/Clean-Fast-Python3-or-DP%3A-LCS-With-Reversed-String | class Solution:
def longestPalindromeSubseq(self, s: str) -> int:
n, rev = len(s), s[::-1]
mem = [[0] * (n+1) for _ in range(n+1)]
for i in range(n):
for j in range(n):
if s[i] == rev[j]:
mem[i+1][j+1] = 1 + mem[i][j]
else:
... | longest-palindromic-subsequence | Clean, Fast Python3 | DP: LCS With Reversed String | ryangrayson | 0 | 16 | longest palindromic subsequence | 516 | 0.607 | Medium | 9,086 |
https://leetcode.com/problems/longest-palindromic-subsequence/discuss/2461810/Simple-DP-solution-with-Python-3 | class Solution:
def longestPalindromeSubseq(self, s: str) -> int:
start, end = 0, len(s)-1
dp = [[-1 for j in range(len(s))]for i in range(len(s))]
def driver(s, start, end, dp):
if start > end:
return 0
if dp[start... | longest-palindromic-subsequence | Simple DP solution with Python 3 | Gilbert770 | 0 | 93 | longest palindromic subsequence | 516 | 0.607 | Medium | 9,087 |
https://leetcode.com/problems/longest-palindromic-subsequence/discuss/2328623/Python3-Solution-with-using-dp | class Solution:
def longestPalindromeSubseq(self, s: str) -> int:
dp = [[0] * len(s) for _ in range(len(s))]
for i in range(len(s) - 1, -1, -1):
dp[i][i] = 1
for j in range(i + 1, len(s)):
if s[i] == s[j]:
dp[i][j] = dp[i + 1][j -... | longest-palindromic-subsequence | [Python3] Solution with using dp | maosipov11 | 0 | 49 | longest palindromic subsequence | 516 | 0.607 | Medium | 9,088 |
https://leetcode.com/problems/longest-palindromic-subsequence/discuss/2289573/Brute-to-DP%3A-Simple-Python()-Solution-oror-Easy-to-Understand | class Solution:
def solve(self, l, r, s, dp):
if l > r:
return 0
if l == r: return 1
if s[l] == s[r]:
return 2 + self.solve(l+1, r-1, s)
else:
return max(self.solve(l+1, r, s),self.solve(l, r-1, s))
def longestPalindromeSubseq(self, s: s... | longest-palindromic-subsequence | Brute to DP: Simple Python(๐) Solution || Easy to Understand | yoayushraj99 | 0 | 19 | longest palindromic subsequence | 516 | 0.607 | Medium | 9,089 |
https://leetcode.com/problems/longest-palindromic-subsequence/discuss/2289573/Brute-to-DP%3A-Simple-Python()-Solution-oror-Easy-to-Understand | class Solution:
def solve(self, l, r, s, dp):
if l > r:
return 0
if l == r:
dp[l][r] = 1
if dp[l][r] != 1001:
return dp[l][r]
if s[l] == s[r]:
dp[l][r] = 2 + self.solve(l+1, r-1, s, dp)
return dp[l][r]
else... | longest-palindromic-subsequence | Brute to DP: Simple Python(๐) Solution || Easy to Understand | yoayushraj99 | 0 | 19 | longest palindromic subsequence | 516 | 0.607 | Medium | 9,090 |
https://leetcode.com/problems/longest-palindromic-subsequence/discuss/2289573/Brute-to-DP%3A-Simple-Python()-Solution-oror-Easy-to-Understand | class Solution:
def longestPalindromeSubseq(self, s: str) -> int:
n = len(s)
dp = [[0]*(n) for _ in range(n)]
for i in range(n-1, -1, -1):
dp[i][i] = 1
for j in range(i+1, n):
if s[i] == s[j]:
dp[i][j] = 2 + dp[i+1][j-1]
... | longest-palindromic-subsequence | Brute to DP: Simple Python(๐) Solution || Easy to Understand | yoayushraj99 | 0 | 19 | longest palindromic subsequence | 516 | 0.607 | Medium | 9,091 |
https://leetcode.com/problems/longest-palindromic-subsequence/discuss/2086661/Python-Solution-oror-DP | class Solution:
def longestPalindromeSubseq(self, s: str) -> int:
a=s
b=s[::-1]
m=len(s)
n=len(s)
dp =([[0 for i in range(n + 1)] for i in range(m + 1)])
for i in range(m+1):
for j in range(n+1):
if i==0 or j==0:
dp[i][j... | longest-palindromic-subsequence | Python Solution || DP | a_dityamishra | 0 | 128 | longest palindromic subsequence | 516 | 0.607 | Medium | 9,092 |
https://leetcode.com/problems/longest-palindromic-subsequence/discuss/1807535/Python-easy-to-read-and-understand-or-DP | class Solution:
def lcs(self, a, b):
m, n = len(a), len(b)
t = [[0 for _ in range(n+1)] for _ in range(m+1)]
for i in range(1, m+1):
for j in range(1, n+1):
if a[i-1] == b[j-1]:
t[i][j] = t[i-1][j-1] + 1
else:
... | longest-palindromic-subsequence | Python easy to read and understand | DP | sanial2001 | 0 | 155 | longest palindromic subsequence | 516 | 0.607 | Medium | 9,093 |
https://leetcode.com/problems/longest-palindromic-subsequence/discuss/1519086/Python-DP-with-space-O(n2)-and-its-O(n)-improvement-solution-with-comments | class Solution:
def longestPalindromeSubseq(self, s: str) -> int:
dp = [[0 for _ in range(len(s))] for _ in range(len(s))]
for k in range(1, len(s) + 1):
for i in range(len(s) - k + 1):
j = k + i - 1
if i == j:
dp[i][j] = 1
... | longest-palindromic-subsequence | Python DP with space O(n^2) and its O(n) improvement solution with comments | Huayra | 0 | 180 | longest palindromic subsequence | 516 | 0.607 | Medium | 9,094 |
https://leetcode.com/problems/longest-palindromic-subsequence/discuss/1519086/Python-DP-with-space-O(n2)-and-its-O(n)-improvement-solution-with-comments | class Solution:
def longestPalindromeSubseq(self, s: str) -> int:
# dp = [[0 for _ in range(len(s))] for _ in range(len(s))]
dp0 = [0] * len(s)
dp1 = [0] * len(s)
dp2 = [0] * len(s)
for k in range(1, len(s) + 1):
for i in range(len(s) - k + 1):
... | longest-palindromic-subsequence | Python DP with space O(n^2) and its O(n) improvement solution with comments | Huayra | 0 | 180 | longest palindromic subsequence | 516 | 0.607 | Medium | 9,095 |
https://leetcode.com/problems/longest-palindromic-subsequence/discuss/1409372/Python-DP-solution-with-explanation | class Solution:
def longestPalindromeSubseq(self, s: str) -> int:
reversed_s = s[::-1]
return self.lcs(s,reversed_s,len(s),len(reversed_s))
def lcs(self,x,y,n,m):
t = [[0 for p in range(0,m+1)]
for q in range(0,n+1)]
for a in range(0,n+1... | longest-palindromic-subsequence | Python DP solution with explanation | Saura_v | 0 | 342 | longest palindromic subsequence | 516 | 0.607 | Medium | 9,096 |
https://leetcode.com/problems/longest-palindromic-subsequence/discuss/1392723/Easy-to-follow-Python-solution-using-DP | class Solution:
def longestPalindromeSubseq(self, s: str) -> int:
def create_palindrome(start: int, end: int, cache: Dict[Tuple[int, int], int]) -> int:
if end < start:
return 0
elif start == end:
return 1
elif (... | longest-palindromic-subsequence | Easy-to-follow Python solution using DP | mlalma | 0 | 175 | longest palindromic subsequence | 516 | 0.607 | Medium | 9,097 |
https://leetcode.com/problems/longest-palindromic-subsequence/discuss/1025483/Python3-Recursive-solution-w-cache | class Solution:
def longestPalindromeSubseq(self, s: str) -> int:
memo = {}
def helper(s: str, i: int, j:int, memo: dict):
if (i,j) in memo:
return memo[(i,j)]
if i==j:
memo[(i,j)] = 1
return 1
... | longest-palindromic-subsequence | [Python3] Recursive solution w/ cache | charlie11 | 0 | 71 | longest palindromic subsequence | 516 | 0.607 | Medium | 9,098 |
https://leetcode.com/problems/super-washing-machines/discuss/1494245/Python3-greedy | class Solution:
def findMinMoves(self, machines: List[int]) -> int:
total = sum(machines)
if total % len(machines): return -1 # impossible
avg = total // len(machines)
ans = prefix = 0
for i, x in enumerate(machines):
ans = max(ans, abs(prefix), x - avg... | super-washing-machines | [Python3] greedy | ye15 | 1 | 138 | super washing machines | 517 | 0.401 | Hard | 9,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.