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/permutation-in-string/discuss/2713363/Python-Solution-100-EXPLAINED-line-by-line | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
n=len(s1) #length of the string s1
m=len(s2) #length of the string s2
ans=0
d={}
for i in s1: #storing the frquency of every element of s1 in dictionary d
if i in d:
d[i]+=1
... | permutation-in-string | Python Solution - 100% EXPLAINED line by line✔ | T1n1_B0x1 | 0 | 6 | permutation in string | 567 | 0.437 | Medium | 10,000 |
https://leetcode.com/problems/permutation-in-string/discuss/2664566/Python-solution-with-O(n)-time-complexity | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
from collections import Counter
n = len(s2)
m = len(s1)
base = dict(Counter(s1))
window = {}
start = 0
for i, c in enumerate(s2):
if c not in base:
... | permutation-in-string | Python solution with O(n) time complexity | datamut | 0 | 22 | permutation in string | 567 | 0.437 | Medium | 10,001 |
https://leetcode.com/problems/permutation-in-string/discuss/2661060/Python3-Sliding-window | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
if len(s1) <= len(s2):
counter = Counter(s1)
for i in range(len(s1)):
counter[s2[i]] -= 1
for i in range(len(s1), len(s2)):
if all([counter[key] == 0 for key ... | permutation-in-string | Python3 Sliding window | xiangyupeng1994 | 0 | 4 | permutation in string | 567 | 0.437 | Medium | 10,002 |
https://leetcode.com/problems/permutation-in-string/discuss/2634970/Python3-SLIDING-WINDOW-or-EASY-TO-UNDERSTAND-or-USING-SORTING | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
#starting and endings of window
opene = 0
close = len(s1)
#sorted s1
sorted_s1 = "".join(sorted(s1))
while close <= len(s2):
#if sorted window portion is equal to sorted_s1
if "".join(sorted(s2[opene:close])) == sorted_s1:
retur... | permutation-in-string | [Python3] SLIDING WINDOW | EASY TO UNDERSTAND | USING SORTING | mayank0936 | 0 | 24 | permutation in string | 567 | 0.437 | Medium | 10,003 |
https://leetcode.com/problems/permutation-in-string/discuss/2624939/python-or-sliding-window-or-o(s-%2B-t) | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
# initialize two move pointers, length of s1 and s2, valid number
left, right, s1_length, s2_length, valid = 0, 0, len(s1), len(s2), 0
# initilize the dictionary of window and target string
window, counter =... | permutation-in-string | python | sliding window | o(s + t) | MichelleZou | 0 | 13 | permutation in string | 567 | 0.437 | Medium | 10,004 |
https://leetcode.com/problems/permutation-in-string/discuss/2562092/Python-or-ShortCode-%2B-Cooments-or-Sliding-window-%2B-Hashmap-or-O(N) | class Solution(object):
def checkInclusion(self, s1, s2):
letter_count, letter_count_s1, start, end, len_s1 = collections.Counter(''), collections.Counter(s1), 0, 0, len(s1)
# letter_count_s1 contains count of all characters in s1
while(end<len(s2)):
if letter_count_s1[s2[end]] > 0: # if cur... | permutation-in-string | Python | ShortCode + Cooments | Sliding window + Hashmap | O(N) | vs36 | 0 | 63 | permutation in string | 567 | 0.437 | Medium | 10,005 |
https://leetcode.com/problems/permutation-in-string/discuss/2550011/Python3-Sliding-window-easy-understand | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
left, right = 0,0
valid = 0
need = {key: 0 for key in s1}
window = {key: 0 for key in s2}
for i in s1:
need[i] +=1
while right < len(s2):
c = s2[right]
... | permutation-in-string | [Python3] Sliding window easy understand | gj1994 | 0 | 89 | permutation in string | 567 | 0.437 | Medium | 10,006 |
https://leetcode.com/problems/permutation-in-string/discuss/2544148/SLIDING-WINDOW-SOLUTION | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
# Get the count of values and their frequency in s1
# Get the count of values and their frequency in s2 with length s1
# Check if the two frequencies are the same and return True else return False
# Slide the windows... | permutation-in-string | SLIDING WINDOW SOLUTION | leomensah | 0 | 19 | permutation in string | 567 | 0.437 | Medium | 10,007 |
https://leetcode.com/problems/permutation-in-string/discuss/2541564/Python-Sliding-window-Optimized-python3-96-faster | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
d1={}
d2={}
if(len(s1)>len(s2)):
return False
sl = len(s1)
left = 0
right = sl-1
for i in s1:
if(i not in d1):
d1[i] = 1
else:
... | permutation-in-string | Python Sliding window Optimized [python3] 96% faster | krushangSk17 | 0 | 24 | permutation in string | 567 | 0.437 | Medium | 10,008 |
https://leetcode.com/problems/subtree-of-another-tree/discuss/265239/Python-Easy-to-Understand | class Solution:
def isSubtree(self, s: TreeNode, t: TreeNode) -> bool:
if not s:
return False
if self.isSameTree(s, t):
return True
return self.isSubtree(s.left, t) or self.isSubtree(s.right, t)
def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
if... | subtree-of-another-tree | Python Easy to Understand | ccparamecium | 83 | 13,100 | subtree of another tree | 572 | 0.46 | Easy | 10,009 |
https://leetcode.com/problems/subtree-of-another-tree/discuss/1199455/Python-Tree-Hash | class Solution:
def isSubtree(self, root: TreeNode, subRoot: TreeNode) -> bool:
def f(root):
if not root:
return "L"
left_end = "$" if not root.left else ""
right_end = "#" if not root.right else ""
return left_end + "|" + f(root.left) + "-" +... | subtree-of-another-tree | Python, Tree Hash 🌲#️⃣ | yasir991925 | 6 | 686 | subtree of another tree | 572 | 0.46 | Easy | 10,010 |
https://leetcode.com/problems/subtree-of-another-tree/discuss/1868923/Easy-understanding-Python-code-with-detailed-explanation-for-beginner | class Solution:
def same(self, root, subroot):
if not root and not subroot:
return True
if not root or not subroot:
return False
return root.val == subroot.val and self.same(root.left, subroot.left) and self.same(root.right, subroot.right)
def checksubtree(se... | subtree-of-another-tree | Easy-understanding Python code with detailed explanation for beginner | jlu56 | 4 | 363 | subtree of another tree | 572 | 0.46 | Easy | 10,011 |
https://leetcode.com/problems/subtree-of-another-tree/discuss/1807904/Python3-oror-Neat-Clean-and-Fast-Code-oror-TC%3A-O(n)-SC%3AO(1) | class Solution:
def isSubtree(self, root: Optional[TreeNode], subroot: Optional[TreeNode]) -> bool:
def subchk(root, subroot):
if not root and not subroot: return True
elif not root or not subroot: return False
if root.val == subroot.val:
... | subtree-of-another-tree | Python3 || Neat - Clean and Fast Code || TC: O(n) SC:O(1) | Dewang_Patil | 4 | 447 | subtree of another tree | 572 | 0.46 | Easy | 10,012 |
https://leetcode.com/problems/subtree-of-another-tree/discuss/1523520/Python3-recursive-solution | class Solution:
def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool:
if not root:
return
def check(r,s):
if not s and not r:
return True
if not s or not r:
return False
if r.val == s.val an... | subtree-of-another-tree | Python3 recursive solution | EklavyaJoshi | 4 | 376 | subtree of another tree | 572 | 0.46 | Easy | 10,013 |
https://leetcode.com/problems/subtree-of-another-tree/discuss/1248309/python-Easiest-solution(with-comments) | class Solution:
def isSubtree(self, root: TreeNode, subRoot: TreeNode) -> bool:
def dfs(root,subroot):
#if both are none
if not root and not subroot:
return True
#if any one is none
if not root or not subr... | subtree-of-another-tree | [python] Easiest solution(with comments) | _kumarmohit_ | 4 | 554 | subtree of another tree | 572 | 0.46 | Easy | 10,014 |
https://leetcode.com/problems/subtree-of-another-tree/discuss/1678398/KMP-O(N%2BM) | class Solution:
def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool:
def inorder(root):
if root:
return inorder(root.left) + str(root.val) + inorder(root.right)
return "#"
def preorder(root):
if root:
... | subtree-of-another-tree | KMP O(N+M) | ismaj | 1 | 238 | subtree of another tree | 572 | 0.46 | Easy | 10,015 |
https://leetcode.com/problems/subtree-of-another-tree/discuss/1592921/Python-easy-Recursive-solution-with-Explanation. | class Solution:
def isSubtree(self, root, subRoot) :
if not subRoot: return True
if not root : return False
if self.sameTree(root , subRoot):
return True
return (self.isSubtree(root.left , subRoot) or self.isSubtree(root.right , subRoot) )
d... | subtree-of-another-tree | Python easy Recursive solution with Explanation. | Into_You | 1 | 227 | subtree of another tree | 572 | 0.46 | Easy | 10,016 |
https://leetcode.com/problems/subtree-of-another-tree/discuss/1520019/Python3-dfs-and-hash | class Solution:
def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool:
def fn(x, y):
"""Return True if a match is found."""
if not x or not y: return x is y
return x.val == y.val and fn(x.left, y.left) and fn(x.right, y.right)
... | subtree-of-another-tree | [Python3] dfs & hash | ye15 | 1 | 184 | subtree of another tree | 572 | 0.46 | Easy | 10,017 |
https://leetcode.com/problems/subtree-of-another-tree/discuss/1490526/python-pre-order-traversal-64ms-beat-98.5 | class Solution:
#--------------traversal-------------#
def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool:
root_list = []
subroot_list = []
self.pre_order(root, root_list)
self.pre_order(subRoot, subroot_list)
return "".join(subroot_list) i... | subtree-of-another-tree | python pre-order traversal, 64ms beat 98.5% | guardianwang | 1 | 300 | subtree of another tree | 572 | 0.46 | Easy | 10,018 |
https://leetcode.com/problems/subtree-of-another-tree/discuss/1410574/Self-understandable-code | class Solution:
def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool:
def isIdentical(root1,root2) :
if not root1 and not root2 :
return True
if ( not root1 and root2 ) or (not root2 and root1 ):
retu... | subtree-of-another-tree | Self understandable code | abhijeetgupto | 1 | 176 | subtree of another tree | 572 | 0.46 | Easy | 10,019 |
https://leetcode.com/problems/subtree-of-another-tree/discuss/1250026/Easy-%2B-Straightforward-Python-Recursion | class Solution:
def isSubtree(self, root: TreeNode, subRoot: TreeNode) -> bool:
def same_tree(p, q):
if not p and not q:
return True
if not p or not q or p.val != q.val:
return False
return same_tree(q.left, p.left) and same_tree(q... | subtree-of-another-tree | Easy + Straightforward Python Recursion | Pythagoras_the_3rd | 1 | 326 | subtree of another tree | 572 | 0.46 | Easy | 10,020 |
https://leetcode.com/problems/subtree-of-another-tree/discuss/1095338/Python-Traversal-Solution | class Solution:
def isIdentical(self, s: TreeNode, t: TreeNode) -> bool:
if not s and not t:
return True
elif not s or not t:
return False
if s.val == t.val:
return self.isIdentical(s.left, t.left) and self.isIdentical(s.right, t.right)
else:
return False
def isSubtree(self, s: TreeNode, t: Tree... | subtree-of-another-tree | Python Traversal Solution | Keyuan_Huang | 1 | 151 | subtree of another tree | 572 | 0.46 | Easy | 10,021 |
https://leetcode.com/problems/subtree-of-another-tree/discuss/1025950/Python-Iterative-Solution | class Solution:
def isSubtree(self, s: TreeNode, t: TreeNode) -> bool:
stack = [s]
tList = self.createTreeList(t)
while stack:
node = stack.pop()
if node.val == t.val and self.createTreeList(node) == tList:
return True
if node.left: stack.a... | subtree-of-another-tree | Python Iterative Solution | JuanRodriguez | 1 | 375 | subtree of another tree | 572 | 0.46 | Easy | 10,022 |
https://leetcode.com/problems/subtree-of-another-tree/discuss/2822270/Python-Solution-or-Easy-approach-or-92.93-Space-Complexity | class Solution:
def dfsCompare(self,root1: Optional[TreeNode],root2: Optional[TreeNode]):
if not root1 and not root2:
return True
if root1 and root2:
return (root1.val == root2.val) and self.dfsCompare(root1.left,root2.left) and self.dfsCompare(root1.right,root2.right)
... | subtree-of-another-tree | Python Solution | Easy approach | 92.93% Space Complexity | vedant_777 | 0 | 5 | subtree of another tree | 572 | 0.46 | Easy | 10,023 |
https://leetcode.com/problems/subtree-of-another-tree/discuss/2747589/Python-No-Recursion-Using-STACK | class Solution:
def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool:
def isSameTree(p, q):
if not p and not q:
return True
elif not q or not p:
return False
if p.val != q.val:
return False
... | subtree-of-another-tree | Python No Recursion Using STACK | taabish_khan22 | 0 | 3 | subtree of another tree | 572 | 0.46 | Easy | 10,024 |
https://leetcode.com/problems/subtree-of-another-tree/discuss/2699012/Simple-Python-solution-using-two-level-recursion | class Solution:
def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool:
def level(root,subRoot):
if not root:
return False
def check(root,subRoot):
if (not subRoot and root) or (not root and subRoot):
return False
elif (not subRoot and not root):
return True... | subtree-of-another-tree | Simple Python solution using two-level recursion | parthdixit | 0 | 6 | subtree of another tree | 572 | 0.46 | Easy | 10,025 |
https://leetcode.com/problems/subtree-of-another-tree/discuss/2673069/Python-solution-O(root-nodes-%2B-subRoot-nodes)-time-complexity | class Solution:
def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool:
if not subRoot:
return True
if not root:
return False
if self.isSametree(root, subRoot):
return True
return (self.isSubtree(root.l... | subtree-of-another-tree | Python solution O(root nodes + subRoot nodes) time complexity | Furat | 0 | 43 | subtree of another tree | 572 | 0.46 | Easy | 10,026 |
https://leetcode.com/problems/subtree-of-another-tree/discuss/2668603/8-Line-Python-Solution | class Solution:
# 8 Line Python Solution
def isSubtree(self, root: Optional[TreeNode], subroot: Optional[TreeNode]) -> bool:
# OG preorder Traversal {node -> left -> right}
def preorder(node):
if not node: return False
elif isIdent(node, subroot): return True
... | subtree-of-another-tree | 8 Line Python Solution 🥶 | shiv-codes | 0 | 12 | subtree of another tree | 572 | 0.46 | Easy | 10,027 |
https://leetcode.com/problems/subtree-of-another-tree/discuss/2668603/8-Line-Python-Solution | class Solution:
def isSubtree(self, root: Optional[TreeNode], subroot: Optional[TreeNode]) -> bool:
res = []
# OG Preorder traversal
def preorder(node):
nonlocal res
if not node:
res.append('#') # Null needs to be accounted for hence putting a #
... | subtree-of-another-tree | 8 Line Python Solution 🥶 | shiv-codes | 0 | 12 | subtree of another tree | 572 | 0.46 | Easy | 10,028 |
https://leetcode.com/problems/subtree-of-another-tree/discuss/2641133/Python-Solution-%3A-Recursive-ft-sameTree | class Solution:
def isSubtree(self, r: Optional[TreeNode], sr: Optional[TreeNode]) -> bool:
if not sr: return True
if not r: return False
if self.sameTree(r,sr):
return True
return (self.isSubtree(r.left, sr) or
self.isSubtree(r.right, sr))
... | subtree-of-another-tree | Python Solution : Recursive ft sameTree ✅ | Khacker | 0 | 73 | subtree of another tree | 572 | 0.46 | Easy | 10,029 |
https://leetcode.com/problems/subtree-of-another-tree/discuss/2518356/Python-DFS-Beats-88-Solution | class Solution:
def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool: # Time: O(root * subRoot) and Space: O(1)
if not subRoot: # when subRoot is Null, root will always have Null values
return True
if not root: # when root is Null, subRoot might ... | subtree-of-another-tree | Python [ DFS / Beats 88%] Solution | DanishKhanbx | 0 | 107 | subtree of another tree | 572 | 0.46 | Easy | 10,030 |
https://leetcode.com/problems/subtree-of-another-tree/discuss/2409507/Simple-Solution-oror-Python-oror-Easily-Understood-oror-iterative-and-recursive | class Solution:
def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool:
if not root:
return False
if root.val == subRoot.val:
q = []
q.append((root.left, subRoot.left))
q.append((root.right, subRoot.right))
al... | subtree-of-another-tree | Simple Solution || 🔥 Python || Easily Understood ✅ || iterative and recursive | wilspi | 0 | 123 | subtree of another tree | 572 | 0.46 | Easy | 10,031 |
https://leetcode.com/problems/subtree-of-another-tree/discuss/2409507/Simple-Solution-oror-Python-oror-Easily-Understood-oror-iterative-and-recursive | class Solution:
def isIdentical(
self, root: Optional[TreeNode], subRoot: Optional[TreeNode]
) -> bool:
if not root and not subRoot:
return True
elif not root or not subRoot:
return False
return (
root.val == subRoot.val
and self.... | subtree-of-another-tree | Simple Solution || 🔥 Python || Easily Understood ✅ || iterative and recursive | wilspi | 0 | 123 | subtree of another tree | 572 | 0.46 | Easy | 10,032 |
https://leetcode.com/problems/subtree-of-another-tree/discuss/2380523/Easy-to-understand-Python-solution | class Solution:
def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool:
# We return false if we have checked every node on current path
if not root: return False
# For every subroot in main tree we check to see if they are the same tree
if self.same_tree(root, subRoot... | subtree-of-another-tree | Easy to understand Python solution | marcus68 | 0 | 64 | subtree of another tree | 572 | 0.46 | Easy | 10,033 |
https://leetcode.com/problems/subtree-of-another-tree/discuss/2379304/Recursive-Python-Solution | class Solution:
def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool:
if subRoot == None:
return True
if root == None:
return False
sameTree = self.isSameTree(root, subRoot)
subTreeOnLeft = self.isSubtree(root.left, subRo... | subtree-of-another-tree | Recursive Python Solution | overlypositiveplayer | 0 | 96 | subtree of another tree | 572 | 0.46 | Easy | 10,034 |
https://leetcode.com/problems/subtree-of-another-tree/discuss/2250327/Python-3-Fully-generator-solution.-Is-it-O(1)-space | class Solution:
def findCandidate(self, root, subRoot):
if root:
if root.val == subRoot.val:
yield root
if root.left:
yield from self.findCandidate(root.left, subRoot)
if root.right:
yield from self.findCandidate(root.right,... | subtree-of-another-tree | [Python 3] Fully generator solution. Is it O(1) space? | mgush | 0 | 60 | subtree of another tree | 572 | 0.46 | Easy | 10,035 |
https://leetcode.com/problems/subtree-of-another-tree/discuss/2042793/Python-DFS | class Solution:
def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool:
# dfs ...
# 1) match roots
# 2) until sub is empty, use dfs to make sure each nodes are equal
rts = []
sbv = subRoot.val
nr = self.fndRoot(root,sbv, rts)
... | subtree-of-another-tree | Python DFS | byyoung3 | 0 | 176 | subtree of another tree | 572 | 0.46 | Easy | 10,036 |
https://leetcode.com/problems/subtree-of-another-tree/discuss/2035773/Beats-86.6-Beginner-Python-Recursion-Solution | class Solution:
def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool:
def isSameTree(root1, root2):
if root1 is None and root2 is None:
return True
elif root1 is None or root2 is None:
return False
else... | subtree-of-another-tree | Beats 86.6% - Beginner Python Recursion Solution | 7yler | 0 | 147 | subtree of another tree | 572 | 0.46 | Easy | 10,037 |
https://leetcode.com/problems/subtree-of-another-tree/discuss/2018654/Python-easy-solution | class Solution:
def matchTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> bool:
if not root1:
if not root2:
return True
return False
if not root2:
return False
if root1.val == root2.val:
... | subtree-of-another-tree | Python easy solution | dbansal18 | 0 | 55 | subtree of another tree | 572 | 0.46 | Easy | 10,038 |
https://leetcode.com/problems/subtree-of-another-tree/discuss/1870585/Python-DFS-Pre-Order | class Solution:
def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool:
if not root and not subRoot:
return True
if not root or not subRoot:
return False
if root.val == subRoot.val:
if self.checkSubtreeStructure(root, s... | subtree-of-another-tree | Python DFS Pre-Order | doubleimpostor | 0 | 122 | subtree of another tree | 572 | 0.46 | Easy | 10,039 |
https://leetcode.com/problems/subtree-of-another-tree/discuss/1847321/python3-simple-recursive-solution | class Solution:
def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool:
if not root or not subRoot:
return False
if root.val == subRoot.val and self.isIdentical(root, subRoot):
return True
return self.isSubtree(root.left, subRoot) or self.i... | subtree-of-another-tree | python3 simple recursive solution | karanbhandari | 0 | 220 | subtree of another tree | 572 | 0.46 | Easy | 10,040 |
https://leetcode.com/problems/subtree-of-another-tree/discuss/1743909/Python-or-DFS | class Solution:
def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool:
def dfs1(node):
if node:
if node.val==subRoot.val:
z=dfs2(node,subRoot)
if z:
return z
return dfs1(n... | subtree-of-another-tree | Python | DFS | heckt27 | 0 | 53 | subtree of another tree | 572 | 0.46 | Easy | 10,041 |
https://leetcode.com/problems/subtree-of-another-tree/discuss/1676456/Merkle-tree-O(N) | class Solution:
def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool:
if root is None:
return False
def add_hash(node):
if not node:
return hash('#')
node.hash = hash(add_hash(node.left) + hash(node.val) ... | subtree-of-another-tree | Merkle tree O(N) | ismaj | 0 | 240 | subtree of another tree | 572 | 0.46 | Easy | 10,042 |
https://leetcode.com/problems/distribute-candies/discuss/1088016/Python.-One-liner-Easy-understanding-solution. | class Solution:
def distributeCandies(self, candyType: List[int]) -> int:
return min(len(candyType) //2, len(set(candyType))) | distribute-candies | Python. One-liner Easy-understanding solution. | m-d-f | 2 | 224 | distribute candies | 575 | 0.661 | Easy | 10,043 |
https://leetcode.com/problems/distribute-candies/discuss/393874/Solution-in-Python-3-(one-line) | class Solution:
def distributeCandies(self, C: List[int]) -> int:
return min(len(C)//2,len(set(C)))
- Junaid Mansuri
(LeetCode ID)@hotmail.com | distribute-candies | Solution in Python 3 (one line) | junaidmansuri | 2 | 367 | distribute candies | 575 | 0.661 | Easy | 10,044 |
https://leetcode.com/problems/distribute-candies/discuss/2178322/Python-Solution-easy-to-understand-oror-beginner-friendly | class Solution:
def distributeCandies(self, candyType: List[int]) -> int:
n=len(candyType)
l=set(candyType)
if len(l)==n//2:
return len(l)
elif len(l)>n//2:
return n//2
else:
return len(l) | distribute-candies | Python Solution- easy to understand || beginner friendly | T1n1_B0x1 | 1 | 47 | distribute candies | 575 | 0.661 | Easy | 10,045 |
https://leetcode.com/problems/distribute-candies/discuss/1236358/easy-solution-with-explanation | class Solution:
def distributeCandies(self, candyType: List[int]) -> int:
sl=len(list(set(candyType))) # count of different candies.
a=len(candyType)/2 # count of how many he can eat.
if a>sl: # if count of how many he can it is greter than count of available candies we return available can... | distribute-candies | easy solution with explanation | souravsingpardeshi | 1 | 137 | distribute candies | 575 | 0.661 | Easy | 10,046 |
https://leetcode.com/problems/distribute-candies/discuss/1102144/Python-simple-solution(EASY-to-understand) | class Solution:
def distributeCandies(self, candyType: List[int]) -> int:
if len(candyType) / 2 >= len(set(candyType)):
return len(set(candyType))
else:
return int(len(candyType) / 2)
``` | distribute-candies | Python simple solution(EASY to understand) | Sissi0409 | 1 | 152 | distribute candies | 575 | 0.661 | Easy | 10,047 |
https://leetcode.com/problems/distribute-candies/discuss/1088409/Python-One-Liner-Sweet-and-Simple-Solution! | class Solution:
def distributeCandies(self, candyType: List[int]) -> int:
return min(len(candyType) //2, len(set(candyType))) | distribute-candies | Python One Liner - Sweet and Simple Solution! | aishwaryanathanii | 1 | 34 | distribute candies | 575 | 0.661 | Easy | 10,048 |
https://leetcode.com/problems/distribute-candies/discuss/788571/python-using-set-and-counter | class Solution:
def distributeCandies(self, candies: List[int]) -> int:
c = candies
s = set()
max_kind = len(c)//2
dif = 0
for i in range(len(c)):
if c[i] not in s and dif != max_kind:
s.add(c[i])
dif+=1
return(len(s)) | distribute-candies | python using set and counter | erjan | 1 | 71 | distribute candies | 575 | 0.661 | Easy | 10,049 |
https://leetcode.com/problems/distribute-candies/discuss/2829797/python | class Solution:
def distributeCandies(self, candyType: List[int]) -> int:
candy_set = set(candyType)
if len(candy_set) >= len(candyType) / 2:
return int(len(candyType) / 2)
else:
return len(candy_set) | distribute-candies | python | Geniuss87 | 0 | 1 | distribute candies | 575 | 0.661 | Easy | 10,050 |
https://leetcode.com/problems/distribute-candies/discuss/2827705/Python-easy-3-line-ans-with-st | class Solution:
def distributeCandies(self, candyType: List[int]) -> int:
l = len(candyType)//2
u_c_l = len(set(candyType))
return min(l,(u_c_l)) | distribute-candies | Python easy 3 line ans with st | pranjalmishra334 | 0 | 1 | distribute candies | 575 | 0.661 | Easy | 10,051 |
https://leetcode.com/problems/distribute-candies/discuss/2822010/Fast-and-easy-python-solution-using-set-oror-Faster-than-92 | class Solution:
def distributeCandies(self, cT: List[int]) -> int:
x = len(cT) / 2 #Maximum number of candies she can eat
cT = set(cT) # number of different types of candy
if x <= len(cT): # if types of candy is more, just return the max she can eat
return int(x)
r... | distribute-candies | Fast and easy python solution using set || Faster than 92% | inusyd | 0 | 1 | distribute candies | 575 | 0.661 | Easy | 10,052 |
https://leetcode.com/problems/distribute-candies/discuss/2796031/Easy-python-every-line-commented-O(logn) | class Solution:
def distributeCandies(self, candyType: List[int]) -> int:
#find the max number of candies she can eat, as per question
_max = int(len(candyType)/2)
#start count for different types at 0
_t = 0
#use Counter to get the keys,
for v in Counter(candyType).... | distribute-candies | Easy python every line commented - O(logn) | ATHBuys | 0 | 1 | distribute candies | 575 | 0.661 | Easy | 10,053 |
https://leetcode.com/problems/distribute-candies/discuss/2743411/python-solution-using-set-and-conditionals | class Solution:
def distributeCandies(self, candyType: List[int]) -> int:
length=len(candyType)//2
ans=list(set(candyType))
ans2=len(ans)
if ans2>length:
return length
elif ans2<=length:
return ans2 | distribute-candies | python solution using set and conditionals | insane_me12 | 0 | 2 | distribute candies | 575 | 0.661 | Easy | 10,054 |
https://leetcode.com/problems/distribute-candies/discuss/2609435/Easy-logic-to-distribute-candies-in-Python-or-faster-than-81 | class Solution(object):
def distributeCandies(self, candyType):
candy=Counter(candyType)
return min(len(candyType)/2,len(candy)) | distribute-candies | Easy logic to distribute candies in Python | faster than 81% | msherazedu | 0 | 15 | distribute candies | 575 | 0.661 | Easy | 10,055 |
https://leetcode.com/problems/distribute-candies/discuss/2587230/Python-One-liner-optimal-Solution-Easy | class Solution:
def distributeCandies(self, candyType: List[int]) -> int:
return min(len(set(candyType)),len(candyType)//2) | distribute-candies | Python One liner optimal Solution Easy | abhisheksanwal745 | 0 | 24 | distribute candies | 575 | 0.661 | Easy | 10,056 |
https://leetcode.com/problems/distribute-candies/discuss/2531523/Python-simple-oneliner | class Solution:
def distributeCandies(self, candyType: List[int]) -> int:
return min(len(candyType)//2, len(set(candyType))) | distribute-candies | Python simple oneliner | StikS32 | 0 | 31 | distribute candies | 575 | 0.661 | Easy | 10,057 |
https://leetcode.com/problems/distribute-candies/discuss/2475271/hashset | class Solution:
def distributeCandies(self, candyType: List[int]) -> int:
# the total number of candies is always even (do not worry about odd)
# use a hashset to store the candy types that alice has
# she can have at most n / 2 candies but the type she can have is the len of the hashset
... | distribute-candies | hashset | andrewnerdimo | 0 | 8 | distribute candies | 575 | 0.661 | Easy | 10,058 |
https://leetcode.com/problems/distribute-candies/discuss/2378648/Python-One-Liner-Sets | class Solution:
def distributeCandies(self, candyType: List[int]) -> int:
return min(len(set(candyType)), (len(candyType)//2)) | distribute-candies | Python One Liner Sets | Yodawgz0 | 0 | 6 | distribute candies | 575 | 0.661 | Easy | 10,059 |
https://leetcode.com/problems/distribute-candies/discuss/2037853/Python3-or-One-line-solution | class Solution:
def distributeCandies(self, candyType: List[int]) -> int:
return min(int(len(candyType) / 2), len(set(candyType))) | distribute-candies | Python3 | One line solution | manfrommoon | 0 | 56 | distribute candies | 575 | 0.661 | Easy | 10,060 |
https://leetcode.com/problems/distribute-candies/discuss/1987138/simple-one-line | class Solution:
def distributeCandies(self, candyType: List[int]) -> int:
return min(len(set(candyType)),len(candyType)//2) | distribute-candies | simple one line | Varunneo | 0 | 20 | distribute candies | 575 | 0.661 | Easy | 10,061 |
https://leetcode.com/problems/distribute-candies/discuss/1973948/Python-One-Line-Solution | class Solution:
def distributeCandies(self, candyType: List[int]) -> int:
return min(len(set(candyType)), len(candyType) // 2)
# set(candyType) will return unique types of candies | distribute-candies | Python One Line Solution | sayantanis23 | 0 | 33 | distribute candies | 575 | 0.661 | Easy | 10,062 |
https://leetcode.com/problems/distribute-candies/discuss/1937034/Python-(Simple-Approach-and-Beginner-Friendly) | class Solution:
def distributeCandies(self, candyType: List[int]) -> int:
cc = int(len(candyType)/2)
unique = set(candyType)
return cc if len(unique) > cc else len(unique) | distribute-candies | Python (Simple Approach and Beginner-Friendly) | vishvavariya | 0 | 39 | distribute candies | 575 | 0.661 | Easy | 10,063 |
https://leetcode.com/problems/distribute-candies/discuss/1893346/Python-3-line-solution-using-sets | class Solution:
def distributeCandies(self, candyType: List[int]) -> int:
if len(candyType) // 2 < len(set(candyType)):
return len(candyType) // 2
return len(set(candyType)) | distribute-candies | Python 3 line solution using sets | alishak1999 | 0 | 26 | distribute candies | 575 | 0.661 | Easy | 10,064 |
https://leetcode.com/problems/distribute-candies/discuss/1883346/Python3-1-Line-Solution | class Solution:
def distributeCandies(self, candyType: List[int]) -> int:
return min(len(candyType)//2,len(set(candyType))) | distribute-candies | [Python3] 1 Line Solution | abhijeetmallick29 | 0 | 14 | distribute candies | 575 | 0.661 | Easy | 10,065 |
https://leetcode.com/problems/distribute-candies/discuss/1836567/1-Line-Python-Solution-oror-92-Faster-oror-Memory-less-than-96 | class Solution:
def distributeCandies(self, candyType: List[int]) -> int:
return min(len(Counter(candyType)),len(candyType)//2) | distribute-candies | 1-Line Python Solution || 92% Faster || Memory less than 96% | Taha-C | 0 | 31 | distribute candies | 575 | 0.661 | Easy | 10,066 |
https://leetcode.com/problems/distribute-candies/discuss/1429395/Easy-set-implementation-in-Python-with-comments!!!! | class Solution:
def distributeCandies(self, candyType: List[int]) -> int:
m = set(candyType) #getting unique types of candyType in here
n = len(candyType)//2 #calculating the length of candies that alice could have according to his doctor
if n <= len(m): #accord... | distribute-candies | Easy set implementation in Python with comments!!!! | adityarichhariya7879 | 0 | 54 | distribute candies | 575 | 0.661 | Easy | 10,067 |
https://leetcode.com/problems/distribute-candies/discuss/1396614/Easy-Python-Solution | class Solution:
def distributeCandies(self, candyType: List[int]) -> int:
length = len(candyType)//2
dis_length = len(set(candyType))
if dis_length < length:
return dis_length
else:
return length | distribute-candies | Easy Python Solution | sangam92 | 0 | 51 | distribute candies | 575 | 0.661 | Easy | 10,068 |
https://leetcode.com/problems/distribute-candies/discuss/1104461/Python3-simple-solution-using-two-approaches | class Solution:
def distributeCandies(self, candyType: List[int]) -> int:
n = len(candyType)//2
x = len(set(candyType))
if n < x:
return n
else:
return x | distribute-candies | Python3 simple solution using two approaches | EklavyaJoshi | 0 | 72 | distribute candies | 575 | 0.661 | Easy | 10,069 |
https://leetcode.com/problems/distribute-candies/discuss/1088350/Python-Solution | class Solution:
def distributeCandies(self, candyType: List[int]) -> int:
counter = Counter(candyType)
n = len(candyType)
return min(len(counter), n//2) | distribute-candies | Python Solution | mariandanaila01 | 0 | 67 | distribute candies | 575 | 0.661 | Easy | 10,070 |
https://leetcode.com/problems/distribute-candies/discuss/1022517/Two-Solutions-simple-and-easy | class Solution:
def distributeCandies(self, candyType: List[int]) -> int:
x,y=len(candyType)//2,len(set(candyType))
if x==y:
return x
elif x<y:
return x
else:
return y | distribute-candies | Two Solutions- simple and easy | thisisakshat | 0 | 21 | distribute candies | 575 | 0.661 | Easy | 10,071 |
https://leetcode.com/problems/distribute-candies/discuss/784138/Python3-92-Faster | class Solution:
def distributeCandies(self, candies: List[int]) -> int:
average = len(candies) // 2
kind = len(set(candies))
if average >= kind :
return kind
else:
return average | distribute-candies | Python3, 92% Faster | DerrickTsui | 0 | 48 | distribute candies | 575 | 0.661 | Easy | 10,072 |
https://leetcode.com/problems/out-of-boundary-paths/discuss/2288190/Python3-oror-recursion-one-grid-w-explanation-oror-TM%3A-8949 | class Solution: # The plan is to accrete the number of paths from the starting cell, which
# is the sum of (a) the number of adjacent positions that are off the grid
# and (b) the number of paths from the adjacent cells in the grid within
... | out-of-boundary-paths | Python3 || recursion , one grid w/ explanation || T/M: 89%/49% | warrenruud | 5 | 371 | out of boundary paths | 576 | 0.443 | Medium | 10,073 |
https://leetcode.com/problems/out-of-boundary-paths/discuss/1294600/Python-simple-recursive-solution-(beats-100-runtime) | class Solution:
def findPaths(self, m: int, n: int, maxMove: int, startRow: int, startColumn: int) -> int:
modulo = 10 ** 9 + 7
@lru_cache(None)
def recursion(move, row, col):
if row == m or row < 0 or col == n or col < 0:
return 1
if move == 0:
... | out-of-boundary-paths | Python, simple recursive solution (beats 100% runtime) | ivanchyshyn14 | 5 | 158 | out of boundary paths | 576 | 0.443 | Medium | 10,074 |
https://leetcode.com/problems/out-of-boundary-paths/discuss/1294532/Python-Super-Easy-to-Understand | class Solution:
def findPaths(self, m: int, n: int, max_move: int, start_row: int, start_col: int) -> int:
memo = {}
mod = 1000000007
def solve(x: int, y: int, move: int) -> int:
if move > max_move:
return 0
if x not in range(m) or y not in range(n):
... | out-of-boundary-paths | Python - Super Easy to Understand | gob1nd | 4 | 304 | out of boundary paths | 576 | 0.443 | Medium | 10,075 |
https://leetcode.com/problems/out-of-boundary-paths/discuss/2288647/Python-DFS-with-Memoization-using-lru_cache | class Solution:
def findPaths(self, m: int, n: int, maxMove: int, startRow: int, startColumn: int) -> int:
@lru_cache(None)
def dfs(curRow,curCol,maxMove):
if curRow >= m or curRow < 0 or curCol >= n or curCol < 0:
return 1
if maxMove <= 0:
... | out-of-boundary-paths | Python, DFS with Memoization using lru_cache | manojkumarmanusai | 1 | 68 | out of boundary paths | 576 | 0.443 | Medium | 10,076 |
https://leetcode.com/problems/out-of-boundary-paths/discuss/1294548/Easy-and-Well-Explained-oror-96-faster-oror-Recursion-and-Memoization | class Solution:
def findPaths(self, m: int, n: int, maxMove: int, startRow: int, startColumn: int) -> int:
MOD = (10**9)+7
dp = [[[-1]*(maxMove+1) for _ in range(n+1)] for _ in range(m+1)]
def dfs(i,j,maxMove):
if maxMove<0:
return 0
if i<0 or i>=m or j<0 or j>... | out-of-boundary-paths | 📌 Easy and Well Explained || 96% faster || Recursion and Memoization 🐍 | abhi9Rai | 1 | 76 | out of boundary paths | 576 | 0.443 | Medium | 10,077 |
https://leetcode.com/problems/out-of-boundary-paths/discuss/2830680/Python-(Simple-DP) | class Solution:
def findPaths(self,m,n,maxMove,startRow,startColumn):
@lru_cache(None)
def dfs(k,i,j):
if i<0 or i>=m or j<0 or j>=n:
return 1
if k == 0:
return 0
total = 0
for ni,nj in [(i-1,j),(i+1,j),(i,j-1),(i,j+1... | out-of-boundary-paths | Python (Simple DP) | rnotappl | 0 | 1 | out of boundary paths | 576 | 0.443 | Medium | 10,078 |
https://leetcode.com/problems/out-of-boundary-paths/discuss/2733632/Simple-DP-with-Memoization | class Solution:
def findPaths(self, m: int, n: int, maxMove: int, startRow: int, startColumn: int) -> int:
memo = {}
visited = set()
rows,cols = m,n
def explore(row, col, moves,memo):
if (row,col, moves) in memo:
return memo[(row,col, moves)]
i... | out-of-boundary-paths | Simple DP with Memoization | vijay_2022 | 0 | 3 | out of boundary paths | 576 | 0.443 | Medium | 10,079 |
https://leetcode.com/problems/out-of-boundary-paths/discuss/2445502/Super-easy-understand-BFS | class Solution:
def findPaths(self, m: int, n: int, maxMove: int, startRow: int, startColumn: int) -> int:
# grid = [[0] * n] * m
q = collections.deque()
q.append((startRow, startColumn))
res = 0
while q:
if maxMove == 0:
break
for _ in... | out-of-boundary-paths | Super easy understand BFS | scr112 | 0 | 27 | out of boundary paths | 576 | 0.443 | Medium | 10,080 |
https://leetcode.com/problems/out-of-boundary-paths/discuss/2305245/Python-Time-133-ms-oror-Memory-18.3-MB-oror-Commented-oror-Easy-to-understand-recursive-method | class Solution:
def findPaths(self, m: int, n: int, maxMove: int, startRow: int, startColumn: int) -> int:
# memoizaation
cache = {}
# recursive function
def dfs(i, j, moves):
# if state is availble in cache, return that result
if (i, j, move... | out-of-boundary-paths | [Python] Time 133 ms || Memory 18.3 MB || Commented || Easy to understand recursive method | Buntynara | 0 | 31 | out of boundary paths | 576 | 0.443 | Medium | 10,081 |
https://leetcode.com/problems/out-of-boundary-paths/discuss/2291402/Python-Runtime%3A-85-ms-faster-than-98.72-of-Python3-online-submissions-for-Out-of-Boundary-Paths. | class Solution:
def findPaths(self, m: int, n: int, maxMove: int, startRow: int, startColumn: int) -> int:
@cache
def max_move(i, j, mx):
if mx == 0: return 0
m1 = max_move(i-1, j, mx-1) if i-1 >= 0 else 1
m2 = max_move(i+1, j, mx-1) if i+1 < m else 1
... | out-of-boundary-paths | Python Runtime: 85 ms, faster than 98.72% of Python3 online submissions for Out of Boundary Paths. | pradyumna04 | 0 | 4 | out of boundary paths | 576 | 0.443 | Medium | 10,082 |
https://leetcode.com/problems/out-of-boundary-paths/discuss/2289327/GoPython-O(row*col*moves)-time-or-O(row*col*moves)-space | class Solution:
def findPaths(self, m: int, n: int, maxMove: int, startRow: int, startColumn: int) -> int:
matrix = [[[-1 for _ in range(maxMove+1)]for _ in range(n+2)]for _ in range(m+2)]
moves = [(-1,0),(1,0),(0,-1),(0,1)]
number_of_ways = dfs(startRow+1,startColumn+1,maxMove,matrix,moves)... | out-of-boundary-paths | Go/Python O(row*col*moves) time | O(row*col*moves) space | vtalantsev | 0 | 18 | out of boundary paths | 576 | 0.443 | Medium | 10,083 |
https://leetcode.com/problems/out-of-boundary-paths/discuss/2288937/DP-Memoization-Solution | class Solution:
def inBound(self, x, y, m, n):
return 0 <= x < m and 0 <= y < n
def dfs(self, m, n, maxMove, x, y, dpMatrix):
mod = 10**9 + 7
if not self.inBound(x, y, m, n):
return 1
if maxMove == 0:
return 0
if dpMatrix[x][y][maxMov... | out-of-boundary-paths | DP Memoization Solution | Vaibhav7860 | 0 | 34 | out of boundary paths | 576 | 0.443 | Medium | 10,084 |
https://leetcode.com/problems/out-of-boundary-paths/discuss/2288925/Python3-Distinct-ways-DP-Top-Down-with-memo-with-comments | class Solution:
def findPaths(self, m: int, n: int, maxMove: int, startRow: int, startColumn: int) -> int:
"""
# 1. Base case:
if move > maxMove:
return
# if ball is going out of boundary
2. if r >= m or c >= n:
... | out-of-boundary-paths | [Python3] Distinct ways DP Top Down with memo with comments | Gp05 | 0 | 3 | out of boundary paths | 576 | 0.443 | Medium | 10,085 |
https://leetcode.com/problems/out-of-boundary-paths/discuss/2288741/Out-of-Boundary-Paths-(August-2022-DLC) | class Solution:
def findPaths(self, m: int, n: int, maxMove: int, startRow: int, startColumn: int) -> int:
visited={}
def check(r,c,moves):
if (r,c,moves) in visited:
return visited[(r,c,moves)]
if r<0 or c<0 or r>=m or c>=n:
return 1
if moves==0:
return 0
ans=0
for x,y in [... | out-of-boundary-paths | Out of Boundary Paths (August 2022 DLC) | garedo123 | 0 | 6 | out of boundary paths | 576 | 0.443 | Medium | 10,086 |
https://leetcode.com/problems/out-of-boundary-paths/discuss/2288191/Cleanest-Python3-solution-with-explanation-Recursive-DP | class Solution:
def findPaths(self, rows: int, cols: int, maxMoves: int, startRow: int, startColumn: int) -> int:
@cache
def find(i, j, m):
if not (0 <= i < rows and 0 <= j < cols):
return 1 # crossed the boundary
if m == 0:
... | out-of-boundary-paths | Cleanest Python3 solution with explanation / Recursive / DP | code-art | 0 | 25 | out of boundary paths | 576 | 0.443 | Medium | 10,087 |
https://leetcode.com/problems/out-of-boundary-paths/discuss/2288155/python3-short-and-simple | class Solution:
def findPaths(self, m: int, n: int, maxMove: int, startRow: int, startColumn: int) -> int:
ans = 0
premap = [[0 for _ in range(n)] for _ in range(m)]
premap[startRow][startColumn] = 1
for _ in range(maxMove):
newmap = [[0 for _ in range(n)] for _ in range(... | out-of-boundary-paths | python3, short and simple | pjy953 | 0 | 8 | out of boundary paths | 576 | 0.443 | Medium | 10,088 |
https://leetcode.com/problems/out-of-boundary-paths/discuss/1785812/Python-easy-to-read-and-understand-or-Memoization | class Solution:
def dfs(self, m, n, row, col, move):
if move < 0:
return 0
if row < 0 or col < 0 or row == m or col == n:
return 1
t = self.dfs(m, n, row-1, col, move-1)
l = self.dfs(m, n, row, col-1, move-1)
d = self.dfs(m, n, row+1, col, move-1)
... | out-of-boundary-paths | Python easy to read and understand | Memoization | sanial2001 | 0 | 90 | out of boundary paths | 576 | 0.443 | Medium | 10,089 |
https://leetcode.com/problems/out-of-boundary-paths/discuss/1785812/Python-easy-to-read-and-understand-or-Memoization | class Solution:
def dfs(self, m, n, row, col, move):
if move < 0:
return 0
if row < 0 or col < 0 or row == m or col == n:
return 1
if (row, col, move) in self.dp:
return self.dp[(row, col, move)]
t = self.dfs(m, n, row-1, col, move-1)
l = s... | out-of-boundary-paths | Python easy to read and understand | Memoization | sanial2001 | 0 | 90 | out of boundary paths | 576 | 0.443 | Medium | 10,090 |
https://leetcode.com/problems/out-of-boundary-paths/discuss/1294841/python3-Recursion-with-memoziation-sol-for-reference. | class Solution:
def findPaths(self, m: int, n: int, maxMove: int, startRow: int, startColumn: int) -> int:
## lru cache to store the results for memoization
@lru_cache(maxsize=100000)
def dfs(node, moves):
(x, y) = node
## fell off the edge, count for 1
if x < 0 or ... | out-of-boundary-paths | [python3] Recursion with memoziation sol for reference. | vadhri_venkat | 0 | 27 | out of boundary paths | 576 | 0.443 | Medium | 10,091 |
https://leetcode.com/problems/out-of-boundary-paths/discuss/884313/Python3-top-down-dp | class Solution:
def findPaths(self, m: int, n: int, maxMove: int, startRow: int, startColumn: int) -> int:
@cache
def fn(i, j, mv):
"""Return out of boundary paths."""
if not (0 <= i < m and 0 <= j < n): return 1
if mv == 0: return 0
return ... | out-of-boundary-paths | [Python3] top-down dp | ye15 | 0 | 75 | out of boundary paths | 576 | 0.443 | Medium | 10,092 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2002965/Python-Simple-Two-Approaches | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
sorted_nums = sorted(nums)
l, u = len(nums) - 1,0
for i in range(len(nums)):
if nums[i]!=sorted_nums[i]:
l=min(l, i)
u=max(u, i)
return 0 if ... | shortest-unsorted-continuous-subarray | Python Simple Two Approaches | constantine786 | 11 | 828 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,093 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2002965/Python-Simple-Two-Approaches | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
l,u = len(nums)-1, 0
stck=[]
for i in range(len(nums)):
while stck and nums[stck[-1]]>nums[i]:
l = min(l, stck.pop())
stck.append(i)
stck=[]
... | shortest-unsorted-continuous-subarray | Python Simple Two Approaches | constantine786 | 11 | 828 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,094 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/536265/Simple-Python-3-beats-90 | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
sortNums = sorted(nums)
if sortNums == nums:
return 0
for i in range(len(nums)):
if nums[i] != sortNums[i]:
firstMismatchIdx = i
break
... | shortest-unsorted-continuous-subarray | Simple Python 3 beats 90% | kstanski | 4 | 546 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,095 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2003703/Python-Simple-and-Clean!-Sort-Solution | class Solution:
def findUnsortedSubarray(self, nums):
n, sortedNums = len(nums), sorted(nums)
mismatchArr = [a != b for a,b in zip(nums,sortedNums)]
mismatchIdx = set(i for i,x in enumerate(mismatchArr) if x)
diff = n - max(mismatchIdx, default=n) + min(mismatchIdx, default=n+1) - 1
... | shortest-unsorted-continuous-subarray | Python - Simple and Clean! Sort Solution | domthedeveloper | 2 | 34 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,096 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2002962/Python-Solution-2-Lines-or-Syntactic-Sugar-or-Very-Simple-Approach-or-90 | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
diff = [i for i, (a, b) in enumerate(zip(nums, sorted(nums))) if a != b]
return len(diff) and max(diff) - min(diff) + 1 | shortest-unsorted-continuous-subarray | [Python Solution] 2 Lines | Syntactic Sugar | Very Simple Approach | 90% | ethanrao | 2 | 37 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,097 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/1391362/Python-or-O(n.logn) | class Solution(object):
def findUnsortedSubarray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
a=nums[:]
a.sort()
c=[]
for i in range(len(a)):
if a[i]!=nums[i]:
c.append(i)
if len(c):
return c[-1... | shortest-unsorted-continuous-subarray | Python | O(n.logn) | nmk0462 | 2 | 103 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,098 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2055149/3-Python-solution-(Sorting-DP-and-most-optimal-one) | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
ns, i, j = sorted(nums), 0, len(nums)-1
while i<len(nums) and ns[i]==nums[i]: i+=1
while j>-1 and ns[j]==nums[j]: j-=1
return j-i+1 if j>i else 0 | shortest-unsorted-continuous-subarray | 3 Python solution (Sorting, DP and most optimal one) | abrarjahin | 1 | 81 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,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.