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/longest-consecutive-sequence/discuss/2598656/simple-python3-solution | class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
if len(nums) == 0:
return 0
max_len, cur_len = 0, 1
nums.sort()
for i in range(1, len(nums)):
if nums[i-1] == nums[i]-1:
cur_len += 1
elif nums[i-1] < nu... | longest-consecutive-sequence | simple python3 solution | codeSheep_01 | 0 | 48 | longest consecutive sequence | 128 | 0.489 | Medium | 1,500 |
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2592483/Simple-Python-O(n)-Solution-or-Beats-99 | class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
ans = 0
s = set(nums)
for i in s:
if i - 1 not in s:
curr = i
c = 1
while curr + 1 in s:
curr += 1
c += 1
... | longest-consecutive-sequence | Simple Python O(n) Solution | Beats 99% | kanchitank | 0 | 68 | longest consecutive sequence | 128 | 0.489 | Medium | 1,501 |
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2571734/Python3-With-Set-or-Faster-Than-95 | class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
longest, s = 0, set(nums)
for num in nums:
if num in s:
l = num - 1
while l in s:
s.remove(l)
l -= 1
r = num + 1
w... | longest-consecutive-sequence | Python3 With Set | Faster Than 95% | ryangrayson | 0 | 47 | longest consecutive sequence | 128 | 0.489 | Medium | 1,502 |
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2558082/Faster-than-99.36-Solutions | class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
if not nums:
return 0
res = 0
n = set(nums)
m = list(n)
l = sorted(m)
count = 1
for i in range(1,len(l)):
if l[i]==(l[i-1]+1):
count+=1
el... | longest-consecutive-sequence | Faster than 99.36% Solutions | jayeshvarma | 0 | 115 | longest consecutive sequence | 128 | 0.489 | Medium | 1,503 |
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2480393/Python-Sorting-Solution | class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
nums = sorted(nums)
index = 0
maxLen = 0
while index < len(nums):
l = 1
while index+1 < len(nums) and (nums[index+1]-nums[index] == 0 or nums[index+1]-nums[index] == 1):
if n... | longest-consecutive-sequence | Python Sorting Solution | DietCoke777 | 0 | 40 | longest consecutive sequence | 128 | 0.489 | Medium | 1,504 |
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2446418/Janky-Python-Solution-using-Dynamic-Programming | class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
ilen = len(nums)
if ilen < 1:
return 0
elif ilen == 1:
return 1
hash_map = dict()
longest = 0
cache = dict()
for num in nums:
hash_map[num] ... | longest-consecutive-sequence | Janky Python Solution using Dynamic Programming | DavidLlanio | 0 | 81 | longest consecutive sequence | 128 | 0.489 | Medium | 1,505 |
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2425862/Python-oror-Simple-and-Easy-solution | class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
nums = set(nums)
longest = 0
for num in nums:
if num - 1 not in nums:
curr = num
while curr + 1 in nums:
curr += 1
longest = max(longest, curr... | longest-consecutive-sequence | Python || Simple and Easy solution | Gyalecta | 0 | 82 | longest consecutive sequence | 128 | 0.489 | Medium | 1,506 |
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2402503/python-simple-solution-O(nlogn) | class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
if len(nums) == 0:
return 0
nums.sort()
count = 1
max_count = 1
for i in range(1,len(nums)):
if nums[i]-1==nums[i-1]:
count += 1
if max_count < count:... | longest-consecutive-sequence | python simple solution O(nlogn) | AshishGohil | 0 | 15 | longest consecutive sequence | 128 | 0.489 | Medium | 1,507 |
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2327609/Python-6872-test-passed-Time-complexity-O(n*k) | class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
lengthLong=0
prevMap = {}
for i, n in enumerate(nums):
prevMap[n]=i
for i, n in enumerate(nums):
count=1
while n+1 in prevMap:
count+=1
n+=1
... | longest-consecutive-sequence | Python 68/72 test passed Time complexity O(n*k) | Simon-Huang-1 | 0 | 9 | longest consecutive sequence | 128 | 0.489 | Medium | 1,508 |
https://leetcode.com/problems/sum-root-to-leaf-numbers/discuss/1557540/Python-99-speed-99-memory | class Solution:
def sumNumbers(self, root: Optional[TreeNode]) -> int:
def helper(node, num):
if node is None:
return 0
num = num * 10 + node.val
if node.left is None and node.right is None:
return num
return helper(node.left, n... | sum-root-to-leaf-numbers | Python 99% speed, 99% memory | dereky4 | 4 | 391 | sum root to leaf numbers | 129 | 0.588 | Medium | 1,509 |
https://leetcode.com/problems/sum-root-to-leaf-numbers/discuss/703693/Python3-iterative-and-recursive-dfs | class Solution:
def sumNumbers(self, root: Optional[TreeNode]) -> int:
ans = 0
stack = [(root, 0)]
while stack:
node, val = stack.pop()
val = 10*val + node.val
if not node.left and not node.right: ans += val
if node.left: stack.append((node... | sum-root-to-leaf-numbers | [Python3] iterative & recursive dfs | ye15 | 2 | 68 | sum root to leaf numbers | 129 | 0.588 | Medium | 1,510 |
https://leetcode.com/problems/sum-root-to-leaf-numbers/discuss/703693/Python3-iterative-and-recursive-dfs | class Solution:
def sumNumbers(self, root: TreeNode) -> int:
def fn(node, val):
"""Return sum of node-to-leaf numbers"""
if not node: return 0
val = 10*val + node.val
if not node.left and not node.right: return val
return fn(node.left, va... | sum-root-to-leaf-numbers | [Python3] iterative & recursive dfs | ye15 | 2 | 68 | sum root to leaf numbers | 129 | 0.588 | Medium | 1,511 |
https://leetcode.com/problems/sum-root-to-leaf-numbers/discuss/1557634/Python-Easy-and-Clean-DFS-Solution | class Solution:
def sumNumbers(self, root: Optional[TreeNode]) -> int:
ans = 0
def dfs(node, num):
if node.left:
dfs(node.left, num*10 + node.left.val)
if node.right:
dfs(node.right, num*10 + node.right.val)
if not node... | sum-root-to-leaf-numbers | [Python] Easy & Clean DFS Solution | nomofika | 1 | 105 | sum root to leaf numbers | 129 | 0.588 | Medium | 1,512 |
https://leetcode.com/problems/sum-root-to-leaf-numbers/discuss/1416739/Level-by-level-no-recursion-96-speed | class Solution:
def sumNumbers(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
ans = 0
row = [[root, []]]
while row:
new_row = []
for node, lst_val in row:
if not node.left and not node.right:
ans +... | sum-root-to-leaf-numbers | Level by level, no recursion, 96% speed | EvgenySH | 1 | 165 | sum root to leaf numbers | 129 | 0.588 | Medium | 1,513 |
https://leetcode.com/problems/sum-root-to-leaf-numbers/discuss/913856/Python-easy-recursive-dfs | class Solution:
def sumNumbers(self, root: TreeNode) -> int:
def solve(node, cur=0):
if not node: return 0
if not node.left and not node.right:
return cur * 10 + node.val
return solve(node.left, cur*10 + node.val) + solve(node.right, cur * 10 + node.val)
... | sum-root-to-leaf-numbers | Python easy recursive dfs | modusV | 1 | 108 | sum root to leaf numbers | 129 | 0.588 | Medium | 1,514 |
https://leetcode.com/problems/sum-root-to-leaf-numbers/discuss/2812117/python3-10-line-Sol.-faster-then-96.3 | class Solution:
def sumNumbers(self, root: Optional[TreeNode]) -> int:
def helper(root,ans):
if root is None:
return 0
ans=ans*10 + root.val
if root.left is None and root.right is None:
return ans
return helper(root.left,ans) + ... | sum-root-to-leaf-numbers | python3 10 line Sol. faster then 96.3% | pranjalmishra334 | 0 | 4 | sum root to leaf numbers | 129 | 0.588 | Medium | 1,515 |
https://leetcode.com/problems/sum-root-to-leaf-numbers/discuss/2806095/EASIEST-SIMPLE-PYTHON-RECUSRSION-WITH-COMMENTS | class Solution:
def sumNumbers(self, root: Optional[TreeNode]) -> int:
arr = self.helper(root,"",[])
print(arr)
return sum(list(map(int,list(arr))))
def helper(self,root,string,arr):
if(root is None):
return ""
# ON REACHING LEAF NODE
... | sum-root-to-leaf-numbers | EASIEST SIMPLE PYTHON RECUSRSION WITH COMMENTS | MAYANK-M31 | 0 | 2 | sum root to leaf numbers | 129 | 0.588 | Medium | 1,516 |
https://leetcode.com/problems/sum-root-to-leaf-numbers/discuss/2803145/Python-(Faster-than-90)-or-DFS-solution | class Solution:
def sumNumbers(self, root: Optional[TreeNode]) -> int:
res = 0
def dfs(node, num):
nonlocal res
if node:
num.append(str(node.val))
if not node.left and not node.right:
res += int(''.join(num))
... | sum-root-to-leaf-numbers | Python (Faster than 90%) | DFS solution | KevinJM17 | 0 | 1 | sum root to leaf numbers | 129 | 0.588 | Medium | 1,517 |
https://leetcode.com/problems/sum-root-to-leaf-numbers/discuss/2659651/Python-3-7-liner-super-simple-and-easy-to-understand | class Solution:
def sumNumbers(self, root: Optional[TreeNode]) -> int:
def dfs(node, curr_num):
if node is None:
return 0
curr_num += str(node.val)
if node.left is None and node.right is None:
return int(curr_num)
... | sum-root-to-leaf-numbers | Python 3 7-liner, super simple and easy to understand | zakmatt | 0 | 12 | sum root to leaf numbers | 129 | 0.588 | Medium | 1,518 |
https://leetcode.com/problems/sum-root-to-leaf-numbers/discuss/2639371/Python-DFS-Easy-Simple-5-line-code | class Solution:
def sumNumbers(self, root: Optional[TreeNode]) -> int:
def dfs(root, num):
if root is None: return 0
num = num * 10 + root.val
if root.left is None and root.right is None:
return num
return dfs(root.left, num) + dfs(ro... | sum-root-to-leaf-numbers | ✔️ [Python] DFS Easy, Simple 5 line code | girraj_14581 | 0 | 9 | sum root to leaf numbers | 129 | 0.588 | Medium | 1,519 |
https://leetcode.com/problems/sum-root-to-leaf-numbers/discuss/2519954/Easy-Python-solution-BFS-(Beats-95) | class Solution:
def sumNumbers(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
q=deque()
q.append([root,str(root.val)])
res=0
while q:
node,sval=q.popleft()
if not node.left and not node.right:
res+=int(sval)... | sum-root-to-leaf-numbers | Easy Python solution, BFS (Beats 95%) | shatheesh | 0 | 25 | sum root to leaf numbers | 129 | 0.588 | Medium | 1,520 |
https://leetcode.com/problems/sum-root-to-leaf-numbers/discuss/2490970/python-easy-dfs-solution-TC-%3A-O(N)-SC-(N) | class Solution:
def solve(self , root , s):
if(not root): return 0;
s = s*10 + root.val
if(not root.left and not root.right):
return s
return self.solve(root.left , s) + self.solve(root.right , s)
def sumNumbers(self, root: Optional[TreeNode]) -> i... | sum-root-to-leaf-numbers | python easy dfs solution TC : O(N) SC (N) | rajitkumarchauhan99 | 0 | 20 | sum root to leaf numbers | 129 | 0.588 | Medium | 1,521 |
https://leetcode.com/problems/sum-root-to-leaf-numbers/discuss/2421822/Python3-or-collect-num-from-root-2-leaf-join-list-of-'nums'-and-then-return-int | class Solution:
def helper(self, node: Optional[TreeNode], nums=[])-> int:
if not node:
return 0
nums += [node.val]
if not node.left and not node.right:
num_str = ''.join([str(n) for n in nums])
return int(num_str)
return self.helper(node.l... | sum-root-to-leaf-numbers | Python3 | collect num from root 2 leaf, join list of 'nums' and then return int | Ploypaphat | 0 | 13 | sum root to leaf numbers | 129 | 0.588 | Medium | 1,522 |
https://leetcode.com/problems/sum-root-to-leaf-numbers/discuss/1984633/Simple-Recursive-Solution-In-Python | class Solution:
def sumNumbers(self, root: Optional[TreeNode]) -> int:
self.ans = 0
def trav(root,val):
if not root:
return root
val += str(root.val)
if not root.left and not root.right:
self.ans += i... | sum-root-to-leaf-numbers | Simple Recursive Solution In Python | gamitejpratapsingh998 | 0 | 62 | sum root to leaf numbers | 129 | 0.588 | Medium | 1,523 |
https://leetcode.com/problems/sum-root-to-leaf-numbers/discuss/1845919/Python3-Solution | class Solution:
def sumNumbers(self, root: Optional[TreeNode]) -> int:
arr=[]
def helper(s,root):
if not root.left and not root.right:
arr.append(int(s+str(root.val)))
return
if root.left:
helper(s+str(root.val),root.left)
if root.right:
helper(s+str(root.val),root.right)
helper("",root)... | sum-root-to-leaf-numbers | Python3 Solution | eaux2002 | 0 | 21 | sum root to leaf numbers | 129 | 0.588 | Medium | 1,524 |
https://leetcode.com/problems/sum-root-to-leaf-numbers/discuss/1835116/Python-Recursion-with-helper | class Solution:
def sumNumbers(self, root: Optional[TreeNode]) -> int:
if root.val is None:
return 0
elif root.right is None and root.left is None:
return root.val
else:
s = str(root.val)
lst = self.sum_number_helper(root.left, s)
l... | sum-root-to-leaf-numbers | Python - Recursion with helper | omaralawadhi | 0 | 30 | sum root to leaf numbers | 129 | 0.588 | Medium | 1,525 |
https://leetcode.com/problems/sum-root-to-leaf-numbers/discuss/1761068/Silly-1-line-solution | class Solution:
def sumNumbers(self, n: Optional[TreeNode], path=0) -> int:
return (0 if n.left is None else self.sumNumbers(n.left, 10*path + n.val)) + (0 if n.right is None else self.sumNumbers(n.right, 10*path + n.val)) + (10*path + n.val if n.left is None and n.right is None else 0) | sum-root-to-leaf-numbers | Silly 1 line solution | JingXiaoLuo | 0 | 63 | sum root to leaf numbers | 129 | 0.588 | Medium | 1,526 |
https://leetcode.com/problems/sum-root-to-leaf-numbers/discuss/1724135/Python3-simple-solution-using-queue | class Solution:
def sumNumbers(self, root: Optional[TreeNode]) -> int:
queue = [(root,str(root.val))]
count = 0
while queue:
flag = 0
node, s = queue.pop(0)
if node.left:
queue.append((node.left, s + str(node.left.val)))
else:
... | sum-root-to-leaf-numbers | Python3 simple solution using queue | EklavyaJoshi | 0 | 34 | sum root to leaf numbers | 129 | 0.588 | Medium | 1,527 |
https://leetcode.com/problems/sum-root-to-leaf-numbers/discuss/1579862/python3-Solution-or-4-line-answer | class Solution:
def sumNumbers(self, root ,ans = 0) -> int:
if not root: return 0
ans = ans*10 + root.val
if (not root.left) and (not root.right): return ans
return self.sumNumbers(root.left,ans)+self.sumNumbers(root.right,ans) | sum-root-to-leaf-numbers | python3 Solution | 4 line answer | satyam2001 | 0 | 59 | sum root to leaf numbers | 129 | 0.588 | Medium | 1,528 |
https://leetcode.com/problems/sum-root-to-leaf-numbers/discuss/1558138/Python-Simple-recursive-solution | class Solution:
def dfs(self, node, partial):
global ans
if node == None:
return
if node.left == None and node.right == None:
ans += int(partial+str(node.val))
return
self.dfs(node.left, partial+str(node.val))
self.dfs(node.right, partial+s... | sum-root-to-leaf-numbers | [Python] Simple recursive solution | mizan-ali | 0 | 37 | sum root to leaf numbers | 129 | 0.588 | Medium | 1,529 |
https://leetcode.com/problems/sum-root-to-leaf-numbers/discuss/1557846/Python3-with-recursive-depth-first-search | class Solution:
path = []
sol = 0
def sumNumbers(self, root: Optional[TreeNode]) -> int:
self.path.append(str(root.val))
if root.left == None and root.right == None:
self.sol += int("".join(self.path))
if root.left: self.sumNumbers(root.left)
if root.right: self.s... | sum-root-to-leaf-numbers | Python3 with recursive depth first search | __Br1__ | 0 | 44 | sum root to leaf numbers | 129 | 0.588 | Medium | 1,530 |
https://leetcode.com/problems/sum-root-to-leaf-numbers/discuss/1557569/Python-DFS-solution | class Solution:
def sumNumbers(self, root: Optional[TreeNode]) -> int:
def dfs(node, num):
if not node:
return
if not node.left and not node.right: #leaf node
self.res += int(''.join(num) + str(node.val))
return
... | sum-root-to-leaf-numbers | Python DFS solution | abkc1221 | 0 | 18 | sum root to leaf numbers | 129 | 0.588 | Medium | 1,531 |
https://leetcode.com/problems/sum-root-to-leaf-numbers/discuss/1557311/Python3-Easy-Solution | class Solution:
def sumNumbers(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
p = {root: root.val}
c = dict()
res = 0
while p:
for node, number in p.items():
if node.left:
c[node.left] = number*10 + no... | sum-root-to-leaf-numbers | Python3 Easy Solution | VicV13 | 0 | 17 | sum root to leaf numbers | 129 | 0.588 | Medium | 1,532 |
https://leetcode.com/problems/sum-root-to-leaf-numbers/discuss/1557118/Python-consice-DFS-solution | class Solution:
def sumNumbers(self, root: Optional[TreeNode]) -> int:
paths = []
def dfs(node, runningSum):
if not node:
return
if not node.right and not node.left:
newSum = runningSum + str(node.val)
path... | sum-root-to-leaf-numbers | Python consice DFS solution | vineeth_moturu | 0 | 26 | sum root to leaf numbers | 129 | 0.588 | Medium | 1,533 |
https://leetcode.com/problems/sum-root-to-leaf-numbers/discuss/1556748/Python3-DFS-Solution-with-Explanation-or-5-Lines-or-96-faster-or-LC-Daily-Challenge-Nov3-2021 | class Solution:
def sumNumbers(self, root: Optional[TreeNode]) -> int:
def sumnumbers(root,res):
if root is None: return 0
res=int(str(res)+str(root.val))
if root.left == root.right == None: return res
return sumnumbers(r... | sum-root-to-leaf-numbers | [Python3] DFS Solution with Explanation | 5 Lines | 96% faster | LC Daily Challenge Nov3 2021 | suhana9010 | 0 | 31 | sum root to leaf numbers | 129 | 0.588 | Medium | 1,534 |
https://leetcode.com/problems/sum-root-to-leaf-numbers/discuss/1556597/Python3-or-String-Based-Approach-or-Recursion-or-Simple-Explanation | class Solution:
def _create_numbers(self, root):
if not root:
return []
if not root.left and not root.right:
return [f'{root.val}']
_curr = root.val
_left_numbers = self._create_numbers(root.left)
_right_numbers = self._create_numbers(roo... | sum-root-to-leaf-numbers | Python3 | String Based Approach | Recursion | Simple Explanation | tg68 | 0 | 15 | sum root to leaf numbers | 129 | 0.588 | Medium | 1,535 |
https://leetcode.com/problems/sum-root-to-leaf-numbers/discuss/1556273/Python-or-Simple-DFS-or-Preorder-or-90 | class Solution:
def sumNumbers(self, root: Optional[TreeNode]) -> int:
ans = 0
def dfs(root: Optional[TreeNode], s: int) -> int:
nonlocal ans
if not root:
return
if not(root.left or root.right):
ans += s + root.val
r... | sum-root-to-leaf-numbers | Python | Simple DFS | Preorder | 90% | PuneethaPai | 0 | 34 | sum root to leaf numbers | 129 | 0.588 | Medium | 1,536 |
https://leetcode.com/problems/sum-root-to-leaf-numbers/discuss/1556025/Python-One-liner-Recursion%3A-Easy-to-understand-with-Explanation | class Solution:
def sumNumbers(self, root: TreeNode, curr: Optional[int] = 0) -> int:
"""
Recursively traverse to the leaf nodes of a given binary tree and obtain total sum.
:param root: The root node of the binary tree.
Note that there is at least 1 node in the tree, so we can guarantee that root... | sum-root-to-leaf-numbers | Python One-liner Recursion: Easy-to-understand with Explanation | zayne-siew | 0 | 63 | sum root to leaf numbers | 129 | 0.588 | Medium | 1,537 |
https://leetcode.com/problems/sum-root-to-leaf-numbers/discuss/1556025/Python-One-liner-Recursion%3A-Easy-to-understand-with-Explanation | class Solution:
def sumNumbers(self, root: TreeNode, curr: Optional[int] = 0) -> int:
return ((self.sumNumbers(root.left, curr*10+root.val) if root.left else 0) + (self.sumNumbers(root.right, curr*10+root.val) if root.right else 0)) if root.left or root.right else (curr*10+root.val) | sum-root-to-leaf-numbers | Python One-liner Recursion: Easy-to-understand with Explanation | zayne-siew | 0 | 63 | sum root to leaf numbers | 129 | 0.588 | Medium | 1,538 |
https://leetcode.com/problems/sum-root-to-leaf-numbers/discuss/1555861/python-ez-solution | class Solution:
def sumNumbers(self, root: Optional[TreeNode]) -> int:
output = 0
curr = ''
stack = [(root, curr)]
while stack:
vertex,curr = stack.pop()
curr += str(vertex.val)
if not vertex.left and not vertex.right:
output += int... | sum-root-to-leaf-numbers | python ez solution | yingziqing123 | 0 | 24 | sum root to leaf numbers | 129 | 0.588 | Medium | 1,539 |
https://leetcode.com/problems/sum-root-to-leaf-numbers/discuss/1482574/Simple-oror-Easy-Approach-oror-97-Faster | class Solution:
def sumNumbers(self, root: Optional[TreeNode]) -> int:
def path(root,local):
nonlocal res
if root is None:
return
if root.left is None and root.right is None:
res.append(local*10+root.val)
return
path(root.left,local*... | sum-root-to-leaf-numbers | 📌📌 Simple || Easy-Approach || 97% Faster 🐍 | abhi9Rai | 0 | 52 | sum root to leaf numbers | 129 | 0.588 | Medium | 1,540 |
https://leetcode.com/problems/surrounded-regions/discuss/558746/Python-DFS-Easy-solution-with-comments | class Solution:
def dfs(self,board,i,j):
if i<0 or j<0 or i>=len(board) or j>=len(board[0]) or board[i][j]!='O':
return
board[i][j]='$' # converting to a dollar sign
self.dfs(board,i+1,j)
self.dfs(board,i-1,j)
self.dfs(board,i,j+1... | surrounded-regions | Python DFS Easy solution with comments | JoyRafatAshraf | 12 | 440 | surrounded regions | 130 | 0.361 | Medium | 1,541 |
https://leetcode.com/problems/surrounded-regions/discuss/1552267/Question-Explanation-is-very-Bad-oror-Well-Explained-Question-and-Solution-oror-Easy | class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
m,n = len(board),len(board[0])
def dfs(i,j):
if i<0 or i>=m or j<0 or j>=n or board[i][j]!="O":
return
board[i][j] = "*"
dfs(i+1,j)
dfs(i-1,j)
dfs(i,j+1)
dfs(... | surrounded-regions | 📌📌 Question Explanation is very Bad || Well-Explained Question and Solution || Easy 🐍 | abhi9Rai | 6 | 145 | surrounded regions | 130 | 0.361 | Medium | 1,542 |
https://leetcode.com/problems/surrounded-regions/discuss/1189857/DFS-oror-PYTHON-oror-98-faster-oror-Easy-to-understand | class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
def dfs(i,j):
if i<0 or i>=m or j<0 or j>=n or board[i][j]!='O':
return
board[i][j]="*"
dfs(i+1,j)
dfs(i-1,j)
dfs(i... | surrounded-regions | DFS || PYTHON || 98% faster || Easy to understand | abhi9Rai | 3 | 199 | surrounded regions | 130 | 0.361 | Medium | 1,543 |
https://leetcode.com/problems/surrounded-regions/discuss/1156085/Python3-DFS-faster-than-98 | class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
rows = len(board)
if rows <= 2:
return board
cols = len(board[0])
if cols <= 2:
return board
... | surrounded-regions | Python3 DFS faster than 98% | faris-shi | 3 | 136 | surrounded regions | 130 | 0.361 | Medium | 1,544 |
https://leetcode.com/problems/surrounded-regions/discuss/2287456/Python3-oror-98-Faster-and-Efficient-oror-Easy-and-Explained | class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
def bfs(r, c):
q = deque([(r,c)])
while q:
r, c = q.popleft()
if board[r][c]... | surrounded-regions | Python3 || 98% Faster and Efficient || Easy & Explained | Dewang_Patil | 2 | 95 | surrounded regions | 130 | 0.361 | Medium | 1,545 |
https://leetcode.com/problems/surrounded-regions/discuss/2186815/Python-DFS-with-full-working-explanation | class Solution:
def solve(self, board: List[List[str]]) -> None:
rows, cols = len(board), len(board[0])
def capture(r, c):
if r < 0 or c < 0 or r == rows or c == cols or board[r][c] != 'O': # index bounds conditions
return
board[r][c] = 'T'
... | surrounded-regions | Python DFS with full working explanation | DanishKhanbx | 2 | 62 | surrounded regions | 130 | 0.361 | Medium | 1,546 |
https://leetcode.com/problems/surrounded-regions/discuss/1552007/Different-Approach-or-Union-Find-or-Python-or-Explanation | class DisjointSet:
def __init__(self, n):
self.n = n
self.id = [0]*n
for i in range(n):
self.id[i] = i
def find(self, i:int) -> int:
while(i!=self.id[i]):
self.id[i] = self.id[self.id[i]]
i = self.id[i]
return i
... | surrounded-regions | Different Approach | Union Find | Python | Explanation | CaptainX | 2 | 310 | surrounded regions | 130 | 0.361 | Medium | 1,547 |
https://leetcode.com/problems/surrounded-regions/discuss/452471/Python-with-clear-BFS-solution-144ms. | class Solution:
def solve(self, board: List[List[str]]) -> None:
if not board or board is None:
return
row, col = len(board), len(board[0])
queueBorder = collections.deque([])
for i in range(row):
for j in range(col):
... | surrounded-regions | Python with clear BFS solution, 144ms. | yzfeng89 | 2 | 118 | surrounded regions | 130 | 0.361 | Medium | 1,548 |
https://leetcode.com/problems/surrounded-regions/discuss/2652417/Python-way-Simple-DFS | class Solution:
def solve(self, mat: List[List[str]]) -> None:
n=len(mat)
m=len(mat[0])
def dfs(i,j):
visited[i][j]=1
dir = [[-1,0],[0,1],[1,0],[0,-1]]
for a,b in dir:
row = a+i
col = b+j
if row>=0 and row<n and col>=0 and col<m and mat[row][col]=='O' and not visited[row][col]:
dfs(row... | surrounded-regions | Python way - Simple DFS | prateekgoel7248 | 1 | 75 | surrounded regions | 130 | 0.361 | Medium | 1,549 |
https://leetcode.com/problems/surrounded-regions/discuss/2445767/Python-2-color-technique | class Solution:
def solve(self, board: List[List[str]]) -> None:
## dfs solution
row = len(board)
col = len(board[0])
visited = set()
def dfs(board, x, y, visited):
if x<0 or y< 0 or x>= row or y >= col or (x,y) in visited or board[x][y] != 'O':
re... | surrounded-regions | Python 2 color technique | Abhi_009 | 1 | 52 | surrounded regions | 130 | 0.361 | Medium | 1,550 |
https://leetcode.com/problems/surrounded-regions/discuss/2034976/python-3-step-easy-and-efficient-DFS-solution | class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
#step 2
def dfs(r, c) :
if board[r][c] == "O" and (r,c) not in s :
s.add((r,c))
if r-1 >= 0 : dfs(r-1, c)
... | surrounded-regions | python 3 step easy and efficient DFS solution | runtime-terror | 1 | 64 | surrounded regions | 130 | 0.361 | Medium | 1,551 |
https://leetcode.com/problems/surrounded-regions/discuss/2028067/Python-dfs-solution | class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
visited = set()
def dfs(i, j, visited):
if (i, j) in visited:
return
if board[i][j] == 'X':
... | surrounded-regions | Python dfs solution | user6397p | 1 | 44 | surrounded regions | 130 | 0.361 | Medium | 1,552 |
https://leetcode.com/problems/surrounded-regions/discuss/1983363/Python3-Runtime%3A-156ms-73.98-Memory%3A-16mb-50.42 | class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
rows, cols = len(board), len(board[0])
self.getBoardRegions(board, rows, cols)
self.changeUnVisitedRegions(board, rows, cols)
... | surrounded-regions | Python3 Runtime: 156ms 73.98% Memory: 16mb 50.42% | arshergon | 1 | 51 | surrounded regions | 130 | 0.361 | Medium | 1,553 |
https://leetcode.com/problems/surrounded-regions/discuss/1812247/Solution-that-you-want-%3A | class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
def bfs(board,i,j):
if i>=0 and j>=0 and i<len(board) and j<len(board[0]) and board[i][j]=='O':
board[i][j]='A'
... | surrounded-regions | Solution that you want : | goxy_coder | 1 | 77 | surrounded regions | 130 | 0.361 | Medium | 1,554 |
https://leetcode.com/problems/surrounded-regions/discuss/1462844/WEEB-DOES-PYTHON-BFS | class Solution:
def solve(self, board: List[List[str]]) -> None:
row, col = len(board), len(board[0])
queue = deque([(0,i) for i in range(col) if board[0][i] == "O"]+ [(row-1,i) for i in range(col) if board[row-1][i] == "O"] + [(i,0) for i in range(1,row-1) if board[i][0] == "O"] + [(i,col-1) for i in range(1,row-... | surrounded-regions | WEEB DOES PYTHON BFS | Skywalker5423 | 1 | 111 | surrounded regions | 130 | 0.361 | Medium | 1,555 |
https://leetcode.com/problems/surrounded-regions/discuss/1247595/Easy-and-simple-Python-DFS-approach | class Solution:
def dfs(self, board, i, j):
if 0 <= i < len(board) and 0 <= j < len(board[0]) and board[i][j] == 'O':
board[i][j] = 'E'
self.dfs(board, i+1, j)
self.dfs(board, i-1, j)
self.dfs(board, i, j+1)
self.dfs(board, i, j-1)
... | surrounded-regions | Easy and simple Python DFS approach | jaipoo | 1 | 279 | surrounded regions | 130 | 0.361 | Medium | 1,556 |
https://leetcode.com/problems/surrounded-regions/discuss/692254/Python3-12-line-flood-fill-(98.83) | class Solution:
def solve(self, board: List[List[str]]) -> None:
m, n = len(board), len(board[0])
def fn(i, j):
"""Flood fill "O" with sentinel"""
if 0 <= i < m and 0 <= j < n and board[i][j] == "O":
board[i][j] = "#" #sentinel
for i... | surrounded-regions | [Python3] 12-line flood fill (98.83%) | ye15 | 1 | 43 | surrounded regions | 130 | 0.361 | Medium | 1,557 |
https://leetcode.com/problems/surrounded-regions/discuss/692254/Python3-12-line-flood-fill-(98.83) | class Solution:
def solve(self, board: List[List[str]]) -> None:
m, n = len(board), len(board[0])
stack = []
for i in range(m):
for j in range(n):
if (i in (0, m-1) or j in (0, n-1)) and board[i][j] == 'O':
board[i][j] = '#'
... | surrounded-regions | [Python3] 12-line flood fill (98.83%) | ye15 | 1 | 43 | surrounded regions | 130 | 0.361 | Medium | 1,558 |
https://leetcode.com/problems/surrounded-regions/discuss/2830534/BFS-(Personal-Notes) | class Solution(object):
def solve(self, board):
"""
:type board: List[List[str]]
:rtype: None Do not return anything, modify board in-place instead.
"""
if not board or not board[0]:
return
self.ROWS = len(board)
self.COLS = len(board[0])
... | surrounded-regions | BFS (Personal Notes) | Farawayy | 0 | 1 | surrounded regions | 130 | 0.361 | Medium | 1,559 |
https://leetcode.com/problems/surrounded-regions/discuss/2827472/Easy-to-understand-python-solution-using-DFS. | class Solution:
def solve(self, board: List[List[str]]) -> None:
m, n = len(board), len(board[0])
vis = []
for i in range(m):
temp = []
for j in range(n):
temp.append(0)
vis.append(temp)
// marking all the "O" accessible from the ... | surrounded-regions | Easy to understand python solution using DFS. | i-haque | 0 | 3 | surrounded regions | 130 | 0.361 | Medium | 1,560 |
https://leetcode.com/problems/surrounded-regions/discuss/2825948/Python3-DFS-and-Union-Find-methods | class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
# Method1: DFS.
def dfs(x, y):
if x < 0 or x > len(board)-1 or y < 0 or y > len(board[0])-1 or board[x][y] != 'O':
retur... | surrounded-regions | [Python3] DFS and Union Find methods | Cceline00 | 0 | 3 | surrounded regions | 130 | 0.361 | Medium | 1,561 |
https://leetcode.com/problems/surrounded-regions/discuss/2825948/Python3-DFS-and-Union-Find-methods | class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
# Method2: UnionFind.
def find(x): # Find parent
if parents[x] != x:
parents[x] = find(parents[x])
return pa... | surrounded-regions | [Python3] DFS and Union Find methods | Cceline00 | 0 | 3 | surrounded regions | 130 | 0.361 | Medium | 1,562 |
https://leetcode.com/problems/surrounded-regions/discuss/2803618/Python-Easy-linear-solution-with-explanations-no-additional-memory | class Solution:
def solve(self, board: List[List[str]]) -> None:
m = len(board)
n = len(board[0])
# Recursive filling of the region
def fill_area(y,x: int):
if y < 0 or x < 0 or y >= m or x >= n: return
if board[y][x] == 'Y':
board[y][x] = 'O... | surrounded-regions | [Python] Easy linear solution with explanations, no additional memory | dlesha | 0 | 7 | surrounded regions | 130 | 0.361 | Medium | 1,563 |
https://leetcode.com/problems/surrounded-regions/discuss/2789805/Python-or-Easy-intuitive-DFS-solution | class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
rows, cols = len(board), len(board[0])
visited = set()
def dfs(r, c):
if r not in range(rows) or c not in range(cols) or (r, c)... | surrounded-regions | Python | Easy intuitive DFS solution | KevinJM17 | 0 | 9 | surrounded regions | 130 | 0.361 | Medium | 1,564 |
https://leetcode.com/problems/surrounded-regions/discuss/2781649/Python-oror-Iterative-DFS | class Solution:
def solve(self, board: List[List[str]]) -> None:
n, m = len(board), len(board[0])
adj = [(-1,0), (0,1), (1,0), (0,-1)]
graph = defaultdict(list)
for i in range(n):
for j in range(m):
for r,c in adj:
if not (0 <= i + r < n and 0 <= j + c < m): continue
graph[(i,j)].append((i+r,j... | surrounded-regions | Python || Iterative DFS | morpheusdurden | 0 | 7 | surrounded regions | 130 | 0.361 | Medium | 1,565 |
https://leetcode.com/problems/surrounded-regions/discuss/2744682/Easy-understanding-python-solution | class Solution:
def solve(self, b: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
m, n = len(b), len(b[0])
def flood(i, j, b):
if i < 0 or i >= m or j < 0 or j >= n or b[i][j] != "O":
return
if ... | surrounded-regions | Easy understanding python solution | jackson-cmd | 0 | 4 | surrounded regions | 130 | 0.361 | Medium | 1,566 |
https://leetcode.com/problems/surrounded-regions/discuss/2742999/Python-DFS-solution | class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
row = len(board)
col = len(board[0])
def capture(r, c):
if (r<0 or c<0 or r==row or c==col or board[r][c]!="O"):
... | surrounded-regions | Python DFS solution | gcheng81 | 0 | 5 | surrounded regions | 130 | 0.361 | Medium | 1,567 |
https://leetcode.com/problems/surrounded-regions/discuss/2739876/Python-Union-Find-Solution | class UF:
def __init__(self, n):
self.parent = [i for i in range(n)]
self.count = n
def _find(self, p):
while p != self.parent[p]:
self.parent[p] = self.parent[self.parent[p]]
p = self.parent[p]
return p
def _union(self, p, q):
rootP ... | surrounded-regions | Python Union Find Solution | Rui_Liu_Rachel | 0 | 3 | surrounded regions | 130 | 0.361 | Medium | 1,568 |
https://leetcode.com/problems/surrounded-regions/discuss/2739790/Python-Floodfill-Simple-Solution | class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
m = len(board)
n = len(board[0])
visited = [[False for _ in range(n)] for _ in range(m)]
steps = [[1, 0], [-1, 0], [0, 1],... | surrounded-regions | Python Floodfill Simple Solution | Rui_Liu_Rachel | 0 | 2 | surrounded regions | 130 | 0.361 | Medium | 1,569 |
https://leetcode.com/problems/surrounded-regions/discuss/2723871/BFS-Two-Easy-Approaches-in-O(N2) | class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
def BFS(x, y, rows, columns):
q = deque([(x, y)])
while q:
x, y = q.popleft()
visited.add((x, y))
... | surrounded-regions | BFS - Two Easy Approaches in O(N^2) | user6770yv | 0 | 7 | surrounded regions | 130 | 0.361 | Medium | 1,570 |
https://leetcode.com/problems/surrounded-regions/discuss/2723871/BFS-Two-Easy-Approaches-in-O(N2) | class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
def capture_if_valid(x, y, rows, columns):
q = deque([(x, y)])
visited = set()
while q:
x, y = q.popleft... | surrounded-regions | BFS - Two Easy Approaches in O(N^2) | user6770yv | 0 | 7 | surrounded regions | 130 | 0.361 | Medium | 1,571 |
https://leetcode.com/problems/surrounded-regions/discuss/2711298/Python-%2B-Chinese-Comment | class UF:
def __init__(self,n):
# 存储若干棵树
self.parent = [i for i in range(n)]
# 记录树的“重量”
self.size = [1 for i in range(n)]
# 记录连通分量个数
self.count = n
def find(self,x):
# 返回节点 x 的根节点
while self.parent[x] != x:
# 进行路径压缩
self.pa... | surrounded-regions | Python + Chinese Comment | Michael_Songru | 0 | 3 | surrounded regions | 130 | 0.361 | Medium | 1,572 |
https://leetcode.com/problems/surrounded-regions/discuss/2697826/Dfs | class Solution:
def solve(self, matrix: List[List[str]]) -> None:
visited = set()
#false = set()
r, c = len(matrix), len(matrix[0])
def dfs(i, j):
if i < 0 or j < 0 or i >= r or j >= c or (i,j) in visited or matrix[i][j] == 'X':
retu... | surrounded-regions | Dfs | mukundjha | 0 | 2 | surrounded regions | 130 | 0.361 | Medium | 1,573 |
https://leetcode.com/problems/surrounded-regions/discuss/2683173/Python-BFS-oror-Easy-to-understand | class Solution:
def solve(self, board: List[List[str]]) -> None:
ROWS = len(board)
COLS = len(board[0])
def bfs(i, j):
seen = set()
q = collections.deque()
q.append((i, j))
surrounded = True
while q:
for _ in range(... | surrounded-regions | Python BFS || Easy to understand | yllera | 0 | 6 | surrounded regions | 130 | 0.361 | Medium | 1,574 |
https://leetcode.com/problems/surrounded-regions/discuss/2681316/DFS-SOLUTION-IN-PYTHON | class Solution:
def solve(self, board: List[List[str]]) -> None:
def dfs(i,j):
if i<0 or i>=m or j<0 or j>=n or board[i][j]!='O':
return
board[i][j]='#' #CONVERTING TO A DOLLAR SIGN
dfs(i+1,j)
dfs(i-1,j)
dfs(i,j-1)
dfs(... | surrounded-regions | DFS SOLUTION IN PYTHON | shashank_2000 | 0 | 28 | surrounded regions | 130 | 0.361 | Medium | 1,575 |
https://leetcode.com/problems/surrounded-regions/discuss/2666192/Python%3A-BFS-Solution | class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
n = len(board)
m = len(board[0])
q = deque([])
visited = set()
for i in range(n):
for j in range(m):
... | surrounded-regions | Python: BFS Solution | vijay_2022 | 0 | 4 | surrounded regions | 130 | 0.361 | Medium | 1,576 |
https://leetcode.com/problems/surrounded-regions/discuss/2640557/Clean-Python3-or-BFS-or-In-Place | class Solution:
def solve(self, board: List[List[str]]) -> None:
neighbors = [(0, -1), (-1, 0), (0, 1), (1, 0)]
def can_reach_edge(st_row, st_col):
q, seen = deque([(st_row, st_col)]), {(st_row, st_col)}
while q:
row, col = q.pop()
for... | surrounded-regions | Clean Python3 | BFS | In Place | ryangrayson | 0 | 58 | surrounded regions | 130 | 0.361 | Medium | 1,577 |
https://leetcode.com/problems/surrounded-regions/discuss/2627777/python3-oror-easy-oror-dfs-solution | class Solution:
def solve(self, board: List[List[str]]) -> None:
if not board:
return
rowSize=len(board)
colSize=len(board[0])
visited=[[0]*colSize for i in range(rowSize)]
directions=[[-1,0],[1,0],[0,1],[0,-1]]
def dfs(row,col)... | surrounded-regions | python3 || easy || dfs solution | _soninirav | 0 | 21 | surrounded regions | 130 | 0.361 | Medium | 1,578 |
https://leetcode.com/problems/surrounded-regions/discuss/2545751/Python-DFS-solution-with-explanation | class Solution:
m, n = 0, 0
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
self.m = len(board)
self.n = len(board[0])
# check skipped first
for i in range(self.m):
if board[i][0] ... | surrounded-regions | Python DFS solution with explanation | haoxj0122 | 0 | 20 | surrounded regions | 130 | 0.361 | Medium | 1,579 |
https://leetcode.com/problems/surrounded-regions/discuss/2497543/128-ms-or-99.7-faster-or-Python-or-DFS-or-Easy-to-Understand | class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
rows, cols = len(board), len(board[0])
visited = set()
def dfs(r, c):
if (
r < 0
or r >... | surrounded-regions | 128 ms | 99.7% faster | Python | DFS | Easy to Understand | ronakpatel2198 | 0 | 81 | surrounded regions | 130 | 0.361 | Medium | 1,580 |
https://leetcode.com/problems/surrounded-regions/discuss/2437942/Surrounded-Regions-oror-Python3-oror-DFS | class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
dirs = [[1, 0], [0, 1], [-1, 0], [0, -1]]
# DFS on all 'O' from border and marking them as 'V'
for i in range(0, len(board)):
... | surrounded-regions | Surrounded Regions || Python3 || DFS | vanshika_2507 | 0 | 17 | surrounded regions | 130 | 0.361 | Medium | 1,581 |
https://leetcode.com/problems/surrounded-regions/discuss/2415830/Python-DFS-Understandable-solution | class Solution:
def dfs(self,board,i,j):
board[i][j]='y'
for a,b in ((i-1,j),(i+1,j),(i,j-1),(i,j+1)):
if a>=len(board) or b>=len(board[0]) or a<0 or b<0:
continue
if board[a][b]=='O':
self.dfs(board,a,b)
def solve(self, board: List[List[st... | surrounded-regions | Python DFS Understandable solution | Neerajbirajdar | 0 | 70 | surrounded regions | 130 | 0.361 | Medium | 1,582 |
https://leetcode.com/problems/surrounded-regions/discuss/2401508/130.-My-Python-and-JAVASolution-with-comments | class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
m, n = len(board), len(board[0])
# we only have to check the boundary, which means first row&column, last row&column.
# If an O e... | surrounded-regions | 130. My Python and JAVASolution with comments | JunyiLin | 0 | 19 | surrounded regions | 130 | 0.361 | Medium | 1,583 |
https://leetcode.com/problems/surrounded-regions/discuss/2121907/Python-or-Food-Fill-or-O(n-2)-Time-O(1)-Space | class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
m = len(board)
n = len(board[0])
def traverse(i, j):
if i < 0 or i >= m or j < 0 or j >= n:
return
... | surrounded-regions | Python | Food-Fill | O(n ^2) Time, O(1) Space | Kiyomi_ | 0 | 57 | surrounded regions | 130 | 0.361 | Medium | 1,584 |
https://leetcode.com/problems/surrounded-regions/discuss/2022556/Python-DFS-Solution-Explained | class Solution:
def dfs(self, board, i, j):
self.visited[i][j] = True
for x, y in [(0,1),(1,0),(0,-1),(-1,0)]:
ni = i + x
nj = j + y
if 0 <= ni < self.m and 0 <= nj < self.n:
if (not self.visited[ni][nj]) and (board[ni][nj] ==... | surrounded-regions | Python DFS Solution Explained | dbansal18 | 0 | 27 | surrounded regions | 130 | 0.361 | Medium | 1,585 |
https://leetcode.com/problems/surrounded-regions/discuss/2008137/Python-easy-to-read-and-understand-or-dfs | class Solution:
def dfs(self, board, row, col):
if row < 0 or col < 0 or row == len(board) or col == len(board[0]) or board[row][col] != 'O':
return
board[row][col] = 1
self.dfs(board, row-1, col)
self.dfs(board, row, col-1)
self.dfs(board, row+1, col)
sel... | surrounded-regions | Python easy to read and understand | dfs | sanial2001 | 0 | 31 | surrounded regions | 130 | 0.361 | Medium | 1,586 |
https://leetcode.com/problems/surrounded-regions/discuss/1894413/DFS-Easy-Solution-In-Python | class Solution:
def solve(self, board: List[List[str]]) -> None:
row,col = len(board),len(board[0])
visited = set()
def dfs(r,c):
if (r,c) not in visited and 0<=r<row and 0<=c<col and board[r][c]=='O':
visited.add((r,c))
... | surrounded-regions | DFS Easy Solution In Python | gamitejpratapsingh998 | 0 | 109 | surrounded regions | 130 | 0.361 | Medium | 1,587 |
https://leetcode.com/problems/surrounded-regions/discuss/1851509/Python-or-BFS | class Solution:
def solve(self, board: List[List[str]]) -> None:
safeplace=set()
r,c=len(board),len(board[0])
for i in range(r):
if board[i][0]=='O':
safeplace.add((i,0))
if board[i][c-1]=='O':
safeplace.add((i,c-1))
for j in ra... | surrounded-regions | Python | BFS | heckt27 | 0 | 48 | surrounded regions | 130 | 0.361 | Medium | 1,588 |
https://leetcode.com/problems/surrounded-regions/discuss/1636261/Python-simple-solution%3A-start-from-boundaries! | class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
m, n = len(board), len(board[0])
if m <=2 or n <=2:
return board
# now let's assume m>=3 and n>=3
from collections impor... | surrounded-regions | Python simple solution: start from boundaries! | byuns9334 | 0 | 83 | surrounded regions | 130 | 0.361 | Medium | 1,589 |
https://leetcode.com/problems/surrounded-regions/discuss/1623550/PYTHON-or-Solution-faster-then-bullet | class Solution:
def dfs(self,board,i,j):
if i<0 or j<0 or i>=len(board) or j>=len(board[0]):
return
elif board[i][j]=='X' or board[i][j]=='1':
return
elif board[i][j] == 'O':
board[i][j] = '1'
li = [(0,1),(1,0),(-1,0)... | surrounded-regions | PYTHON🐍 | Solution faster then bullet | Brillianttyagi | 0 | 114 | surrounded regions | 130 | 0.361 | Medium | 1,590 |
https://leetcode.com/problems/surrounded-regions/discuss/1619543/Python-DFS | class Solution:
def solve(self, board: List[List[str]])->None:
def isSafe(i,j,grid,visited):
if 0<=i<len(grid) and 0<=j<len(grid[0]) and visited[i][j]==False and grid[i][j]=='O':
return True
return False
def travel(row,col,board,visited):
... | surrounded-regions | Python DFS | Zach0787 | 0 | 86 | surrounded regions | 130 | 0.361 | Medium | 1,591 |
https://leetcode.com/problems/surrounded-regions/discuss/1610304/BFS-Python-3 | class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
m = len(board)
n = len(board[0])
for i in range(m):
for j in range(n):
visited = set()
... | surrounded-regions | BFS Python 3 | throwawayleetcoder19843 | 0 | 51 | surrounded regions | 130 | 0.361 | Medium | 1,592 |
https://leetcode.com/problems/palindrome-partitioning/discuss/1667786/Python-Simple-Recursion-oror-Detailed-Explanation-oror-Easy-to-Understand | class Solution(object):
@cache # the memory trick can save some time
def partition(self, s):
if not s: return [[]]
ans = []
for i in range(1, len(s) + 1):
if s[:i] == s[:i][::-1]: # prefix is a palindrome
for suf in self.partition(s[i:]): # process suffix r... | palindrome-partitioning | ✅ [Python] Simple Recursion || Detailed Explanation || Easy to Understand | linfq | 209 | 9,200 | palindrome partitioning | 131 | 0.626 | Medium | 1,593 |
https://leetcode.com/problems/palindrome-partitioning/discuss/1667786/Python-Simple-Recursion-oror-Detailed-Explanation-oror-Easy-to-Understand | class Solution(object):
def __init__(self):
self.memory = collections.defaultdict(list)
def partition(self, s):
if not s: return [[]]
if s in self.memory: return self.memory[s] # the memory trick can save some time
ans = []
for i in range(1, len(s) + 1):
... | palindrome-partitioning | ✅ [Python] Simple Recursion || Detailed Explanation || Easy to Understand | linfq | 209 | 9,200 | palindrome partitioning | 131 | 0.626 | Medium | 1,594 |
https://leetcode.com/problems/palindrome-partitioning/discuss/1667603/Python3-RECURSION-Explained | class Solution:
def partition(self, s: str) -> List[List[str]]:
l = len(s)
def isPalindrom(s):
return s == s[::-1]
@cache
def rec(start):
if start == l:
return []
res = []
for i in range(start + 1, l +... | palindrome-partitioning | ❤ [Python3] RECURSION, Explained | artod | 5 | 472 | palindrome partitioning | 131 | 0.626 | Medium | 1,595 |
https://leetcode.com/problems/palindrome-partitioning/discuss/2004437/Python-Bottom-up-DP-Solution-oror-100-Faster-oror-Iterative-oror-Easy-to-understand | class Solution:
def partition(self, s: str) -> List[List[str]]:
dp = []
n = len(s)
for i in range(n+1):
dp.append([]) # create dp of size n+1
dp[-1].append([]) # because for s[n:] i.e. empty string , answer = [[]]
# dp[i] store al... | palindrome-partitioning | Python Bottom up DP Solution || 100% Faster || Iterative || Easy to understand | Laxman_Singh_Saini | 3 | 154 | palindrome partitioning | 131 | 0.626 | Medium | 1,596 |
https://leetcode.com/problems/palindrome-partitioning/discuss/2263662/Easy-Python-Backtracking-Solution | class Solution:
def partition(self, s: str) -> List[List[str]]:
res, part = [], []
def dfs(i):
if i >= len(s):
res.append(part.copy())
return
for j in range(i, len(s)):
if isPali(s, i, j):
part.appen... | palindrome-partitioning | Easy Python Backtracking Solution | shikha_pandey | 2 | 125 | palindrome partitioning | 131 | 0.626 | Medium | 1,597 |
https://leetcode.com/problems/palindrome-partitioning/discuss/1669234/Python-Full-Code-Explanation-or-Backtracking | class Solution:
def partition(self, s: str) -> List[List[str]]:
result = [] # will store all posible partitions
partition = [] # current partition
# function for backtracking
def dfs(i): # i is the index of the character we are currently at
if i >= len(s): # chec... | palindrome-partitioning | Python Full Code Explanation | Backtracking | yashitanamdeo | 2 | 311 | palindrome partitioning | 131 | 0.626 | Medium | 1,598 |
https://leetcode.com/problems/palindrome-partitioning/discuss/1668647/Python3-backtracking-solution | class Solution:
def partition(self, s: str) -> List[List[str]]:
substring = []
result = []
# backtracking
def dfs(i):
if i >= len(s):
result.append(substring.copy())
return
for j in range(i, len(s)):
... | palindrome-partitioning | Python3 backtracking solution | Janetcxy | 2 | 75 | palindrome partitioning | 131 | 0.626 | Medium | 1,599 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.